octave: Java Interface Functions
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")
DONTPRINTYET See also: javaMethod XREFjavaMethod, *notejavaArray:
DONTPRINTYET See also: javaMethod XREFjavaMethod, javaArray
XREFjavaArray.
-- : 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 XREFjavaObject.
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 XREFclass, typeinfo XREFtypeinfo,
isa XREFisa, javaObject XREFjavaObject.
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
DONTPRINTYET See also: java_set XREFjava_set, *notejavaMethod:
DONTPRINTYET See also: java_set XREFjava_set, javaMethod
XREFjavaMethod, javaObject XREFjavaObject.
-- : 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
DONTPRINTYET See also: java_get XREFjava_get, *notejavaMethod:
DONTPRINTYET See also: java_get XREFjava_get, javaMethod
XREFjavaMethod, javaObject XREFjavaObject.
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.
DONTPRINTYET See also: methods XREFmethods, *notejavaObject:
DONTPRINTYET See also: methods XREFmethods, javaObject
XREFjavaObject.
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 no output is requested, the dynamic and static classpaths
are printed to the standard output.
• If one output value DPATH is requested, the result is the
dynamic classpath.
• If two output valuesDPATH and SPATH are requested, the first
variable will contain the dynamic classpath and the second
will contain the static classpath.
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.
DONTPRINTYET See also: javaaddpath XREFjavaaddpath, *notejavarmpath:
DONTPRINTYET See also: javaaddpath XREFjavaaddpath, javarmpath
XREFjavarmpath.
-- : 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.
DONTPRINTYET See also: javarmpath XREFjavarmpath, *notejavaclasspath:
DONTPRINTYET See also: javarmpath XREFjavarmpath, javaclasspath
XREFjavaclasspath.
-- : 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.
DONTPRINTYET See also: javaaddpath XREFjavaaddpath, *notejavaclasspath:
DONTPRINTYET See also: javaaddpath XREFjavaaddpath, javaclasspath
XREFjavaclasspath.
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
• no output argument is requested:
Return an empty string
• an output argument is requested:
Return a struct with fields "feature" and "identifier" both
empty
If FEATURE is not supported and
• no output argument is requested:
Emit an error message
• an output argument is requested:
Return a struct with field "feature" set to FEATURE and field
"identifier" set to COMPONENT
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 XREFusejava, error XREFerror.
-- : 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 XREFjavachk.
-- : 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
XREFjava_unsigned_autoconversion, debug_java XREFdebug_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
XREFjava_matrix_autoconversion, debug_java XREFdebug_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.
DONTPRINTYET See also: java_matrix_autoconversion
XREFjava_matrix_autoconversion, *notejava_unsigned_autoconversion:
DONTPRINTYET See also: java_matrix_autoconversion
XREFjava_matrix_autoconversion, java_unsigned_autoconversion
XREFjava_unsigned_autoconversion.