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


A.4.4 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.

: jobj = javaObject (classname)
: 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.

: jary = javaArray (classname, sz)
: 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.

There are many different variable types in Octave, but only ones created through javaObject can use Java functions. Before using Java with an unknown object the type can be checked with isjava.

: isjava (x)

Return true if x is a Java object.

See also: class, typeinfo, isa, javaObject.

Once an object has been created it is natural to find out what fields the object has, and to read (get) and write (set) them.

In Octave the fieldnames function for structures has been overloaded to return the fields of a Java object. For example:

dobj = javaObject ("java.lang.Double", pi);
fieldnames (dobj)
⇒
{
  [1,1] = public static final double java.lang.Double.POSITIVE_INFINITY
  [1,2] = public static final double java.lang.Double.NEGATIVE_INFINITY
  [1,3] = public static final double java.lang.Double.NaN
  [1,4] = public static final double java.lang.Double.MAX_VALUE
  [1,5] = public static final double java.lang.Double.MIN_NORMAL
  [1,6] = public static final double java.lang.Double.MIN_VALUE
  [1,7] = public static final int java.lang.Double.MAX_EXPONENT
  [1,8] = public static final int java.lang.Double.MIN_EXPONENT
  [1,9] = public static final int java.lang.Double.SIZE
  [1,10] = public static final java.lang.Class java.lang.Double.TYPE
}

The analogy of objects with structures is carried over into reading and writing object fields. To read a field the object is indexed with the ‘.’ operator from structures. This is the preferred method for reading fields, but Octave also provides a function interface to read fields with java_get. An example of both styles is shown below.

dobj = javaObject ("java.lang.Double", pi);
dobj.MAX_VALUE
⇒  1.7977e+308
java_get ("java.lang.Float", "MAX_VALUE")
⇒  3.4028e+38
: val = java_get (obj, name)

Get the value of the field name of the Java object obj.

For static fields, obj can be a string representing the fully qualified name of the corresponding class.

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

  java_get (x, "field1")
  x.field1

See also: java_set, javaMethod, javaObject.

: obj = java_set (obj, name, val)

Set the value of the field name of the Java object obj to val.

For static fields, obj can be a string representing the fully qualified named of the corresponding Java class.

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

  java_set (x, "field1", val)
  x.field1 = val

See also: java_get, javaMethod, javaObject.

To see what functions can be called with an object use methods. For example, using the previously created dobj:

methods (dobj)
⇒
Methods for class java.lang.Double:
boolean equals(java.lang.Object)
java.lang.String toString(double)
java.lang.String toString()
…

To call a method of an object the same structure indexing operator ‘.’ is used. Octave also provides a functional interface to calling the methods of an object through javaMethod. An example showing both styles is shown below.

dobj = javaObject ("java.lang.Double", pi);
dobj.equals (3)
⇒  0
javaMethod ("equals", dobj, pi)
⇒  1
: ret = javaMethod (methodname, obj)
: 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.

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")

javaMethod returns the result of the method invocation.

See also: methods, javaObject.

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.

: javaclasspath ()
: dpath = javaclasspath ()
: [dpath, spath] = javaclasspath ()
: 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.

: javaaddpath (clspath)
: 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.

: javarmpath (clspath)
: 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 functions provide information and control over the interface between Octave and the Java Virtual Machine.

: javachk (feature)
: javachk (feature, component)
: msg = javachk (…)

Check for the presence of the Java feature in the current session and print or return an error message if it is not.

Possible features are:

"awt"

Abstract Window Toolkit for GUIs.

"desktop"

Interactive desktop is running.

"jvm"

Java Virtual Machine.

"swing"

Swing components for lightweight GUIs.

If feature is supported and

If feature is not supported and

The optional input component will be used in place of feature in any error messages for greater specificity.

javachk 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" is never available as Octave has no Java-based desktop. Other features may be available if Octave was compiled with the Java Interface and Java is installed.

See also: usejava, error.

: 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.

See also: javachk.

: javamem ()
: 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.

: val = java_matrix_autoconversion ()
: old_val = java_matrix_autoconversion (new_val)
: 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.

: val = java_unsigned_autoconversion ()
: old_val = java_unsigned_autoconversion (new_val)
: 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.

: val = debug_java ()
: old_val = debug_java (new_val)
: 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.


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