I have a java class, and I need to call it from matlab to implement a document. is that you have an idea?
MATLAB: Calling a java class from matlab
java classMATLAB
Related Solutions
Here is an example. This assumes you already have knowledge of Java.
The following is a Java program:
public class HelloWorld { public static void main( String args[] ) { System.out.println( "Hello World!" ); }}
To call this program in MATLAB:
0. Make sure you have the version of JRE/JDK on your system that is supported with your version of MATLAB. You can get this version by executing this on the MATLAB command prompt:
>> version -java
1. Outside of MATLAB: use Java to compile this class, so you have file HelloWorld.class
2. Locate the classpath.txt file for the MATLAB installation. The location of this file can be found by typing the following command in MATLAB command window:
which classpath.txt
3. Open the 'classpath.txt' with a text editor as Administrator. Add the full path for the directory with the HelloWorld.class to the end of the 'classpath.txt' file as a single line and save the file.
4. Restart MATLAB.
5. In MATLAB: to create an object of class HelloWorld, type:
o = HelloWorld
6. In MATLAB: to execute main() of object o, type:
javaMethod('main', o, '')
Alternately one may also add the directory in which the class files are to the dynamic path. Use the JAVAADDPATH command to add the directory (which contains the HelloWorld.class file) to JAVA's dynamic classpath. This also obviates the need to restart MATLAB. Once this is done the code can be invoked as follows:
o = HelloWorld;javaMethod('main', o);
For more information on calling Java class from MATLAB, see chapter 3 of the "External Interfaces" documentation. This documentation can be accessed from the following URL:
This enhancement has been incorporated in Release 2006a (R2006a). For previous product releases, read below for any possible workarounds:
The following example demonstrates how a Java class from a JAR file can be used in MATLAB. It assumes you already have some knowledge of Java.
Three files are included with this example. They are:
1. HelloWorld.java - Java source code for HelloWorld.class.2. HelloWorld.class - Compiled HelloWorld.class.3. myHelloArchive.jar - JAR archive containing HelloWorld.class.
The first two files are included for reference. The third file, myHelloArchive.jar, is the JAR file containing the HelloWorld.class Java class, with which this example is concerned.
Step 1. Download the attached example file(s).
This example assumes that you will place myHelloArchive.jar in your $MATLAB\work directory (where the term $MATLAB represents the MATLAB root directory on your machine).
The other files, HelloWorld.java and HelloWorld.class, are also available for your reference.
Step 2. Add the JAR archive file to your Java class path.
When working with JAR archive files, the full name of the JAR file must be specified explicitly in the Java class path.
There are two parts to the Java class path: a static part that is loaded from a file at the start of each MATLAB session, and a dynamic part that can be loaded and modified during the MATLAB session through function calls.
The static Java class path is loaded from the following file:
$MATLAB\toolbox\local\classpath.txt
To add our myHelloArchive.jar to your static Java path, edit classpath.txt by adding the following line to the top of the path listings:
$matlabroot/work/myHelloArchive.jar
The dynamic Java class path is modified using the following MATLAB commands:
javaaddpath % Add entries to dynamic Java class path.
javarmpath % Remove entries from dynamic Java class path
clear java % Removes all variables, functions, MEX-files, and dynamic Java class definitions from memory, leaving the workspace empty.
To add myHelloArchive.jar to your dynamic Java path, issue the following command at the MATLAB command prompt:
javaaddpath(fullfile(matlabroot,'work','myHelloArchive.jar'))
You need only add the JAR file to either your static or dynamic Java class path.
For more information on the Java class path, issue the following command at the MATLAB command prompt:
web([docroot '/techdoc/matlab_external/ch05mat5.html#4867'])
Step 3. Verify that the JAR archive file is listed on your Java class path.
Issue the following command at the MATLAB command prompt:
javaclasspath
You should see "$MATLAB\work\myHelloArchive.jar" listed under either your Static Java Path or Dynamic Java Path, depending on how you added it to your Java class path in Step 2.
Step 4. Make calls to the Java class(es) contained in your JAR file.
Now that you have the myHelloArchive.jar file on your Java class path, you should be able to use the HelloWorld class that it contains.
4a. Call Public Static Functions of the ClassPublic static functions of a Java class can be called without having to create an instance of that class. For the HelloWorld class of the myHelloArchive.jar archive file, this can be done by issuing the following command at the MATLAB command prompt:HelloWorld.main('') % Call public static functions of the HelloWorld class
4b. Create an Instance of the ClassTo create an instance of the HelloWorld class, issue the following command at the MATLAB command prompt:myHelloObject = HelloWorld % Create an instance of the class through the constructor
4c. Call methods of the ClassThe following code sequence shows different calls to the public methods of the myHelloObject object created in Step 4b. myHelloObject.getVersion() myHelloObject.setVersion(-1) myHelloObject.getVersion() myHelloObject.setVersion(1) myHelloObject.getVersion()
For more information on calling Java from MATLAB, refer to the "Calling Java from MATLAB" section of the MATLAB documentation by issuing the following command at the MATLAB command prompt:
web([docroot '/techdoc/matlab_external/ch05matf.html'])
Best Answer