Compile Cplex in java in Linux 2 - java

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

Related

Calling matlab from java, java.lang.UnsatisfiedLinkError

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.

Class not found when running program from command line

Here is the structure of my project:
MyProject
-lib
-mp3agic-0.9.0.jar
-src
-com.company
-Main.java
Inside Main, I import the content of mp3agic:
import com.mpatric.mp3agic.*;
From MyProject, I compile the class using:
javac -cp "lib/*" src/com/company/Main.java
From MyProject/src, I try to run the program with:
java -cp . com/company/Main
and I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/mpatric/mp3agic/InvalidDataException
Apparently, the jdk can not find the dependencies from the lib folder when running the class, albeit this problem doesn't occur when compiling.
I guess there's a problem with the syntax of the command, but I just can't find it.
Can you please help me?
EDIT: I solved the problem, moving the lib folder under src and then running:
C:\MyProject\src>javac -cp "lib/*" com/company/Main.java
C:\MyProject\src>java -cp .;"lib/*" com/company/Main
Now, the code compiles and executes without any issue.
HOWEVER, if I leave lib under the project root, this is what happens:
C:\MyProject>javac -cp "lib/*" src/com/company/Main.java
C:\MyProject>java -cp .;"lib/*" src/com/company/Main
Error: Could not find or load main class src.com.company.Main
WHY?
You should use maven or gradle or you'll have to say which console are your using, and which operating system -if you don't know what a console is, you'll first have to google for that before messing with javac directly- But if you insist:
for Linux, OSX, any UNIX-like:
javac -cp ".:lib/mp3agic-0.9.0.jar" src/com/company/Main.java
for Windows:
javac -cp ".;lib/mp3agic-0.9.0.jar" src/com/company/Main.java
To run under windows:
java -cp .;lib\mp4agic-0.9.0.jar com.company.Main
You may need to use slash (/) instead of backslash depending of which console you're using. Or just try "/" or "\" and see what works for you.
If you still get class not found exception, then the issue is not related with classpath or java command but on your code (hard to say, without seeing the code).
java.exe -classpath {path} Main
I think you have to add classpath also and when run the program(not only compile)

Error in java.library.path when calling a Java function that uses Cplex, from Matlab

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.

Cross-compile java applications

I am working on an embedded system with openwrt root files system and linux kernel.
I have compiled the trunk, no problem with that. I have installed the Java resources in /usr/bin, /usr/lib and /usr/share, but I haven't been able to compile some simple programs that I have done in Eclipse. I have used javac to compile a hello world and I obtain the .class file but when I try to execute the helloworld.class file in my embedded system with:
java helloworld.class
it does nothing, it just says:
/usr/bin/java: line 1: syntax error: unexpected word (expecting ")")
When I execute this in my computer it runs, so I guess it is because I have to cross-compile the java files, so how can I do that?
The problem is not with your class, but with a syntax error in the /usr/bin/java script - try cat /usr/bin/java
Just try the java command withoud .class extension like
java helloworld

.jar works in Windows but not Linux (stacktrace)

This is what I get:
[user#localhost KioskMenu]$ java -jar ./kioskmenu.jar
Exception in thread "main" java.lang.ClassFormatError: KioskMenu (unrecognized class file version)
at java.lang.VMClassLoader.defineClass(libgcj.so.7rh)
at java.lang.ClassLoader.defineClass(libgcj.so.7rh)
at java.security.SecureClassLoader.defineClass(libgcj.so.7rh)
at java.net.URLClassLoader.findClass(libgcj.so.7rh)
at gnu.gcj.runtime.SystemClassLoader.findClass(libgcj.so.7rh)
at java.lang.ClassLoader.loadClass(libgcj.so.7rh)
at java.lang.ClassLoader.loadClass(libgcj.so.7rh)
at gnu.java.lang.MainThread.run(libgcj.so.7rh)
[user#localhost KioskMenu]$
The only thing I noticed was that my RHEL is using a "free" version of Java. But this shouldn't matter should it?
You compiled the jar with a newer version of Java than the one on your Linux machine.
You should really be doing this:
java -jar ./kioskmenu.jar
Notice the -.
If you just give java jar ./kioskmenu.jar, the JVM is trying to find a class named jar and trying to pass ./kioskmenu.jar as an argument to it - which is clearly not what you want.

Categories