Next: , Up: Java Interface   [Contents][Index]


37.1 Java Interface Functions

The following functions are the core of the Java Interface. They provide a way to create a Java object, get and set its data fields, and call Java methods which return results to Octave.

Built-in Function: jobj = javaObject (classname)
Built-in Function: jobj = javaObject (classname, arg1, …)

Create a Java object of class classsname, by calling the class constructor with the arguments arg1, …

The first example below creates an uninitialized object, while the second example supplies an initial argument to the constructor.

x = javaObject ("java.lang.StringBuffer")
x = javaObject ("java.lang.StringBuffer", "Initial string")

See also: javaMethod, javaArray.

FIXME: Need documentation on how fieldnames() is overloaded to return the methods of a Java object.

FIXME: Need documentation on how to use structure-like indexing to get fields from Java object.

FIXME: Need documentation on how to use structure-like indexing to set fields from Java object.

Built-in Function: isjava (x)

Return true if x is a Java object.

See also: class, typeinfo, isa, javaObject.

Function File: jary = javaArray (classname, sz)
Function File: jary = javaArray (classname, m, n, …)

Create a Java array of size sz with elements of class classname. classname may be a Java object representing a class or a string containing the fully qualified class name. The size of the object may also be specified with individual integer arguments m, n, etc.

The generated array is uninitialized. All elements are set to null if classname is a reference type, or to a default value (usually 0) if classname is a primitive type.

Sample code:

jary = javaArray ("java.lang.String", 2, 2);
jary(1,1) = "Hello";

See also: javaObject.

Built-in Function: ret = javaMethod (methodname, obj)
Built-in Function: ret = javaMethod (methodname, obj, arg1, …)

Invoke the method methodname on the Java object obj with the arguments arg1, … For static methods, obj can be a string representing the fully qualified name of the corresponding class. The function returns the result of the method invocation.

When obj is a regular Java object, structure-like indexing can be used as a shortcut syntax. For instance, the two following statements are equivalent

  ret = javaMethod ("method1", x, 1.0, "a string")
  ret = x.method1 (1.0, "a string")

See also: methods, javaObject.

FIXME: Need documentation on how methods() is overloaded to return the methods of a Java object.

The following three functions are used to display and modify the class path used by the Java Virtual Machine. This is entirely separate from Octave’s PATH variable and is used by the JVM to find the correct code to execute.

Function File: javaclasspath ()
Function File: dpath = javaclasspath ()
Function File: [dpath, spath] = javaclasspath ()
Function File: clspath = javaclasspath (what)

Return the class path of the Java virtual machine in the form of a cell array of strings.

If called with no inputs:

If called with a single input parameter what:

"-dynamic"

Return the dynamic classpath.

"-static"

Return the static classpath.

"-all"

Return both the static and dynamic classpath in a single cellstr.

See also: javaaddpath, javarmpath.

Function File: javaaddpath (clspath)
Function File: javaaddpath (clspath1, …)

Add clspath to the dynamic class path of the Java virtual machine. clspath may either be a directory where .class files are found, or a .jar file containing Java classes. Multiple paths may be added at once by specifying additional arguments.

See also: javarmpath, javaclasspath.

Function File: javarmpath (clspath)
Function File: javarmpath (clspath1, …)

Remove clspath from the dynamic class path of the Java virtual machine. clspath may either be a directory where .class files are found, or a .jar file containing Java classes. Multiple paths may be removed at once by specifying additional arguments.

See also: javaaddpath, javaclasspath.

The following four functions provide information and control over the interface between Octave and the Java Virtual Machine.

Function File: usejava (feature)

Return true if the Java element feature is available.

Possible features are:

"awt"

Abstract Window Toolkit for GUIs.

"desktop"

Interactive desktop is running.

"jvm"

Java Virtual Machine.

"swing"

Swing components for lightweight GUIs.

usejava determines if specific Java features are available in an Octave session. This function is provided for scripts which may alter their behavior based on the availability of Java. The feature "desktop" always returns false as Octave has no Java-based desktop. Other features may be available if Octave was compiled with the Java Interface and Java is installed.

Function File: javamem ()
Function File: jmem = javamem ()

Show the current memory usage of the Java virtual machine (JVM) and run the garbage collector.

When no return argument is given the info is printed to the screen. Otherwise, the output cell array jmem contains Maximum, Total, and Free memory (in bytes).

All Java-based routines are run in the JVM’s shared memory pool, a dedicated and separate part of memory claimed by the JVM from your computer’s total memory (which comprises physical RAM and virtual memory / swap space on hard disk).

The maximum allowable memory usage can be configured using the file java.opts. The directory where this file resides is determined by the environment variable OCTAVE_JAVA_DIR. If unset, the directory where javaaddpath.m resides is used instead (typically OCTAVE_HOME/share/octave/OCTAVE_VERSION/m/java/

java.opts is a plain text file with one option per line. The default initial memory size and default maximum memory size (which are both system dependent) can be overridden like so:

-Xms64m

-Xmx512m

(in megabytes in this example). You can adapt these values to your own requirements if your system has limited available physical memory or if you get Java memory errors.

"Total memory" is what the operating system has currently assigned to the JVM and depends on actual and active memory usage. "Free memory" is self-explanatory. During operation of Java-based Octave functions the amount of Total and Free memory will vary, due to Java’s own cleaning up and your operating system’s memory management.

Built-in Function: val = java_matrix_autoconversion ()
Built-in Function: old_val = java_matrix_autoconversion (new_val)
Built-in Function: java_matrix_autoconversion (new_val, "local")

Query or set the internal variable that controls whether Java arrays are automatically converted to Octave matrices. The default value is false.

When called from inside a function with the "local" option, the variable is changed locally for the function and any subroutines it calls. The original variable value is restored when exiting the function.

See also: java_unsigned_autoconversion, debug_java.

Built-in Function: val = java_unsigned_autoconversion ()
Built-in Function: old_val = java_unsigned_autoconversion (new_val)
Built-in Function: java_unsigned_autoconversion (new_val, "local")

Query or set the internal variable that controls how integer classes are converted when java_matrix_autoconversion is enabled. When enabled, Java arrays of class Byte or Integer are converted to matrices of class uint8 or uint32 respectively. The default value is true.

When called from inside a function with the "local" option, the variable is changed locally for the function and any subroutines it calls. The original variable value is restored when exiting the function.

See also: java_matrix_autoconversion, debug_java.

Built-in Function: val = debug_java ()
Built-in Function: old_val = debug_java (new_val)
Built-in Function: debug_java (new_val, "local")

Query or set the internal variable that determines whether extra debugging information regarding the initialization of the JVM and any Java exceptions is printed.

When called from inside a function with the "local" option, the variable is changed locally for the function and any subroutines it calls. The original variable value is restored when exiting the function.

See also: java_matrix_autoconversion, java_unsigned_autoconversion.


Next: , Up: Java Interface   [Contents][Index]