comparison doc/interpreter/java.txi @ 16392:801297f14e4b

doc: Improve documentation for Java chapter and java functions. * doc/interpreter/java.txi, scripts/java/javamem.m, scripts/java/listdlg.m, scripts/java/msgbox.m, scripts/java/questdlg.m: Improve documentation for Java chapter and java functions.
author Rik <rik@octave.org>
date Fri, 29 Mar 2013 12:05:45 -0700
parents 1834b91292ab
children 52c661334154
comparison
equal deleted inserted replaced
16391:a695ee2dc17e 16392:801297f14e4b
22 @cindex using Octave with Java 22 @cindex using Octave with Java
23 @cindex Java, using with Octave 23 @cindex Java, using with Octave
24 @cindex calling Java from Octave 24 @cindex calling Java from Octave
25 @cindex Java, calling from Octave 25 @cindex Java, calling from Octave
26 @cindex calling Octave from Java 26 @cindex calling Octave from Java
27 @cindex Octave, calling from Java 27 @cindex Octave, calling from Java
28 28
29 The Java Interface is designed for calling Java functions from within Octave. 29 The Java Interface is designed for calling Java functions from within Octave.
30 If you want to do the reverse, and call Octave from within Java, try 30 If you want to do the reverse, and call Octave from within Java, try
31 a library like 31 a library like
32 @code{javaOctave} (@url{http://kenai.com/projects/javaOctave}) or 32 @code{javaOctave} (@url{http://kenai.com/projects/javaOctave}) or
33 @code{joPas} (@url{http://jopas.sourceforge.net}). 33 @code{joPas} (@url{http://jopas.sourceforge.net}).
34 34
35 @menu 35 @menu
36 * Java Interface Functions:: 36 * Java Interface Functions::
37 * Dialog Box Functions:: 37 * Dialog Box Functions::
38 * FAQ - Frequently asked Questions:: 38 * FAQ - Frequently asked Questions::
39 @end menu 39 @end menu
40 40
41 @node Java Interface Functions 41 @node Java Interface Functions
42 @section Java Interface Functions 42 @section Java Interface Functions
43 43
44 The following functions are the core of the Java Interface. They provide 44 The following functions are the core of the Java Interface. They provide
45 a way to create a Java object, get and set its data fields, and call Java 45 a way to create a Java object, get and set its data fields, and call Java
46 methods which return results to Octave. 46 methods which return results to Octave.
47 47
48 @cindex object, creating a Java object 48 @cindex object, creating a Java object
49 @DOCSTRING(javaObject) 49 @DOCSTRING(javaObject)
50 50
51 @cindex fields, displaying available fields of a Java object 51 @cindex fields, displaying available fields of a Java object
52 FIXME: Need documentation on how fieldnames() is overloaded to return 52 @strong{FIXME:} Need documentation on how fieldnames() is overloaded to return
53 the methods of a Java object. 53 the methods of a Java object.
54 54
55 @cindex field, returning value of Java object field 55 @cindex field, returning value of Java object field
56 FIXME: Need documentation on how to use structure-like indexing 56 @strong{FIXME:} Need documentation on how to use structure-like indexing
57 to get fields from Java object. 57 to get fields from Java object.
58 58
59 @cindex field, setting value of Java object field 59 @cindex field, setting value of Java object field
60 FIXME: Need documentation on how to use structure-like indexing 60 @strong{FIXME:} Need documentation on how to use structure-like indexing
61 to set fields from Java object. 61 to set fields from Java object.
62 62
63 @DOCSTRING(isjava) 63 @DOCSTRING(isjava)
64 64
65 @cindex array, creating a Java array 65 @cindex array, creating a Java array
67 67
68 @cindex method, invoking a method of a Java object 68 @cindex method, invoking a method of a Java object
69 @DOCSTRING(javaMethod) 69 @DOCSTRING(javaMethod)
70 70
71 @cindex methods, displaying available methods of a Java object 71 @cindex methods, displaying available methods of a Java object
72 FIXME: Need documentation on how methods() is overloaded to return 72 @strong{FIXME:} Need documentation on how methods() is overloaded to return
73 the methods of a Java object. 73 the methods of a Java object.
74 74
75 The following three functions are used to display and modify the 75 The following three functions are used to display and modify the
76 class path used by the Java Virtual Machine. This is entirely separate 76 class path used by the Java Virtual Machine. This is entirely separate
77 from Octave's PATH variable and is used by the JVM to find the correct 77 from Octave's PATH variable and is used by the JVM to find the correct
139 @c ------------------------------------------------------------------------ 139 @c ------------------------------------------------------------------------
140 @node FAQ - Frequently asked Questions 140 @node FAQ - Frequently asked Questions
141 @section FAQ - Frequently asked Questions 141 @section FAQ - Frequently asked Questions
142 142
143 @menu 143 @menu
144 * How to distinguish between Octave and Matlab?:: 144 * How to distinguish between Octave and @sc{matlab}?::
145 * How to make Java classes available?:: 145 * How to make Java classes available?::
146 * How to create an instance of a Java class?:: 146 * How to create an instance of a Java class?::
147 * How can I handle memory limitations?:: 147 * How can I handle memory limitations?::
148 * Which @TeX{} symbols are implemented in the dialog functions?:: 148 * Which @TeX{} symbols are implemented in the dialog functions?::
149 @end menu 149 @end menu
150 150
151 @c ------------------------------------------------------------------------ 151 @c ------------------------------------------------------------------------
152 @node How to distinguish between Octave and Matlab? 152 @node How to distinguish between Octave and @sc{matlab}?
153 @subsection How to distinguish between Octave and Matlab? 153 @subsection How to distinguish between Octave and @sc{matlab}?
154 @anchor{doc-FAQ} 154 @anchor{doc-FAQ}
155 @c - index - 155 @c - index -
156 @cindex Octave and Matlab, how to distinguish between 156 @cindex Octave and @sc{matlab}, how to distinguish between
157 @c - index - 157 @c - index -
158 158
159 Octave and @sc{matlab} are very similar, but handle Java slightly different. 159 Octave and @sc{matlab} are very similar, but handle Java slightly different.
160 Therefore it may be necessary to detect the environment and use the appropriate 160 Therefore it may be necessary to detect the environment and use the appropriate
161 functions. The following function can be used to detect the environment. Due 161 functions. The following function can be used to detect the environment. Due
162 to the persistent variable it can be called repeatedly without a heavy 162 to the persistent variable it can be called repeatedly without a heavy
163 performance hit. 163 performance hit.
164 164
165 Example: 165 Example:
166 166
167 @example 167 @example
168 @group 168 @group
169 %% 169 %%
170 %% Return: true if the environment is Octave. 170 %% Return: true if the environment is Octave.
171 %% 171 %%
172 function ret = isOctave 172 function retval = isOctave
173 persistent retval; % speeds up repeated calls 173 persistent cacheval; % speeds up repeated calls
174 174
175 if isempty(retval) 175 if isempty (cacheval)
176 retval = (exist('Octave_VERSION','builtin') > 0); 176 cacheval = (exist ('OCTAVE_VERSION', 'builtin') > 0);
177 end 177 end
178 178
179 ret = retval; 179 retval = cacheval;
180 end 180 end
181 @end group 181 @end group
182 @end example 182 @end example
183 183
184 @c ------------------------------------------------------------------------ 184 @c ------------------------------------------------------------------------
185 @node How to make Java classes available? 185 @node How to make Java classes available?
187 @c - index - 187 @c - index -
188 @cindex classpath, setting 188 @cindex classpath, setting
189 @cindex classpath, difference between static and dynamic 189 @cindex classpath, difference between static and dynamic
190 @cindex static classpath 190 @cindex static classpath
191 @cindex dynamic classpath 191 @cindex dynamic classpath
192 @cindex @file{javaclasspath.txt}
192 @cindex @file{classpath.txt} 193 @cindex @file{classpath.txt}
193 @cindex classes, making available to Octave 194 @cindex classes, making available to Octave
194 @c - index - 195 @c - index -
195 196
196 Java finds classes by searching a @var{classpath}. This is a list of Java 197 Java finds classes by searching a @var{classpath}. This is a list of Java
197 archive files and/or directories containing class files. In Octave and 198 archive files and/or directories containing class files. In Octave
198 @sc{matlab} the @var{classpath} is composed of two parts: 199 the @var{classpath} is composed of two parts:
199 200
200 @itemize 201 @itemize
201 @item the @var{static classpath} is initialized once at startup of the JVM, and 202 @item the @var{static classpath} is initialized once at startup of the JVM, and
202 203
203 @item the @var{dynamic classpath} which can be modified at runtime. 204 @item the @var{dynamic classpath} which can be modified at runtime.
204 @end itemize 205 @end itemize
205 206
206 Octave searches the @var{static classpath} first, then the @var{dynamic 207 Octave searches the @var{static classpath} first, then the @var{dynamic
207 classpath}. Classes appearing in the @var{static} as well as in the 208 classpath}. Classes appearing in the @var{static} as well as in the
208 @var{dynamic classpath} will therefore be found in the @var{static classpath} 209 @var{dynamic classpath} will therefore be found in the @var{static classpath}
209 and loaded from this location. Classes which shall be used regularly or must 210 and loaded from this location. Classes which will be used frequently or must
210 be available to all users should be added to the @var{static classpath}. The 211 be available to all users should be added to the @var{static classpath}. The
211 @var{static classpath} is populated once from the contents of a plain text file 212 @var{static classpath} is populated once from the contents of a plain text file
212 named @file{classpath.txt} or @file{javaclasspath.txt} when the Java Virtual 213 named @file{javaclasspath.txt} (or @file{classpath.txt} historically) when the
213 Machine starts. This file contains one line for each individual classpath to be 214 Java Virtual Machine starts. This file contains one line for each individual
214 added to the @var{static classpath}. These lines can identify single class 215 classpath to be added to the @var{static classpath}. These lines can identify
215 files, directories containing class files or Java archives with complete class 216 single class files, directories containing class files, or Java archives with
216 file hierarchies. Comment lines starting with a @code{#} or a @code{%} 217 complete class file hierarchies. Comment lines starting with a @samp{#} or a
217 character are ignored. 218 @samp{%} character are ignored.
218 219
219 The search rules for the file @file{classpath.txt} (or @file{javaclasspath.txt}) 220 The search rules for the file @file{javaclasspath.txt}
220 are: 221 (or @file{classpath.txt}) are:
221 222
222 @itemize 223 @itemize
223 @item First, Octave tries to locate it in the current directory (where Octave 224 @item First, Octave tries to locate it in the current directory (where Octave
224 was started from). If such a file is found, it is read and defines the initial 225 was started from). If such a file is found, it is read and defines the initial
225 @var{static classpath}. So it is possible to define a static classpath on a 'per 226 @var{static classpath}. Thus, it is possible to define a static classpath on a
226 Octave invocation' basis. 227 'per Octave invocation' basis.
227 228
228 @item Next, Octave searches in the user's home directory. If a file 229 @item Next, Octave searches in the user's home directory. If a file
229 @file{classpath.txt} (or @file{javaclasspath.txt}) exists here, its contents 230 @file{javaclasspath.txt} exists here, its contents are appended to the static
230 are appended to the static classpath (if any). Thus it is possible to build an 231 classpath (if any). Thus, it is possible to build an initial static classpath
231 initial static classpath on a 'per user' basis. 232 on a 'per user' basis.
232 233
233 @item Finally, Octave looks for a next occurrence of file @file{classpath.txt} 234 @item Finally, Octave looks for a next occurrence of file
234 (@file{javaclasspath.txt}) in the m-files directory where Octave Java functions 235 @file{javaclasspath.txt} in the m-files directory where Octave Java functions
235 live. This is where @file{javaclasspath.m} resides, usually something like 236 live. This is where @file{javaclasspath.m} resides, usually something like
236 @file{@dots{}/share/octave/packages/<version>/m/java}. You can find this 237 @file{@env{OCTAVE_HOME}/share/octave/@env{OCTAVE_VERSION}/m/java/}. You can
237 directory by executing the command 238 find this directory by executing the command
238 239
239 @example 240 @example
240 which javaclasspath 241 which javaclasspath
241 @end example 242 @end example
242 243
243 If this file exists here, its contents are also appended to the static classpath. 244 If this file exists here, its contents are also appended to the static
244 Note that the archives and class directories defined in this last step will affect 245 classpath. Note that the archives and class directories defined in this last
245 all users. 246 step will affect all users.
246 @end itemize 247 @end itemize
247 248
248 Classes which are used only by a specific script should be placed in the 249 Classes which are used only by a specific script should be placed in the
249 @var{dynamic classpath}. This portion of the classpath can be modified at 250 @var{dynamic classpath}. This portion of the classpath can be modified at
250 runtime using the @code{javaaddpath} and @code{javarmpath} functions. 251 runtime using the @code{javaaddpath} and @code{javarmpath} functions.
251 252
252 Example: 253 Example:
253 254
254 @example 255 @example
255 octave> base_path = 'C:/Octave/java_files'; 256 octave> base_path = 'C:/Octave/java_files';
256 257
257 octave> % add two JARchives to the dynamic classpath 258 octave> % add two JARchives to the dynamic classpath
258 octave> javaaddpath([base_path, '/someclasses.jar']); 259 octave> javaaddpath ([base_path, '/someclasses.jar']);
259 octave> javaaddpath([base_path, '/moreclasses.jar']); 260 octave> javaaddpath ([base_path, '/moreclasses.jar']);
260 261
261 octave> % check the dynamic classpath 262 octave> % check the dynamic classpath
262 octave> p = javaclasspath; 263 octave> p = javaclasspath;
263 octave> disp(p@{1@}); 264 octave> disp (p@{1@});
264 C:/Octave/java_files/someclasses.jar 265 C:/Octave/java_files/someclasses.jar
265 octave> disp(p@{2@}); 266 octave> disp (p@{2@});
266 C:/Octave/java_files/moreclasses.jar 267 C:/Octave/java_files/moreclasses.jar
267 268
268 octave> % remove the first element from the classpath 269 octave> % remove the first element from the classpath
269 octave> javarmpath([base_path, '/someclasses.jar']); 270 octave> javarmpath ([base_path, '/someclasses.jar']);
270 octave> p = javaclasspath; 271 octave> p = javaclasspath;
271 octave> disp(p@{1@}); 272 octave> disp (p@{1@});
272 C:/Octave/java_files/moreclasses.jar 273 C:/Octave/java_files/moreclasses.jar
273 274
274 octave> % provoke an error 275 octave> % provoke an error
275 octave> disp(p@{2@}); 276 octave> disp (p@{2@});
276 error: A(I): Index exceeds matrix dimension. 277 error: A(I): Index exceeds matrix dimension.
277 @end example 278 @end example
278 279
279 Another way to add files to the @var{dynamic classpath} exclusively for your 280 Another way to add files to the @var{dynamic classpath} exclusively for your
280 user account is to use the file @file{.octaverc} which is stored in your home 281 user account is to use the file @file{.octaverc} which is stored in your home
281 directory. All Octave commands in this file are executed each time you start a 282 directory. All Octave commands in this file are executed each time you start a
282 new instance of Octave. The following example adds the directory @file{octave} 283 new instance of Octave. The following example adds the directory @file{octave}
283 to Octave's search path and the archive @file{myclasses.jar} in this directory 284 to Octave's search path and the archive @file{myclasses.jar} in this directory
284 to the Java search path. 285 to the Java search path.
285 286
286 @example 287 @example
287 @group 288 @group
288 % content of .octaverc: 289 % contents of .octaverc:
289 addpath('~/octave'); 290 addpath ('~/octave');
290 javaaddpath('~/octave/myclasses.jar'); 291 javaaddpath ('~/octave/myclasses.jar');
291 @end group 292 @end group
292 @end example 293 @end example
293 294
294 @c ------------------------------------------------------------------------ 295 @c ------------------------------------------------------------------------
295 @node How to create an instance of a Java class? 296 @node How to create an instance of a Java class?
299 @cindex instance, how to create 300 @cindex instance, how to create
300 @c - index - 301 @c - index -
301 302
302 The function @code{javaObject} can be used to create Java objects.. 303 The function @code{javaObject} can be used to create Java objects..
303 304
304 Example: 305 Example:
305 306
306 @example 307 @example
307 Passenger = javaObject ('package.FirstClass', row, seat); 308 Passenger = javaObject ('package.FirstClass', row, seat);
308 @end example 309 @end example
309 310
310 @c ------------------------------------------------------------------------ 311 @c ------------------------------------------------------------------------
311 @node How can I handle memory limitations? 312 @node How can I handle memory limitations?
312 @subsection How can I handle memory limitations? 313 @subsection How can I handle memory limitations?
313 @cindex memory, limitations 314 @cindex memory, limitations
314 315
315 In order to execute Java code Octave creates a Java Virtual Machine (JVM). 316 In order to execute Java code Octave creates a Java Virtual Machine (JVM).
316 Such a JVM allocates a fixed amount of initial memory and may expand this pool 317 Such a JVM allocates a fixed amount of initial memory and may expand this pool
317 up to a fixed maximum memory limit. The default values depend on the Java 318 up to a fixed maximum memory limit. The default values depend on the Java
318 version (see @ref{doc-javamem,,javamem}). The memory pool is shared by all Java 319 version (see @ref{doc-javamem,,javamem}). The memory pool is shared by all
319 objects running in the JVM@. This strict memory limit is intended mainly to 320 Java objects running in the JVM@. This strict memory limit is intended mainly
320 avoid that runaway applications inside web browsers or in enterprise servers 321 to avoid that runaway applications inside web browsers or in enterprise servers
321 can consume all memory and crash the system. When the maximum memory limit is 322 can consume all memory and crash the system. When the maximum memory limit is
322 hit, Java code will throw exceptions so that applications will fail or behave 323 hit, Java code will throw exceptions so that applications will fail or behave
323 unexpectedly. 324 unexpectedly.
324 325
325 You can specify options for the creation of the JVM inside a file named 326 You can specify options for the creation of the JVM inside a file named
326 @file{java.opts}. This is a text file where you can enter lines containing 327 @file{java.opts}. This is a text file where you can enter lines containing
327 @option{-X} and @option{-D} options handed to the JVM during initialization. 328 @option{-X} and @option{-D} options handed to the JVM during initialization.
328 329
329 The Java options file must be located in the directory where 330 The directory where the Java options file is located is specified by the
330 @file{javaclasspath.m} resides, usually something like 331 environment variable @w{@env{OCTAVE_JAVA_DIR}}. If unset the directory where
331 @file{@dots{}/share/Octave/<version>/m/java}. You can find this directory 332 @file{javaclasspath.m} resides is used instead (typically
332 by executing 333 @file{@env{OCTAVE_HOME}/share/octave/@env{OCTAVE_VERSION}/m/java/}). You can
334 find this directory by executing
333 335
334 @example 336 @example
335 which javaclasspath 337 which javaclasspath
336 @end example 338 @end example
337 339
338 The @option{-X} options allow you to increase the maximum amount of memory 340 The @option{-X} options allow you to increase the maximum amount of memory
339 available to the JVM to 256 Megabytes by adding the following line to the 341 available to the JVM@. The following example allows up to 256 Megabytes to
340 @file{java.opts} file: 342 be used by adding the following line to the @file{java.opts} file:
341 343
342 @example 344 @example
343 -Xmx256m 345 -Xmx256m
344 @end example 346 @end example
345 347
346 The maximum possible amount of memory depends on your system. On a Windows 348 The maximum possible amount of memory depends on your system. On a Windows
347 system with 2 Gigabytes main memory you should be able to set this maximum to 349 system with 2 Gigabytes main memory you should be able to set this maximum to
348 about 1 Gigabyte. 350 about 1 Gigabyte.
349 351
350 If your application requires a large amount of memory from the beginning, you 352 If your application requires a large amount of memory from the beginning, you
351 can also specify the initial amount of memory allocated to the JVM@. Adding 353 can also specify the initial amount of memory allocated to the JVM@. Adding
352 the following line to the @file{java.opts} file starts the JVM with 64 354 the following line to the @file{java.opts} file starts the JVM with 64
353 Megabytes of initial memory: 355 Megabytes of initial memory:
354 356
355 @example 357 @example
356 -Xms64m 358 -Xms64m
357 @end example 359 @end example
358 360
359 For more details on the available @option{-X} options of your Java Virtual 361 For more details on the available @option{-X} options of your Java Virtual
360 Machine issue the command @samp{java -X} at the operating system command prompt 362 Machine issue the command @samp{java -X} at the operating system command prompt
361 and consult the Java documentation. 363 and consult the Java documentation.
362 364
363
364 The @option{-D} options can be used to define system properties which can then 365 The @option{-D} options can be used to define system properties which can then
365 be used by Java classes inside Octave. System properties can be retrieved by 366 be used by Java classes inside Octave. System properties can be retrieved by
366 using the @code{getProperty()} methods of the @code{java.lang.System} class. 367 using the @code{getProperty()} methods of the @code{java.lang.System} class.
367 The following example line defines the property @var{MyProperty} and assigns it 368 The following example line defines the property @var{MyProperty} and assigns it
368 the string @code{12.34}. 369 the string @code{12.34}.
369 370
370 @example 371 @example
371 -DMyProperty=12.34 372 -DMyProperty=12.34
372 @end example 373 @end example
373 374
374 The value of this property can then be retrieved as a string by a Java object 375 The value of this property can then be retrieved as a string by a Java object
375 or in Octave: 376 or in Octave:
376 377
377 @example 378 @example
378 @group 379 @group
379 octave> javaMethod('getProperty', 'java.lang.System', 'MyProperty'); 380 octave> javaMethod ('getProperty', 'java.lang.System', 'MyProperty');
380 ans = 12.34 381 ans = 12.34
381 @end group 382 @end group
382 @end example 383 @end example
383 384
384 @seealso{javamem} 385 @seealso{javamem}
390 @cindex symbols, translation table 391 @cindex symbols, translation table
391 @cindex @TeX{} symbols, translation table 392 @cindex @TeX{} symbols, translation table
392 @cindex translation table for @TeX{} symbols 393 @cindex translation table for @TeX{} symbols
393 @c - index - 394 @c - index -
394 395
395 The dialog functions contain a translation table for @TeX{} like symbol codes. 396 The dialog functions contain a translation table for @TeX{} like symbol codes.
396 Thus messages and labels can be tailored to show some common mathematical 397 Thus messages and labels can be tailored to show some common mathematical
397 symbols or Greek characters. No further @TeX{} formatting codes are supported. 398 symbols or Greek characters. No further @TeX{} formatting codes are supported.
398 The characters are translated to their Unicode equivalent. However, not all 399 The characters are translated to their Unicode equivalent. However, not all
399 characters may be displayable on your system. This depends on the font used by 400 characters may be displayable on your system. This depends on the font used by
400 the Java system on your computer. 401 the Java system on your computer.
409 @need 5000 410 @need 5000
410 @c --------------------------------- 411 @c ---------------------------------
411 @ifhtml 412 @ifhtml
412 @float Table 413 @float Table
413 The table below shows each @TeX{} character code and the corresponding Unicode 414 The table below shows each @TeX{} character code and the corresponding Unicode
414 character: 415 character:
415 @multitable @columnfractions 0.18 0.1 0.05 0.18 0.1 0.05 0.18 0.1 416 @multitable @columnfractions 0.18 0.1 0.05 0.18 0.1 0.05 0.18 0.1
416 @item \alpha 417 @item \alpha
417 @tab 'α' 418 @tab 'α'
418 @tab 419 @tab
419 @tab \beta 420 @tab \beta
677 @end float 678 @end float
678 @end ifhtml 679 @end ifhtml
679 @c --------------------------------- 680 @c ---------------------------------
680 @iftex 681 @iftex
681 @float Table 682 @float Table
682 The table below shows each @TeX{} character code and the corresponding Unicode character: 683 The table below shows each @TeX{} character code and the corresponding Unicode
684 character:
683 @multitable @columnfractions 0.18 0.1 0.05 0.18 0.1 0.05 0.18 0.1 685 @multitable @columnfractions 0.18 0.1 0.05 0.18 0.1 0.05 0.18 0.1
684 @headitem @TeX{} code 686 @headitem @TeX{} code
685 @tab Symbol 687 @tab Symbol
686 @tab 688 @tab
687 @tab @TeX{} code 689 @tab @TeX{} code