I want to run the Java code below in Matlab.
It ran successfully in JAVA environment.
I am using
apache-jakarta-commons-net-1.4.1.jar
to run this code.
The software that I use are
MATLAB Version 7.7.0.471 (R2008b)
Operating System: Microsoft Windows Vista Version 6.1 (Build 7600)
Java VM Version: Java 1.6.0_04 with Sun Microsystems Inc.
Java HotSpot(TM) Client VM mixed mode
My java code is
import java.io.IOException;
import org.apache.commons.net.TimeTCPClient;// third party class
public final class TimeClient
{
public static final void timeTCP(String host) throws IOException
{
TimeTCPClient client = new TimeTCPClient();
try {
// We want to timeout if a response takes longer than 4 seconds
client.setDefaultTimeout(4000);
client.connect(host);
System.out.println(client.getDate());
}
finally {
client.disconnect();
}
}
public static final void main(String[] args)
{
{
try
{
timeTCP(args[0]);
}
catch (IOException e)
{
e.printStackTrace();
System.exit(1);
}
}
}
}
which timeclient gave the following error
Warning: A Java exception occurred trying to load the TimeClient class:
Java exception occurred:
java.lang.UnsupportedClassVersionError: TimeClient : Unsupported major.minor
version 51.0
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)
'TimeClient' not found.
MATLAB is apparently using a different Java version from the one you used to compile your
.jar. Make sure to compile it with the Java version used inside MATLAB. Since you didn’t state how you determined the Java version number given in your question, make sure you callfrom the Matlab command prompt, and then use that version to compile your
.jar. More info can be found here.