Compiling Java programs with GCJ for MATLAB to invoke Java object method practice

Source: Internet
Author: User

1 Introduction

With MATLAB as the development platform, the numerical calculation has the characteristics of direct and high efficiency.

However, in the aspect of object-oriented program design, the existing MATLAB support features are not high in development and operation efficiency.

The Java language object-oriented and its platform features are introduced into MATLAB, which can extend its model expression ability and means. In addition, the characteristics of Java itself, it also determines that it has good development performance.

The development purpose of the MEX dynamic link library is different from the C language (Mex is mainly at the speed), and the Java-based MATLAB extension can provide more flexibility for MATLAB. Even more libraries under the Java platform can enrich the functions of MATLAB platform.

2 selection of the Java compiler

Test platform: Kylin 14.04 64bits

MATLAB version: 13a

Java environment: Oracle Sun Jdk8 and Gnu GCJ


Search for resources on the network, including the Java static and dynamic class search path settings in MATLAB, but the use of JDK8 experiment is not successful-when loading the custom class, MATLAB error

<span style= "color: #3333FF;" >>> d = javaobject (' Dog ') </span>a Java exception occurred trying to load the Dog Class:java exception occur Red:java.lang.UnsupportedClassVersionError:Dog: <span style= "color: #FF0000;" >unsupported major.minor version 52.0</span>    at java.lang.ClassLoader.defineClass1 ( Native Method)     at java.lang.ClassLoader.defineClass (Unknown Source)     at Java.security.SecureClassLoader.defineClass (Unknown Source)     at Java.net.URLClassLoader.defineClass (Unknown Source)     at java.net.urlclassloader.access$000 ( Unknown source)     at Java.net.urlclassloader$1.run (Unknown source)     at Java.security.AccessController.doPrivileged (Native Method)     at Java.net.URLClassLoader.findClass (Unknown source)     at Java.lang.ClassLoader.loadClass (Unknown source)     at Sun.misc.launcher$appclassloader.loaDClass (Unknown source)     at Java.lang.ClassLoader.loadClass (Unknown source)     at Java.lang.ClassLoader.loadClassInternal (Unknown Source)     at JAVA.LANG.CLASS.FORNAME0 (Native Method)     at java.lang.Class.forName (Unknown Source)     at Com.mathworks.jmi.OpaqueJavaInterface.findClass (opaquejavainterface.java:470) Error using Javaobjectno class Dog can Be located on the Java class path
Analysis, the main reason is that the Java version is too high, and the Java version used by Matlab does not adapt.


View the Java version in MATLAB

>> Version-java

Ans =

Java 1.6.0_17-B04 with Sun Microsystems Inc. Java HotSpot (TM) 64-bit Server VM Mixed mode


Tested by Javac-target 1.6 Xxx.java, show Javac: Target release 1.6 conflicts with the default source release 1.8; You cannot use the current Javac compiler to produce a class that requires a version of MATLAB.


Since some of the posts mention using the GNU GCJ compiler, install the compiler test:

The installation method is:

sudo apt-get install GCJ-JDK

GCJ-JDK version 4.8 after installation

3 Compiling Java Class 3.1 test code using GCJ-JDK Dog.java

Class Dog {  private String name;  Private Integer count;    Public Dog () {  name = null;  Count = 0;   }    Public Dog (String aname) {  count = 0;  name = Aname;  }    Public String GetName () {  count++;  Return name + "_" + count.tostring ();  }    public void SetName (String newName) {  name = NewName;  }    public static void Main (string[] args) {    dog d = new Dog ("Martin Chen");    System.out.println (D.getname ());}  }
For ease of debugging and testing in MATLAB, the package is not used.


3.2 Code Compilation

Gcj-c Dog.java

Produced a Dog.class

Compress Dog.class to Dog.jar, in order to simplify operation, do not write MANIFEST.MF file, directly compress

Jar CVF Dog.jar Dog.class

3.3 Java class search path setting in MATLAB 1) View the existing Classpath command in MATLAB and see its built-in loadable Java classes and dynamic class loading information

Javaclasspath

The information is as follows:

>> javaclasspathstatic JAVA Path/usr/local/matlab/r2013a/java/patch /usr/local/matlab/r2013a/java/jarext/jaxen.jar/usr/local/matlab/r2013a/j Ava/jarext/xstream.jar omitted .../usr/local/matlab/r2013a/java/jarext/comm                                    Ons-io.jar/usr/local/matlab/r2013a/java/jar/toolbox/dastudio.jar /usr/local/matlab/r2013a/java/jarext/freehep-util.jar/usr/local/matl                                            Ab/r2013a/java/jarext/rxtxcomm.jar/usr/local/matlab/r2013a/java/jar/instutil.jar                                  /usr/local/matlab/r2013a/java/jar/toolbox/instrument.jar                                                                                     /usr/local/matlab/r2013a/java/jarext/openxml4j.jar    DYNAMIC JAVA path<empty> 

2) View the location of the static path store file in matlab

Which Classpath.txt

Results:

/usr/local/matlab/r2013a/toolbox/local/classpath.txt

3) Edit the Classpath.txt, add Dog.jar to the static path

sudo gedit/usr/local/matlab/r2013a/toolbox/local/classpath.txt

Add at end of file

/home/your_username/matlab_java/dog.jar

4) test the dog in MATLAB

>> D1 = javaobject (' Dog ', ' martin ') d1 = [email protected] >> d1.setname (' program ') >> d1.getname () ans = Program _1 >> D1.getname () ans = program _2 >> d1.getname () ans = Program _3 >> d2 = javaobject (' Dog ') d2 = [email protected] >> D2.getname () ans = null_1

Description: Two constructors for dog were called when D1 and D2 were created.

The test results show that the Java class is loaded and called correctly.

5) Dynamically load the class in MATLAB and call it

During the development process, because of the need to constantly modify the code, so during debugging, you can use dynamic loading mode, load user-defined class.

Re-edit the Classpath.txt file to remove Dog.jar loading information.

Run the following command in MATLAB:

>> javaaddpath ('/home/your_username/documents/matlab_java/dog.jar ') >> d = javaobject (' Dog ', ' martin ') d = [Email protected] >> D.getname () ans = martin_1
The experimental results also show that the dog is loaded and called correctly.

6) View the available methods of the dog class

>> methods Dogmethods for class Dog:dog        getclass   hashcode   notify     setName    wait       equals     getName    main       notifyall  toString   

Ok, after the above experiments, we can finally rest assured and free to use Java in MATLAB.

Compiling Java programs with GCJ for MATLAB to invoke Java object method practice

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.