JPype is an amazing project since I allows to instantiate a JVM directly from Python.
Unfortunately, I got stuck in the first baby steps.
I have A.java source code (located in C:\tmp folder):
class A {
public A() {
super();
}
public String sayHi() {
return("Hello");
}
}
Which was compiled to a class, using: javac A.java
Thus, A.class is located in C:\tmp folder.
I have the following Python source code:
import os
import jpype
jpype.startJVM(jpype.getDefaultJVMPath(), '-ea', '-Djava.class.path=c:\\tmp')
A = jpype.JClass("A")
a = A()
print a.sayHi()
jpype.shutdownJVM()
When I run it, I get the error below:
C:\tmp>jpype_test.py
Traceback (most recent call last):
File "C:\tmp\jpype_test.py", line 10, in <module>
A = jpype.JClass("A")
File "C:\Python27\lib\site-packages\jpype\_jclass.py", line 54, in JClass
raise _RUNTIMEEXCEPTION.PYEXC("Class %s not found" % name)
jpype._jexception.ExceptionPyRaisable: java.lang.Exception: Class A not found
Since I can't find the A class, it is probably an issue related to CLASSPATH, but I can't realize what I am doing wrong.
Any clues?
EDIT 1:
The problem persists. But, just to add to my question, if I use native java libraries, like: java.util, the code runs WITHOUT errors. For example, the code below works:
import jpype
jpype.startJVM(jpype.getDefaultJVMPath())
util = jpype.JPackage("java.util")
al = util.ArrayList()
al.add(1)
al.add(2)
print al.size()
jpype.shutdownJVM()
And returns 2.
EDIT 2:
Issue solved, see answer below...
I solved the problem and I would let the answer here for the records.
1) Nothing was wrong with the source code.
2) Problem was that my Python was 32 bits and my java sdk (including the javac bytecode compiler) was 64 bits. I uninstalled the java sdk and re-installed a 32bits version. Done! Solved!
Try to change your path like this:
jpype.startJVM(jpype.getDefaultJVMPath(), '-ea', '-Djava.class.path=c:\\tmp\')
Related
I have built a DLL which I am attempting to wrap Java code with, however I am having some troubles with running my Java program. I wrote a simple test DLL and Java program and am producing the same error, and although there are plenty of resources regarding NoClassDefFoundError online I can't seem to solve mine with any troubleshooting methods.
Here is my D:\Test1.Java file
public class Test1 {
static {
//System.loadLibrary("HeyLand");
System.load("D://HeyLand.dll");
}
public native void displayHeyLand();
public static void main (String[] args) {
Test1 t = new Test1();
t.displayHeyLand();
}
}
After compiling, attempting to run D:\Test1.classresults in the following:
D:\>java Test1.class
Exception in thread "main" java.lang.NoClassDefFoundError: Test1.class
Caused by: java.lang.ClassNotFoundException: Test1.class
at java.net.URLClassLoader.findClass(URLClassLoader.java:434)
at java.lang.ClassLoader.loadClass(ClassLoader.java:660)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:358)
at java.lang.ClassLoader.loadClass(ClassLoader.java:626)
Could not find the main class: Test1.class. Program will exit.
Why I am stumped :
1. I have set my classpath to be D:\, so I believe my class definition would be in the classpath, and I do not see how my compile-time and run-time classpaths could be any different.
2. I don't see how this could have anything to do with static initialization, and I believe the exception would look different.
Perhaps I'm just missing something incredibly simple, I am very newbie with Java.
Any help is greatly appreciated!
The classpath environmental variable is taking precedence over that in the java run command. You need to specify the class location (as well as removing the .class file extension)
java -cp . Test1
Java normal syntax for executing class file is
Java [<options>....} <class-name> [<arguments>....]
For example
java com.package.name.Test1
here how compiler works
1. Compiler search for complete class name
2. Load that class
3. check for main method - in the same class
4. Call main method with passed arguments in command line string.
Now following are the possibilities why your class may not found main method.
1 - forgot to include package name
I am new developer in java but I found when I run application using eclips or intellJ editor it gives different path and package name and execute code as I noticed it on command line edior. So make sure you are including package name
For example:
java com.package.name.Test1 instead of
java Test1
2. File name or pathname rather then class name
As I noticed output file is in different location. That why class file path was different.
java Test1.class
java com/package/name/Test1.class
3. Typo
also I noticed you are using
static {
//System.loadLibrary("HeyLand");
System.load("D://HeyLand.dll");
}
Is this function ? or constructor? If it is function then where is name of the function? You cant write code without any reference in classs
I have read and searched all stack overflow .. I also found JPype class not found but it didn't help me although it is solved! I have the same problem ! I am using Mac , python 2.7.6
My both python code and A.java are on desktop. But I keep receiving this error :
Traceback (most recent call last): File
"/Users/jeren/Desktop/aa.py", line 13, in
A = jpype.JClass("A") File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/jpype/_jclass.py",
line 54, in JClass
raise _RUNTIMEEXCEPTION.PYEXC("Class %s not found" % name) java.lang.ExceptionPyRaisable: java.lang.Exception: Class A not found
aa.py :
import jpype
import os
jpype.startJVM(jpype.getDefaultJVMPath(), "-ea", "-Djava.class.path=/Users/jeren/Desktop/")
A = jpype.JClass("A")
a = A()
jpype.shutdownJVM()
A.java :
class A
{
public A()
{
super();
}
public String sayHi()
{
return("Hello");
}
public static void main(String[] argv)
{
System.out.println ("Hello ");
}
public static int add(int a, int b)
{
return(a+b);
}
}
My mac , java and python are all 64bit ! where the problem can be?
everything was ok just needed to add a 'public' to the beginning of class A:
public class A
{
public A()
{
super();
}
public String sayHi()
{
return("Hello");
}
Here are some further nodes on specifying the class path for jpype.
A. Check JDK path
I had several versions of Java JDK installed and getDefaultJVMPath did not yield the expected path. I needed to replace
jpype.getDefaultJVMPath()
with the path to the JDK, that actually has been used to compile the code, e.g
D:/jdk11/bin/server/jvm.dll
B. relative paths
It is possible to use relative paths. If my python file is for example in a package folder "pkg" and my java class file is in a sub folder "foo" of a "bin" folder:
parentFolder
pkg/main.py
bin/foo/Foo.class
jpype.startJVM(jvmPath, '-Djava.class.path=../bin")
link = jpype.JClass('foo.Foo')
For this example, the working directory of the java application will be the pkg folder. With other words, inside a main method of Foo class, you might want to use "../" to access the parentFolder.
C. -cp option does not work
I tried to use -cp option instead of -Djava.class.path, which I would found more induitive. However, the following code does not work:
jpype.startJVM(jvmPath, '-cp', classPath)
D. jars need to be included individually
I tried to include a folder with several jar files.
parentFolder
foo/main.py
lib/foo.jar
Following code does not work:
jpype.startJVM(jvmPath, '-Djava.class.path=../lib/*")
link = jpype.JClass('foo.Foo')
Each jar file needs to be included individually, e.g.:
libOath = '../lib'
libJarPaths = str.join(';', [libPath + '/' + name for name in os.listdir(libPath)])
jpype.startJVM(jvmPath, '-Djava.class.path=../lib/*")
link = jpype.JClass('foo.Foo')
(Solution from JPype (Python): importing folder of jar's )
OK, again having some problems with caliper.
I am now running on Linux, trying to use the beta snapshot. I am attempting to run Google's caliper via commandline using just the jar. (Beta snapshot)
I do not have access to maven on this machine, and installing it is out of the question. I would just like to use a jar and, maybe once this is working, I can write up a script or something.
Here is what I am doing:
1. Using small example Benchmark:
import com.google.caliper.Benchmark;
public class Tutorial {
public static class Benchmark1 {
#Benchmark void timeNanoTime(int reps) {
for (int i = 0; i < reps; i++) {
System.nanoTime();
}
}
}
}
2. Compile with javac -cp caliper-1.0-beta-SNAPSHOT-all.jar Tutorial.java
3. (Attempt to) run with
java -cp caliper-1.0-beta-SNAPSHOT-all.jar com.google.caliper.runner.CaliperMain Tutorial.Benchmark1, receive message Benchmark class not found: Tutorial.Benchmark1.
I've tried to work this out from bits and pieces of information from various sources but I am really having a heck of a time with this. I would appreciate any input.
I believe you really need no maven, this should work.
Your own class doesn't get found and I think it's a problem of your classpath. As they're usually more problem with nested classes try simply
java -cp caliper-1.0-beta-SNAPSHOT-all.jar com.google.caliper.runner.CaliperMain Tutorial
If the message changes to something like "class contains no benchmarks", then you'll know more. If you insists on using nested class, you may need to call Tutorial$Benchmark1 (unprobable, but possible; java class naming is sick).
Please try also
java -cp caliper-1.0-beta-SNAPSHOT-all.jar Tutorial.Benchmark1
to see if your class lies on the classpath (the message should change to something like "no main method").
See also this older post.
I want to use JOMP API (equivalent to OpenMP in C) but I met some problems:
This is the code I want to run:
import jomp.runtime.*;
public class Hello
{
public static void main (String argv[])
{
int myid;
//omp parallel private(myid)
{
myid = OMP.getThreadNum();
System.out.println("Hello from " + myid);
}
}
}
It is just an hello worl but I have a problem with the compiler. Please have a quick look at this page to understand:
http://www2.epcc.ed.ac.uk/computing/research_activities/jomp/download.html
But I can't, I do not understand how it works... I can only compile it with eclipse default compiler (I guess) and then I have only one thread!
I understand I have to compile this code (in a .jomp file) with
java jomp.compiler.Jomp MyFile
and then compile normally but I can't do this in ecplise neither in the terminal (I do not know how to install this compiler!)
ps: I use Ubuntu 12.04 on a Intel® Core™ i7-3610QM CPU # 2.30GHz × 8.
You just need to add the JOMP parameters to your launch configuration, this example can help you:
JOMP eclipse workaround
I've been trying to use Matlab's javabuilder package under Windows XP, but I'm getting a strange error when trying to instantiate any javabuilder class. To illustrate the problem, I've created a simple program that prints the MCRROOT and PATH system variables (to check if they're correctly set) and tries to create a MWCharArray:
import com.mathworks.toolbox.javabuilder.*;
import com.mathworks.toolbox.javabuilder.internal.MCRConfiguration;
class Main
{
public static void main(String[] args)
{
System.out.println("MCRROOT: " + System.getenv("MCRROOT"));
System.out.println("PATH: " + System.getenv("PATH"));
System.out.println(MCRConfiguration.isInstalledMCR());
MWCharArray test = new MWCharArray("Test");
}
}
When I execute the program, the output is:
MCRROOT: C:\Program files\MATLAB\MATLAB Compiler Runtime\v710
PATH: C:\Program files\CollabNet Subversion Client;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program files\MATLAB\MATLAB Compiler Runtime\v710
false
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration.getMCRRoot(MCRConfiguration.java:77)
at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration$ModuleDir.<clinit>(MCRConfiguration.java:51)
at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration.getModuleDir(MCRConfiguration.java:56)
at com.mathworks.toolbox.javabuilder.internal.MWMCR.<clinit>(MWMCR.java:1447)
at com.mathworks.toolbox.javabuilder.MWUtil.GetUnknownClassID(MWUtil.java:1258)
at com.mathworks.toolbox.javabuilder.MWClassID.<clinit>(MWClassID.java:41)
at com.mathworks.toolbox.javabuilder.MWCharArray.<init>(MWCharArray.java:75)
at Main.main(Main.java:11)
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1937)
at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration$MCRRoot.get(MCRConfiguration.java:70)
at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration$MCRRoot.<clinit>(MCRConfiguration.java:72)
... 8 more
Java Result: 1
First of all, are MCRROOT's and PATH's values correct? I've tried google for finding out how to set MCRROOT, but there are conflicting results: some sources say that I should include de version dir, others say the opposite. Also, why is the isInstalledMCR method returning false? I've double-checked the MCR installation (and even uninstalled and installed it to be sure), so why isn't the library finding it?
Thanks on advance for any help!
Edit: I've also tried setting MCRROOT with no version string, and it also fails.
Just wild guessing! Java is messing around with strings, while your 'mcrroot' contains white spaces. I might change the mcr install path to something like C:\MATLAB\MATLABCompilerRuntime\v710, omitting any white spaces and special characters.
I've found the solution, so I'm post a self answer for future reference: Besides adding the javabuilder.jar to the program's classpath, you also have to add the path to the MCR's runtime libraries to the java.library.path JDK parameter.
My mistake was that, instead of setting the path as the path to the libraries at the MCR installation directory (On my case, C:\MATLAB\MCR\v710\runtime\win32), I copied the runtime directory to my project's dir and used it instead. It seems that the javabuilder library uses the java.library.path variable the guess the MCROOT, what would explain the weird "StringIndexOutOfBoundsException".