我希望python使用py4j调用Java方法,当我将Java文件打包为jar并在Windows中运行时,它出错了:
>java -jar spark-1.0-SNAPSHOT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: py4j/GatewayServer
at com.littlely.AddNum.main(AddNum.java:11)
Caused by: java.lang.ClassNotFoundException: py4j.GatewayServer
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
java:
package com.littlely;
import py4j.GatewayServer;
public class AddNum{
public float add(float a, float b){
return a + b;
}
public static void main(String[] args) {
GatewayServer server = new GatewayServer(new AddNum());
server.start();
System.out.println("server started!");
}}
python:
from py4j.java_gateway import JavaGateway
def main():
jvm = JavaGateway().jvm
myadd = jvm.com.littlely.AddNum()
print(myadd.add(40.0, 80.0))
if __name__ == "__main__":
main()
我已经将主类添加到manifest.MF文件中,这怎么了?我该如何处理它,而我是java的新手,请举个例子。