我正在尝试使用jython 2.7.0在python中使用2个Java定义的类但在我运行我的脚本之后它会抛出java.lang.NoClassDefFoundError
这是我的2个Java类:
Hello.java
package hello;
public class Hello {
public void printMsg(){
System.out.println("check out this cool message");
}
}
Hello2.java
package hello;
public class Hello2 {
public void printHelloMsg(){
Hello msg = new Hello();
msg.printMsg();
}
}
我用它们编译了它们:
hello$ javac Hello.java Hello2.java
然后用:
创建了一个jar文件hello$ jar cf hello-jar.jar *.class
我的python脚本应该使用Hello2类,它使用Hello类:
hello.py
import Hello2
h = Hello2()
h.printHelloMsg()
用jython运行我的脚本后使用:
hello$ jython -J-cp ./hello-jar.jar hello.py
Traceback (most recent call last):
File "hello.py", line 1, in <module>
import Hello2
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:411)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at org.python.core.Py.loadAndInitClass(Py.java:991)
at org.python.core.Py.findClassInternal(Py.java:926)
at org.python.core.Py.findClassEx(Py.java:977)
at org.python.core.packagecache.SysPackageManager.findClass(SysPackageManager.java:133)
at org.python.core.packagecache.PackageManager.findClass(PackageManager.java:33)
at org.python.core.packagecache.SysPackageManager.findClass(SysPackageManager.java:122)
at org.python.core.PyJavaPackage.__findattr_ex__(PyJavaPackage.java:134)
at org.python.core.PyObject.__findattr__(PyObject.java:946)
at org.python.core.packagecache.PackageManager.lookupName(PackageManager.java:141)
at org.python.core.JavaImporter.find_module(JavaImporter.java:40)
at org.python.core.JavaImporter.find_module(JavaImporter.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
java.lang.NoClassDefFoundError: java.lang.NoClassDefFoundError: Hello2 (wrong name: hello/Hello2)
这可能是什么问题? 提前致谢
答案 0 :(得分:0)
问题出在文件hello.py中。 import语句不正确;它应该是:
from hello import Hello2
而不是
import Hello2