A.4.1 Making Java Classes Available

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

Octave searches the static classpath first, and then the dynamic classpath. Classes appearing in the static classpath, 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 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:

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.


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

octave> # add two JAR archives 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});
octave> disp (p{2});

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

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