Java package: Difference between revisions

523 bytes added ,  7 December 2011
using templates
(using <pre> blocks for code blocks in lists)
(using templates)
Line 1: Line 1:
Octave is an easy to use but powerful environment for mathematical calculations, which can easily be extended by packages. Its features are close to the commercial tool Matlab so that it can often be used as a replacement. Java on the other hand offers a rich, object oriented and platform independent  environment for many applications. The core Java classes can be easily extended by many freely available libraries. This document refers to the package <code>java</code>, which is part of the GNU Octave project. This package allows you to access Java classes from inside Octave. Thus it is possible to use existing class files or complete Java libraries directly from Octave.
Octave is an easy to use but powerful environment for mathematical calculations, which can easily be extended by packages. Its features are close to the commercial tool Matlab so that it can often be used as a replacement. Java on the other hand offers a rich, object oriented and platform independent  environment for many applications. The core Java classes can be easily extended by many freely available libraries. This document refers to the package <code>java</code>, which is part of the GNU Octave project. This package allows you to access Java classes from inside Octave. Thus it is possible to use existing class files or complete Java libraries directly from Octave.


This description is based on the Octave package <code>java-1.2.8</code>. The <code>java</code> package usually installs its script files (.m) in the directory <code>.../share/Octave/packages/java-1.2.8</code> and its binary (.oct) files in <code>.../libexec/Octave/packages/java-1.2.8</code>. You can get help on specific functions in Octave by executing the help command
This description is based on the Octave package {{Codeline|java-1.2.8}}. The {{Forge|java}} package usually installs its script files (.m) in the directory {{Path|.../share/Octave/packages/java-1.2.8}} and its binary (.oct) files in {{Path|.../libexec/Octave/packages/java-1.2.8}}. You can get help on specific functions in Octave by executing the help command
with the name of a function from this package:
with the name of a function from this package:
  octave> help javaObject
  octave> help javaObject
Line 8: Line 8:
  octave> doc java
  octave> doc java


Note on calling Octave from Java: the <code>java</code> package is designed for calling Java from Octave. If you want to call Octave from Java, you might want to use a library like <code>javaOctave</code> [http://kenai.com/projects/javaOctave] or <code>joPas</code> [http://jopas.sourceforge.net].  
Note on calling Octave from Java: the java package is designed for calling Java from Octave. If you want to call Octave from Java, you might want to use a library like [http://kenai.com/projects/javaOctave javaOctave] or [http://jopas.sourceforge.net joPas].  


=FAQ=
=FAQ=
Line 15: Line 15:


==How to make Java classes available to Octave?==
==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:
Java finds classes by searching a {{Codeline|classpath}}. This is a list of Java archive files and/or directories containing class files. In Octave and Matlab the {{Codeline|classpath}} is composed of two parts:
*the static ''classpath'' is initialized once at startup of the JVM, and;
*the static {{Codeline|classpath}} is initialized once at startup of the JVM, and;
*the dynamic ''classpath'' which can be modified at runtime.
*the dynamic {{Codeline|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''
Octave searches the static {{Codeline|classpath}} first, then the dynamic {{Codeline|classpath}}. Classes appearing in the static as well as in the dynamic {{Codeline|classpath}} will therefore be found in the static {{Codeline|classpath}} and loaded from this location.
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.
Classes which shall be used regularly or must be available to all users should be added to the static {{Codeline|classpath}}. The static {{Codeline|classpath}} is populated once from the contents of a plain text file named {{Path|classpath.txt}} when the Java Virtual Machine starts. This file contains one line for each individual {{Codeline|classpath}} to be added to the static {{Codeline|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 {{Codeline|#}} or a {{Codeline|%}} character are ignored.


The search rules for the file classpath.txt are:
The search rules for the file {{Path|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.
*First, Octave searches for the file {{Path|classpath.txt}} in your home directory, If such a file is found, it is read and defines the initial static {{Codeline|classpath}}. Thus it is possible to build an initial static {{Codeline|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
*Next, Octave looks for another file {{Path|classpath.txt}} in the package installation directory. This is where {{Path|javaclasspath.m}} resides, usually something like
:<pre>...\share\Octave\packages\java-1.2.8.</pre>
:<pre>...\share\Octave\packages\java-1.2.8.</pre>
: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.
:you can find this directory by executing the command {{Codeline|pkg list}}. If this file exists, its contents is also appended to the static {{Codeline|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:
Classes which are used only by a specific script should be placed in the dynamic {{Codeline|classpath}}. This portion of the {{Codeline|classpath}} can be modified at runtime using the {{Codeline|javaaddpath}} and {{Codeline|javarmpath}} functions. Example:
  octave> base_path = "C:/Octave/java_files";
  octave> base_path = "C:/Octave/java_files";
  octave> % add two JARchives to the dynamic classpath
  octave> % add two JARchives to the dynamic classpath
Line 50: Line 49:
   error: A(I): Index exceeds matrix dimension.
   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.
Another way to add files to the dynamic {{Codeline|classpath}} exclusively for your user account is to use the file {{Path|.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 {{Path|~/octave}} to Octave’s search path and the archive {{Path|myclasses.jar}} in this directory to the Java search path.


% content of .octaverc:
{{File|octaverc|<pre>
addpath ("~/octave");
addpath ("~/octave");
javaaddpath ("~/octave/myclasses.jar");
javaaddpath ("~/octave/myclasses.jar");</pre>}}


== How to create an instance of a Java class? ==
== 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. Using [[Compatibility#Are_we_running_octave.3F|'''is_octave()''']] to distinguish between environments
If your code shall work under Octave as well as Matlab you should use the function {{Codeline|javaObject}} to create Java objects. The function {{Codeline|java_new}} is Octave specific and does not exist in the Matlab environment. Using [[Compatibility#Are_we_running_octave.3F|{{Codeline|is_octave()}}]] to distinguish between environments


  if (is_octave)
  if (is_octave)
Line 66: Line 65:


==How can I handle memory limitations?==
==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 '''help 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 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 {{Codeline|help 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 as well as in Matlab, you can specify options for the creation of the JVM inside a file named {{Path|java.opts}}. This is a text file where you can enter lines containing {{Codeline|-X}} and {{Codeline|-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:
In Octave, the Java options file must be located in the directory where {{Path|javaclasspath.m}} resides, i.e. the package installation directory, usually something like {{Path|...\share\Octave\packages\java-1.2.8}}. You can find this directory by executing {{Codeline|pkg list}}.
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:
In Matlab, the options file goes into the {{Path|MATLABROOT/bin/ARCH}} directory or in your personal Matlab startup directory (can be determined by a {{Codeline|pwd}} command). MATLABROOT is the Matlab root directory and ARCH is your system architecture, which you find by issuing the commands {{Codeline|matlabroot}} respectively {{Codeline|computer('arch')}}.
-Xmx256m
 
The {{Codeline|-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:{{File|java.opts|<pre>-Xmx256m</pre>}}


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.
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:
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 {{Path|java.opts}} file starts the JVM with 64 Megabytes of initial memory:{{File|java.opts|<pre>-Xms64m</pre>}}
-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.
For more details on the available {{Codeline|-X}} options of your Java Virtual Machine issue the command {{Codeline|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
The {{Codeline|-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()'''
classes inside Octave. System properties can be retrieved by using the {{Codeline|getProperty()}}
methods of the '''java.lang.System''' class. The following example line defines the property
methods of the {{Codeline|java.lang.System}} class. The following example line defines the property
''MyProperty'' and assigns it the string ''12.34''.
{{Codeline|MyProperty}} and assigns it the string {{Codeline|12.34}}.
  -DMyProperty=12.34
  -DMyProperty=12.34
The value of this property can then be retrieved as a string by a Java object or in Octave:
The value of this property can then be retrieved as a string by a Java object or in Octave:
Line 95: Line 92:
==How to install the java package in Octave?==
==How to install the java package in Octave?==
===Uninstall the currently installed package java===
===Uninstall the currently installed package java===
Check whether the java package is already installed by issuing the ''pkg list'' command:
Check whether the java package is already installed by issuing the {{Codeline|pkg list}} command:
  octave> pkg list
  octave> pkg list
   Package Name | Version | Installation directory
   Package Name | Version | Installation directory
Line 108: Line 105:


===Make sure that the build environment is configured properly===
===Make sure that the build environment is configured properly===
The installation process requires that the environment variable '''JAVA_HOME''' points to the
The installation process requires that the environment variable {{Codeline|JAVA_HOME}} points to the
Java Development Kit (JDK) on your computer.
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.
*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:
*Do not use backslashes but ordinary slashes in the path. Set the environment variable {{Codeline|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:
:<pre>octave> setenv ("JAVA_HOME", "C:/Java/jdk1.6.0_21");</pre>
:<pre>octave> setenv ("JAVA_HOME", "C:/Java/jdk1.6.0_21");</pre>
:If you are using a Linux system this would look probably more like:
:If you are using a Linux system this would look probably more like:
:<pre>octave> setenv ("JAVA_HOME", "/usr/local/jdk1.6.0_21");</pre>
:<pre>octave> setenv ("JAVA_HOME", "/usr/local/jdk1.6.0_21");</pre>
:Note, that on all systems you must use the forward slash '''/''' as the separator, not the backslash '''\'''. If on a Windows system the environment variable '''JAVA_HOME''' is already defined using the backslash, you can easily change this by issuing the following Octave command before starting the installation:
:Note, that on all systems you must use the forward slash {{Codeline|/}} as the separator, not the backslash {{Codeline|\}}. If on a Windows system the environment variable {{Codeline|JAVA_HOME}} is already defined using the backslash, you can easily change this by issuing the following Octave command before starting the installation:
:<pre>octave> setenv ("JAVA_HOME", strrep (getenv ("JAVA_HOME"), '\', '/'))</pre>
:<pre>octave> setenv ("JAVA_HOME", strrep (getenv ("JAVA_HOME"), '\', '/'))</pre>


===Compile and install the package in Octave===
===Compile and install the package in Octave===
[[Octave_Forge#installing_packages|Install the package]] downloaded from octave-forge.
[[Octave_Forge#installing_packages|Install the package]] from octave-forge.


===Test the java package installation===
===Test the java package installation===
Line 131: Line 128:


This "auto boxing" scheme seems to be implemented for the following Java classes:
This "auto boxing" scheme seems to be implemented for the following Java classes:
*java.lang.Integer
*{{Codeline|java.lang.Integer}}
*java.lang.Double
*{{Codeline|java.lang.Double}}
*java.lang.Boolean
*{{Codeline|java.lang.Boolean}}
*java.lang.String
*{{Codeline|java.lang.String}}


If you instead create an object for which no "auto-boxing" is implemented, '''javaObject'''
If you instead create an object for which no "auto-boxing" is implemented, {{Codeline|javaObject}}
returns the genuine Java object:
returns the genuine Java object:
  octave> v = javaObject ("java.util.Vector")
  octave> v = javaObject ("java.util.Vector")
Line 162: Line 159:


Each TEX symbol code must be terminated by a space character to make it distinguishable from
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
the surrounding text. Therefore the string {{Codeline|\alpha =12.0}} will produce the
desired result, whereas "\alpha=12.0" would produce the literal text "\alpha=12.0".
desired result, whereas {{Codeline|\alpha=12.0}} would produce the literal text {{Codeline|\alpha=12.0}}.


[[Category:OctaveForge]]
[[Category:OctaveForge]]
[[Category:Packages]]
[[Category:Packages]]