Java Interface
**************

The Java Interface is designed for calling Java functions from within Octave. If you want to do the reverse, and call Octave from within Java, try a library like javaOctave (http://kenai.com/projects/javaOctave) or joPas (http://jopas.sourceforge.net).

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.

(javaObject)

(javaArray)

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)

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

(java_get)

(java_set)

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

(javaMethod)

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)

(javaaddpath)

(javarmpath)

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

(usejava)

(javamem)

(java_matrix_autoconversion)

(java_unsigned_autoconversion)

(debug_java)

Dialog Box Functions
====================

The following functions all use the Java Interface to provide some form of dialog box.

(msgbox)

(errordlg)

(helpdlg)

(inputdlg)

(listdlg)

(questdlg)

(warndlg)

FAQ - Frequently asked Questions
================================

How to distinguish between Octave and Matlab?
---------------------------------------------

Octave and MATLAB are very similar, but handle Java slightly different. Therefore it may be necessary to detect the environment and use the appropriate functions.  The following function can be used to detect the environment.  Due to the persistent variable it can be called repeatedly without a heavy performance hit.

Example:

     %%
     %% Return: true if the environment is Octave.
     %%
     function retval = isOctave
       persistent cacheval;  % speeds up repeated calls

       if isempty (cacheval)
         cacheval = (exist ("OCTAVE_VERSION", "builtin") > 0);
       end

       retval = cacheval;
     end

How to make Java classes available to Octave?
---------------------------------------------

Java finds classes by searching a CLASSPATH.  This is a list of Java archive files and/or directories containing class files.  In Octave the CLASSPATH is composed of two parts:

  * the STATIC CLASSPATH is initialized once at startup of the JVM, and

  * the DYNAMIC CLASSPATH which can be modified at runtime.

Octave searches the STATIC CLASSPATH first, then the DYNAMIC CLASSPATH.  Classes appearing in the STATIC as well as in the DYNAMIC CLASSPATH will therefore be found in the STATIC CLASSPATH and loaded from this location.  Classes which will be used frequently or must be available to all users should be added to the STATIC CLASSPATH.  The STATIC CLASSPATH is populated once from the contents of a plain text file named javaclasspath.txt (or classpath.txt historically) when the Java Virtual Machine starts.  This file contains one line for each individual classpath to be added to the STATIC CLASSPATH.  These lines can identify single class files, directories containing class files, or Java archives with complete class file hierarchies.  Comment lines starting with a # or a % character are ignored.

The search rules for the file javaclasspath.txt (or classpath.txt) are:

  * First, Octave tries to locate it in the current directory (where Octave was started from).  If such a file is found, it is read and defines the initial STATIC CLASSPATH.  Thus, it is possible to define a static classpath on a 'per Octave invocation' basis.

  * Next, Octave searches in the user's home directory.  If a file javaclasspath.txt exists here, its contents are appended to the static classpath (if any).  Thus, it is possible to build an initial static classpath on a 'per user' basis.

  * Finally, Octave looks for a next occurrence of file javaclasspath.txt in the m-files directory where Octave Java functions live.  This is where javaclasspath.m resides, usually something like OCTAVE_HOME/share/octave/OCTAVE_VERSION/m/java/.  You can find this directory by executing the command

          which javaclasspath

     If this file exists here, its contents are also appended to the static classpath.  Note that the archives and class directories defined in this last step will affect all users.

Classes which are used only by a specific script should be placed in the DYNAMIC CLASSPATH.  This portion of the classpath can be modified at runtime using the javaaddpath and javarmpath functions.

Example:

     octave> base_path = "C:/Octave/java_files";

     octave> % add two JARchives to the dynamic classpath
     octave> javaaddpath ([base_path, "/someclasses.jar"]);
     octave> javaaddpath ([base_path, "/moreclasses.jar"]);

     octave> % check the dynamic classpath
     octave> p = javaclasspath;
     octave> disp (p{1});
     C:/Octave/java_files/someclasses.jar
     octave> disp (p{2});
     C:/Octave/java_files/moreclasses.jar

     octave> % remove the first element from the classpath
     octave> javarmpath ([base_path, "/someclasses.jar"]);
     octave> p = javaclasspath;
     octave> disp (p{1});
     C:/Octave/java_files/moreclasses.jar

     octave> % provoke an error
     octave> disp (p{2});
     error: A(I): Index exceeds matrix dimension.

Another way to add files to the DYNAMIC CLASSPATH exclusively for your user account is to use the file .octaverc which is stored in your home directory.  All Octave commands in this file are executed each time you start a new instance of Octave.  The following example adds the directory octave to Octave's search path and the archive myclasses.jar in this directory to the Java search path.

     % contents of .octaverc:
     addpath ("~/octave");
     javaaddpath ("~/octave/myclasses.jar");

How to create an instance of a Java class?
------------------------------------------

The function javaObject can be used to create Java objects..

Example:

     Passenger = javaObject ("package.FirstClass", row, seat);

How can I handle memory limitations?
------------------------------------

In order to execute Java code Octave creates a Java Virtual Machine (JVM). Such a JVM allocates a fixed amount of initial memory and may expand this pool up to a fixed maximum memory limit.  The default values depend on the Java version (see javamem).  The memory pool is shared by all Java objects running in the JVM.  This strict memory limit is intended mainly to avoid that runaway applications inside web browsers or in enterprise servers can consume all memory and crash the system.  When the maximum memory limit is hit, Java code will throw exceptions so that applications will fail or behave unexpectedly.

You can specify options for the creation of the JVM inside a file named java.opts.  This is a text file where you can enter lines containing -X and -D options handed to the JVM during initialization.

The directory where the Java options file is located is specified by the environment variable OCTAVE_JAVA_DIR.  If unset the directory where javaclasspath.m resides is used instead (typically OCTAVE_HOME/share/octave/OCTAVE_VERSION/m/java/).  You can find this directory by executing

     which javaclasspath

The -X options allow you to increase the maximum amount of memory available to the JVM.  The following example allows up to 256 Megabytes to be used by adding the following line to the java.opts file:

     -Xmx256m

The maximum possible amount of memory depends on your system.  On a Windows system with 2 Gigabytes main memory you should be able to set this maximum to about 1 Gigabyte.

If your application requires a large amount of memory from the beginning, you can also specify the initial amount of memory allocated to the JVM.  Adding the following line to the java.opts file starts the JVM with 64 Megabytes of initial memory:

     -Xms64m

For more details on the available -X options of your Java Virtual Machine issue the command java -X at the operating system command prompt and consult the Java documentation.

The -D options can be used to define system properties which can then be used by Java classes inside Octave.  System properties can be retrieved by using the getProperty() methods of the java.lang.System class. The following example line defines the property MYPROPERTY and assigns it the string 12.34.

     -DMyProperty=12.34

The value of this property can then be retrieved as a string by a Java object or in Octave:

     octave> javaMethod ("getProperty", "java.lang.System", "MyProperty");
     ans = 12.34

javamem

Which TeX symbols are implemented in dialog functions?
------------------------------------------------------

The dialog functions contain a translation table for TeX like symbol codes. Thus messages and labels can be tailored to show some common mathematical symbols or Greek characters.  No further TeX formatting codes are supported. The characters are translated to their Unicode equivalent.  However, not all characters may be displayable on your system.  This depends on the font used by the Java system on your computer.

Each TeX symbol code must be terminated by a space character to make it distinguishable from the surrounding text.  Therefore the string \alpha =12.0 will produce the desired result, whereas \alpha=12.0 would produce the literal text '\ALPHA=12.0'.

errordlg, helpdlg, inputdlg, listdlg, msgbox, questdlg, warndlg