I am trying to call a matlab function I have from java (developing in Eclipse). I can get it to run just fine with R2019b, but when I change the java build path to R2019a (engine.jar - C:\Program Files\MATLAB\R2019a\extern\engines\java\jar), I get an error:
Caused by: java.lang.UnsatisfiedLinkError: com.mathworks.mvm.MvmFactory.nativeSetJavaEngine(Z)V
at com.mathworks.mvm.MvmFactory.nativeSetJavaEngine(Native Method)
at com.mathworks.mvm.MvmImpl.setJavaEngineMode(MvmImpl.java:203)
at com.mathworks.engine.MatlabEngine.<clinit>(MatlabEngine.java:69)
at MyController.onRunSimClick(MyController.java:190)
... 62 more
I have added both C:\Program Files\MATLAB\R2019b\bin\win64 and C:\Program Files\MATLAB\R2019a\bin\win64 to my PATH variable.
The start of the stack trace is:
java.lang.NoSuchMethodError: Method com.mathworks.mvm.MvmFactory.nativeCreateFactory(IIZ[Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;)J not found
I am happy to provide the rest, but don't want to create clutter. Thanks in advance!
I cannot comment yet, so putting this in as an answer. The first thing you might want to do is run:
java -XshowSettings:properties -version
to see what your java.library.path is.
I found a workaround, where I can just compile and run my code from the command line. I had to add C:\Program Files\MATLAB\R2019a\bin\win64; to my path before any other matlab version. Then, as per the matlab documentation I could just do:
> javac -classpath matlabroot\extern\engines\java\jar\engine.jar MyJavaCode.java
> java -classpath .;matlabroot\extern\engines\java\jar\engine.jar MyJavaCode
I'm still not sure why this wouldn't work in Eclipse, but this workaround will work for the short term.
Related
I was trying to run a simple java program in windows by running the command
pmd -d path-to-source\com\vaannila\domain\User.java -f text -R java-basic
and error showing up is as below.
Exception in thread "main" java.lang.NoClassDefFoundError: C:\pmd\pmd-bin-5/4/6\bin//\lib\asm-5/0/4/jar
Caused by: java.lang.ClassNotFoundException: C:\pmd\pmd-bin-5.4.6\bin..\lib\asm-5.0.4.jar
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: C:\pmd\pmd-bin-5.4.6\bin..\lib\asm-5.0.4.jar. Program will exit.
Please help me with this to run the PMD
I can't reproduce your problem and I can't exactly make sense of the error message.
According to the error message, you have extracted PMD 5.4.6 into C:\pmd, so that the directory C:\pmd\pmd-bin-5.4.6 exists. Inside this directory, there are two subdirectories: bin - which contains pmd.bat and lib, which contains the necessary jars to be put onto the classpath.
So, without further configuration of your system, you should be able to run simply C:\pmd\pmd-5.4.6\bin\pmd.bat in a command shell - you should see the help screen (and not the NoClassDefFound exception).
Can you try this? Does that work for you?
If not -> reinstall PMD as your installation seems to be broken somehow.
I see, that you are calling pmd with just using pmd - this works, if you are currently in the directory C:\pmd\pmd-bin-5.4.6\bin - or if you add this directory to your PATH environment variable. Even if I do this, it works for me, regardless in which directory I'm in.
Did you configure something like the PATH environment variable?
I cannot make sense out of the error message - it claims, it can't find a class named "C:\pmd\pmd-bin-5/4/6\bin//\lib\asm-5/0/4/jar". This is not a class name, it looks like a manipulated class path - and also look at the mix of forward and backward slashes. The next part of the error is "C:\pmd\pmd-bin-5.4.6\bin..\lib\asm-5.0.4.jar" - while it might seem to be right, that there is a slash missing - it still doesn't make sense. The error message said, that it could not find the main class - but the main class is called net.soureforge.pmd.PMD.
For reference, here is the original content of pmd.bat:
#echo off
set TOPDIR=%~dp0..
set OPTS=
set MAIN_CLASS=net.sourceforge.pmd.PMD
java -classpath %TOPDIR%\lib\* %OPTS% %MAIN_CLASS% %*
Can you compare this with your pmd.bat?
I suspect, your file looks somehow different.
You can also run PMD without the batch file, e.g.:
java -classpath C:\pmd\pmd-bin-5.4.6\lib\* net.sourceforge.pmd.PMD -d path-to-source\com\vaannila\domain\User.java -f text -R java-basic
Does that work for you?
P.S.: It's true, that PMD 5.4.6 is the last one, that runs on Java 1.6. But that doesn't mean, that newer versions of PMD cannot analyze Java 1.6. code. You would just need a recent java runtime (e.g. java 10 or 11) for executing PMD and a separate JDK for building your project. PMD can analyze any version of java, it doesn't depend on the java version, it is running with.
I would like to call a Java function that uses Cplex, from Matlab.
At the moment, I am stuck. Below is a list of things I tried, but I am out of ideas. Before I get there, let me list the versions:
Matlab version 8.3.0.532 (R2014a)
Java 1.7.0_11-b21 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
Eclipse with jre7 and jdk1.7.0_79
I created a package "milp.jar" and try to call the following function from Matlab:
milp.MTsolver.Plan()
In a basic version of my milp.jar, without any Cplex functionality, this goes without problems. Just as soon as I use a Cplex function in my Java class, I get in trouble. I tried the following:
To be able to use Cplex, I added the path to the javaclasspath.txt file in the Matlab folder
prefdir
The command
javaclasspath
in Matlab console indeed shows that this path is added to the Static Java Path.
But then, when calling my cplex dependent function milp.MTsolver.Plan(), I get the following error:
Java exception occurred:
java.lang.UnsatisfiedLinkError: no cplex1262 in java.library.path
java.library.path must point to the directory containing the CPLEX shared library
try invoking java with java -Djava.library.path=...
Following this post, I should first load the library:
java.lang.System.load('C:\Program Files\IBM\ILOG\CPLEX_Studio1262\cplex\lib\cplex.jar');
But this gives the following error:
Java exception occurred:
java.lang.UnsatisfiedLinkError: C:\Program Files\IBM\ILOG\CPLEX_Studio1262\cplex\lib\cplex.jar: %1 is not a valid Win32 application
At his point, I tried another approach. I just copied the cplex.jar into the Matlab root folder. No success. Nor any succes when just copying the cplex.jar in the project folder in Matlab.
Then, I tried to find a cplex1262.jar, but I only found a cplex1262.dll. So I added this path to the Static Java Path. This gives the following error:
Java exception occurred:
java.lang.UnsatisfiedLinkError: ilog.cplex.Cplex.CPXopenCPLEX([I)J
The same error occurs when I run the following in the Matlab console:
java.lang.System.load('C:\Program Files\IBM\ILOG\CPLEX_Studio1262\opl\oplide\plugins\ilog.odms.ide.opllang.win32.win32.x86_64_12.6.2.0\cplex1262.dll');
A last thing I could try comes from an answer in this post.
Here, he sets
-Djava.library.path=...
in the VM Options field, but in Netbeans. I could try to do something similar in Matlab, but I have not figured out yet how.
Last, I just added the directories containing cplex.jar and cplex1262.dll to the PATH environment variable. No luck there either.
Another important note might be that I have no problem running my code in Eclipse, including Cplex functionality! It might also be worth mentioning that, in between changes, I restarted Matlab to be sure that all changes in path settings took effect.
I hope to have stated my problem clearly enough. Any suggestions on how to proceed are very welcome!
I was able to find a quick and dirty solution, which is ok for now.
First I tried to add the path to the directory containing cplex1262.dll to the
'java.library.path' as follows:
java.lang.System.setProperty('java.library.path', [path to dir])
But that did not work for me. In a quick and dirty manner, I looked up what paths were already included in this property, with the following command.
>> java.lang.System.getProperty('java.library.path')
ans =
C:\Program Files\MATLAB\R2014a\bin\win64;C:\Program Files\MATLAB\R2014a\sys\jxbrowser\win64\lib
And just copy/pasted the cplex1262.dll in the \lib directory.
That worked.
I'm trying to run a file.java and file.class for which I need to use a library of Cplex (optimization problem) using
$ javac -cp /home/apps/cplex/12.6.1/cplex/lib/cplex.jar file.java
$ java -cp /home/apps/cplex/12.6.1/cplex/lib/cplex.jar:. file
which was recommended when I asked in Compile Cplex in Java in linux . I finally ended the correction of my file (thing that worked in windows didn't in Linux) and the first line worked, but in the second one I got this error...
java.lang.UnsatisfiedLinkError: no cplex1261 in java.library.path
java.library.path must point to the directory containing the CPLEX shared library
try invoking java with java -Djava.library.path=...
Exception in thread "main" java.lang.UnsatisfiedLinkError: ilog.cplex.Cplex.CPXopenCPLEX([I)J
at ilog.cplex.Cplex.CPXopenCPLEX(Native Method)
at ilog.cplex.CplexI.init(CplexI.java:6608)
at ilog.cplex.CplexI.<init>(CplexI.java:629)
at ilog.cplex.IloCplex.<init>(IloCplex.java:10194)
at ilog.cplex.IloCplex.<init>(IloCplex.java:10209)
at memoria.bosques.problemafull(bosques.java:11395)
at memoria.bosques.main2(bosques.java:17829)
at memoria.bosques.main(bosques.java:18014)
I tried replacing java -Djava.library.path=/home/apps/cplex/12.6.1/cplex/lib/cplex.jar instead of -cp /home/apps/cplex/12.6.1/cplex/lib/cplex.jar but didn't work either.
The error occur in the fist time I define something of Cplex(before this was running fine)
IloCplex cplex = new IloCplex();
what can I do to solve my problem?
You need to do exactly what is suggested in the error message. And invoke java with -Djava.library.path pointing to the locations of the cplex shared module.
Something like this:
java -cp /home/apps/cplex/12.6.1/cplex/lib/cplex.jar:. -Djava.library.path=/path/to/libcplex121.so file
I am trying to compile and a sample Helloworld.java file.
I have my jdk installed in C:\Program Files\jdk1.7\bin.
And I have my Helloworld.java in C:\Helloworld.java
I am actually a novice in both powershell and java.
I got some examples from web regarding this but many of them advice to run it like this:
java.exe -classpath $Env:CLASSPATH C:\Helloworld.java
But when I give this in powershell I get an error called 'CLASSPATH' is not defined even after adding it in env variables.
And when I try to compile the code with the following syntax:
$javac C:\Helloworld.java I get an error "javac is not recognised as a token".
So, I am literally lost in this topic . Any step by step procedure to run java programs using powershell for dummies like me will be greatly appreciated.
Setup environment variables in your system.
set JAVA_HOME to C:\Program Files\jdk1.7
add to PATH variable the string %JAVA_HOME%\bin
open new cmd session.
navigate your java source folder.
use javac to compile your java files.
UPDATE:
also if you are experiencing difficulities upon launching an executable via PowerShell check this Microsoft TechNet article
The variables you speak of do not exist in PowerShell as you name them.
The correct variable names are
$Env:JAVA_HOME: C:\jdk1.6.0;
$Env:PATH: C:\jdk1.6.0\bin;.;
$Env:CLASSPATH: C:\jdk1.6.0\lib;.;
As they all must be defined in the ENV: PSDrive
To answer it in a much simpler way , its the path problem .
You probably not have set env variables that's it.
This is how you should set it:
JAVA_HOME: C:\jdk1.6.0;
PATH: C:\jdk1.6.0\bin;.;
CLASSPATH: C:\jdk1.6.0\lib;.;
And later if you open a cmd prompt and type java -version , if you are able to see the java installed version then you are good to go.
I am getting a error message like this:
The library libraryname.dll could not be loaded by Windows. Make
sure that the library is in you Path environment variable. Exception
in thread "main" java.lang.UnsatifiedLinkError: no libraryname in
java.library.path.
This error is from me trying to run a jar file on Windows XP via cmd. I am wondering, where exactly is java.library.path? I've already added C:\Program Files\Java\jdk1.6.0_26 to my PATH but it still gives me the error. How would you go about debugging this?
Thanks.
You can simply pass java.library.path as a system property as shown below:
java -Djava.library.path=<path_to_dll> <main_class>
First you need to find out where the libraryname.dll is and add it above in "path_to_dll".
The error is basically saying it cannot find your native libraries. Java tries to locate your library by looking into java.library.path property
It's an System environment that you need so Java can find your native libraries when you run your application. Several ways to do it:
Use java -Djava.library.path=[path to your library] when running your program
From the code you could also do.
System.setProperty( "java.library.path", "/path/to/libs" );
Set it up from your IDE. An example for Eclipse can be found in this SO question How to set java.library.path from eclipse
EDIT: A good comment below pointed out that #2 will not working 100% because you might not set this prior to calling getProperty(). Missed that point and thanks for pointing that out.