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)

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.

(isjava)

(javaArray)

(javaMethod)

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.

(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 ret = isOctave
       persistent retval;  % speeds up repeated calls

       if isempty(retval)
         retval = (exist('Octave_VERSION','builtin') > 0);
       end

       ret = retval;
     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 and MATLAB 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 shall be used regularly 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 classpath.txt 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 classpath.txt are:

  * First, Octave searches for the file classpath.txt in your home directory, If such a file is found, it is read and defines the initial STATIC CLASSPATH.  Thus it is possible to build an initial static classpath on a 'per user' basis.

  * Next, Octave looks for another file classpath.txt in the package installation directory.  This is where javaclasspath.m resides, usually something like ...\share\Octave\packages\java-1.2.8.  You can find this directory by executing the command

          pkg list

     If this file exists, its contents is also appended to the static classpath. Note that the archives and class directories defined in this file 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.

     % content of .octaverc:
     addpath('~/octave');
     javaaddpath('~/octave/myclasses.jar');

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

If your code shall work under Octave as well as MATLAB you should use the function javaObject to create Java objects.  The function java_new is Octave specific and does not exist in the MATLAB environment.

Example 1, suitable for Octave but not for MATLAB:

        Passenger = java_new('package.FirstClass', row, seat);

Example 2, which works in Octave as well as in MATLAB:

        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.

In Octave as well as in MATLAB, 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.

In Octave, the Java options file must be located in the directory where javaclasspath.m resides, i.e., the package installation directory, usually something like ...\share\Octave\packages\java-1.2.8.  You can find this directory by executing

     pkg list

In MATLAB, the options file goes into the MATLABROOT/bin/ARCH directory or in your personal MATLAB startup directory (can be determined by a pwd command).  MATLABROOT is the MATLAB root directory and ARCH is your system architecture, which you find by issuing the commands matlabroot respectively computer('arch').

The -X options allow you to increase the maximum amount of memory available to the JVM to 256 Megabytes 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('java.lang.System', 'getProperty', 'MyProperty');
     ans = 12.34

javamem

How to compile the java package in Octave?
------------------------------------------

Most Octave installations come with the JAVA package pre-installed.  In case you want to replace this package with a more recent version, you must perform the following steps:

Uninstall the currently installed package JAVA
..............................................

Check whether the JAVA package is already installed by issuing the pkg list command:

     octave> pkg list
     Package Name  | Version | Installation directory
     --------------+---------+-----------------------
             java *|   1.2.8 | /home/octavio/octave/java-1.2.8
     octave>

If the JAVA package appears in the list you must uninstall it first by issuing the command

     octave> pkg uninstall java
     octave> pkg list

Now the java package should not be listed anymore.  If you have used the JAVA package during the current session of Octave, you have to exit and restart Octave before you can uninstall the package.  This is because the system keeps certain libraries in memory after they have been loaded once.

Make sure that the build environment is configured properly
...........................................................

The installation process requires that the environment variable JAVA_HOME points to the Java Development Kit (JDK) on your computer.

  * Note that JDK is not equal to JRE (Java Runtime Environment).  The JDK home directory contains subdirectories with include, library and executable files which are required to compile the JAVA package.  These files are not part of the JRE, so you definitely need the JDK.

  * Do not use backslashes but ordinary slashes in the path.

Set the environment variable JAVA_HOME according to your local JDK installation.  Please adapt the path in the following examples according to the JDK installation on your system.  If you are using a Windows system that might be:

     octave> setenv("JAVA_HOME","C:/Java/jdk1.6.0_21");

Note, that on both system types, Windows as well as Linux, you must use the forward slash '/' as the separator, not the backslash '\'.

If you are using a Linux system this would look probably more like:

     octave> setenv("JAVA_HOME","/usr/local/jdk1.6.0_21");

Compile and install the package in Octave
.........................................

If you have for example saved the package archive on your z: drive the command would be:

     Octave> pkg install -verbose z:/java-1.2.8.tar.gz

or if you have Linux and the package file is stored in your home directory:

     octave> pkg install -verbose ~/java-1.2.8.tar.gz

The option -verbose will produce some lengthy output, which should not show any errors (maybe a few warnings at best).

You can then produce a list of all installed packages:

     octave> pkg list

This list of packages should now include the package JAVA:

     octave> pkg list
     Package Name  | Version | Installation directory
     --------------+---------+-----------------------
             java *|   1.2.8 | /home/octavio/octave/java-1.2.8
     octave>

Test the java package installation
..................................

The following code creates a Java string object, which however is automatically converted to an Octave string:

     octave> s = javaObject('java.lang.String', 'Hello OctaveString')
     s = Hello OctaveString

Note that the java package automatically transforms the Java String object to an Octave string.  This means that you cannot apply Java String methods to the result.

This "auto boxing" scheme seems to be implemented for the following Java classes:

  * java.lang.Integer

  * java.lang.Double

  * java.lang.Boolean

  * java.lang.String

If you instead create an object for which no "auto-boxing" is implemented, javaObject returns the genuine Java object:

     octave> v = javaObject('java.util.Vector')
     v =
     <Java object: java.util.Vector>
     octave> v.add(12);
     octave> v.get(0)
     ans = 12

If you have created such a Java object, you can apply all methods of the Java class to the returned object.  Note also that for some objects you must specify an initializer:

     % not:
     octave> d = javaObject('java.lang.Double')
     error: [java] java.lang.NoSuchMethodException: java.lang.Double
     % but:
     octave> d = javaObject('java.lang.Double',12.34)
     d = 12.340

Which TeX symbols are implemented in the 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