I'm having difficulty using a java package in MATLAB
Firstly I add my .jar to the dynamic path:
filename = fullfile(pwd,'Compiled','SpectrometerInterface.jar');
ls(filename)
javaaddpath(filename);
The ls returns SpectrometerInterface.jar confirming the files existence, and javaclasspath confirms it is indeed added.
Then when I request an object, using either
obj = javaObject('com.jellymatter.spectrometerinterface.UDPSpectrometer');
or
obj = com.jellymatter.spectrometerinterface.UDPSpectrometer()
it says the class cannot be found. It is definitely in the correct directory within the .jar, and a reduced version of my class is as follows
package com.jellymatter.spectrometerinterface;
public class UDPSpectrometer {
private UDPInterface inter = null;
public UDPSpectrometer(){
/* Nothing here */
}
public void connect(String addr, int p1, int p2) throws CommunicationException{
...
}
public double[] get() throws CommunicationException, SpectrometerNotCalibratedException {
...
}
}
Any suggestions?
Edit: The Diagnosis
MATLAB's Java virtual machine was to low a version (JRE 1.5) for the compiled java (JDK 1.7).
Edit: Solutions
I solved it by compiling using JDK 1.5, but, also, one could upgrade the JVM in MATLAB.
Please take a look at this and set your classpath.txt
Related
Is it still possible to use Java in Classic ASP (IIS 7.* / Windows) via Java Moniker as is demonstrated at http://cephas.net/blog/2004/03/15/scripting-in-asp-with-java/? (and http://thrysoee.dk/InsideCOM+/ch11f.htm and https://hq.lojcomm.com.br/java/Interop.class.asp).
I cannot make it work from IIS 8.5 (or from dektop VbScript) in my Windows 10 with Java 1.8.0_131 installed in C:\Program Files\Java.
I get all the time: error'800401e4' with no other clarifications in the first line:
set Java_Date = getObject("java:java.util.Date")
response.write( Java_Date.toString() )
and the same error comes if I try to use NetBeans-compiled custom class:
set Java_Greeter = getObject("java:omatestit.Hello")
public class Hello
{
public static void main(String[] args)
{
//do nothing - this will keep us from getting a compile error
}
public String SayHello()
{
return "Hello Geek";
}
}
which locates in:
"C:\Program Files\Java\jdk1.8.0_131\include\win32\TrustLib\omatestit\Hello.class"
Can anyone figure out what is wrong with previous settings?
I'm trying to call a java function from a Matlab script, I tried all the solutions put in the website but I didn't get an issue.
My class is simple:
package testMatlabInterface;
public class TestFunction
{
private double value;
public TestFunction()
{
value=0;
}
public double Add(double v)
{
value += v;
return value;
}
public static void main(String args[])
{
}
}
So I put .java file (also .class) in my workingspace C:\scriptsMatlab and I added this path to javaclasspath of Matlab, but when I try to call the function , it tells me that there's no class with this name in javaclasspath of Matlab.
EDIT:
Here's the version of java that Matlab uses:
Java 1.6.0_17-b04 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode
And this is the version on jdk which I used to compile my class :
But when I try to execute this commande from matlab
>> javaaddpath 'C:\scriptsMatlab'
>> obj = TestFunction
it tells me:
Undefined function or variable 'TestFunction'.
Option 1
Check if same JRE/JDK is being used to compile your JAVA file. Execute on Matlab:
version -java
Compie your MyFunction.java with same jdk as above , and locate your MyFunction.class
Locate your Matlab classpath.txt. Type following into matlab cmd.
which classpath.txt
Open your classpath.txt as administrator.Add the full path for the directory with the MyFunction.class to the end of the 'classpath.txt' file as a single line and save the file.
Restart Matlab.
To run in Matlab . Create object of MyFunction.
obj = MyFunction
To run main() method in matlab.
javaMethod('main', obj, '')
Option 2
Follow Steps 1-2.
Execute following command in Matlab
JAVAADDPATH PATH/to/Directoryof MyFunction.class.
No need to restart Matlab here.
Just run using
obj = MyFunction;
javaMethod('main', obj);
From MathWorks :
javaMethod(MethodName,JavaObj,x1,...,xN) calls the method in the class of the Java® object array with the signature matching the arguments x1,...,xN.
javaMethod(StaticMethodName,ClassName,x1,...,xN) calls the static method in class ClassName.
I am relatively new to programming and java and am trying to learn how to create a user-defined package from the command-line. I get the following: Error: Could not find or load main class TestPhone. I've reviewed posts on this type of error including the well-commented post here. The post lists 4 steps that the JVM goes through to run a java program:
Search for the compiled version of the class.
Load the class.
Check that the class has a main method with signature static void main(String[]).
Call that method passing it the command line arguments as a String[].
Apparently, my JVM can't find my TestPhone.class for some reason I am yet to figure out. Here's my directory structure:
My classpath is set as follows:
My classes contain simple codes from Mala Gupta to test accessibility of class variables:
package mobile;
class Phone {
static boolean softKeyboard = true;
}
package mobile;
class TestPhone {
public static void main (String[] args) {
Phone.softKeyboard = false;
Phone p1 = new Phone();
Phone p2 = new Phone();
System.out.println(p1.softKeyboard);
System.out.println(p2.softKeyboard);
p1.softKeyboard = true;
System.out.println(p1.softKeyboard);
System.out.println(p2.softKeyboard);
}
}
Any idea why it doesn't find my classes? Many thanks.
System specs:
Java version 1.8.0
Javac 1.8.0
Win 7 on 32-bit OS
The java application launcher, java, expects the fully qualified name of the class, mobile.TestPhone. The ../mobile directory need not be in the classpath.
You are in the myJavaProject folder in cmd.
Try to go to c:\myJavaProject\mobile then it should work because your class is in mobile not in
myJavaProject folder
I have looked at all the other stuff, mine is a compatibility issue I think, or PATH maybe. I have a bunch of classes I've been using since about 2008 and now the java command and the javac command can't find the classes even though they are in the same directory/folder. I have C:\Program Files\Java\jdk1.6.0_25\bin in the Path variable, but nothing in the Classpath. I normally have the compiled classes in the same folder with the java I'm compiling. I've been doing the same thing for 5 years! I have recompiled the lowest level class which is called WotifCat01. The compiler comes back with
WotifCat00.java:27:cannot find symbol
Symbol : WotifCat01
import java.io.*;
/** Find/replace program **/
class WotifCat00
{
private static int cnt;
private static String[][] filnamStrg={ {"Data/reftfile.txt","Data/AccumData/allreftfile.txt","","",""},
{"Data/reft1file.txt","Data/AccumData/allreft1file.txt","","",""},
{"Data/reft2file.txt","Data/AccumData/allreft2file.txt","","",""},
{"Data/reft3file.txt","Data/AccumData/allreft3file.txt","","",""},
{"Data/reft4file.txt","Data/AccumData/allreft4file.txt","","",""},
{"Data/work1file.txt","Data/AccumData/alltextsrc.txt","","",""},
{"","","","",""} };
private static String[] args1={"","","","",""};
public static void main(String[] args) {
while (filnamStrg[cnt][0] != "") {
args1[0] = filnamStrg[cnt][0];
args1[1] = filnamStrg[cnt][1];
WotifCat01 wotifCat01 = new WotifCat01();
wotifCat01.main(args1);
cnt++;
}
}
}
I've used this setup for a while and it worked fine on my laptop till now with Windows 7. I suspect something I've installed has overwritten something. This has to be really simple but I can't see it. I've removed jdk1.7.0_25 back to 1.6 but no change.
I backed up the classpath and deleted it. Now works fine. It contained IBM DB2 paths. DB2 I had installed in 2011 so discounted it, but it did have java paths in it so it must have overrode the path. I am not sure how DB2 managed to do so 2 years after install, I may have inadvertently activated something. Thanks for your input.
Neil Mc
I'm trying to run project which uses fannj library, but I'm getting error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'fann_create_standard_array':
at com.sun.jna.Function.<init>(Function.java:179)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:347)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:327)
at com.sun.jna.Native.register(Native.java:1355)
at com.sun.jna.Native.register(Native.java:1032)
at com.googlecode.fannj.Fann.<clinit>(Fann.java:46)
at javaapplication9.JavaApplication9.main(JavaApplication9.java:14)
Java Result: 1
This is what I did:
I put fannfloat.dll to C:\Windows\System32
I added fannj-0.3.jar to project
I added newest jna.jar to project
here is code:
public static void main(String[] args) {
System.setProperty("jna.library.path", "C:\\Windows\\System32");
System.loadLibrary("fannfloat");
Fann fann=new Fann("D:\\SunSpots.net");
fann.close();
}
SunSpots.net is file from example package. fannfloat.dll: you can get from here.
The "#8" at the end of _fann_create_standard_array indicates that the library is using the stdcall calling convention, so your library interface needs to implement that interface (StdCallLibrary) and it will automatically get the function name mapper applied that converts your simple java name to the decorated stdcall one.
This is covered in the JNA documentation.
It was the first time I had to work with FANN and it took me some time to make it work.
Downloaded Fann 2.2.0. Extract (in my case "C:/FANN-2.2.0-Source") and check the path of the fannfloat.dll file. This is the library that we will use later.
Download fannj-0.6.jar from http://code.google.com/p/fannj/downloads/list.
The dll is compiled for 32 bit environment. So, make sure you have a 32 bit Java installed (even in 64 bit Windows).
I suppose you already have the .net file with your ANN. Write something like this in Java
public class FannTest {
public static void main(String[] args) {
System.setProperty("jna.library.path", "C:/FANN-2.2.0-Source/bin");
Fann fann = new Fann("C:/MySunSpots.net" );
float[] inputs = new float[]{0.686470295f, 0.749375936f, 0.555167249f, 0.816774838f, 0.767848228f, 0.60908637f};
float[] outputs = fann.run( inputs );
fann.close();
for (float f : outputs) {
System.out.print(f + ",");
}
}
}