问题
I've just started learning Jython, and I'm having some issues with implementation. I've looked through the Demo files that come with Jython 2.5, and I'm still unsure how to implement Jython to get what I want.
Currently, I've got Java code executing a Python server as a Process. The Python server in turn starts a second section of Java code as a subprocess. I was originally going to replace the Python server with a Java NIO server, but that's given me no end of grief, and thus why I'm trying Jython. I also want to get all of this into a .jar.
I tried making a simple Python file (print "Hello World"
). It runs with Jython, but when I try to run it using java (after doing jython -m compileall
.) it says that it can't find main. I assume that I need to add something to my Python code to make it work, but I'm not sure what.
Edit: The exact error I'm getting is this-
Exception in thread "main" java.lang.NoClassDefFoundError: jythonTest
Caused by: java.lang.ClassNotFoundException: jythonTest
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
Could not find the main class: jythonTest. Program will exit.
回答1:
I think the answer is CLASSPATH.
hello.py
print "Hello"
Let's run it
> python hello.py
Hello
> jython hello.py
Hello
Compile (I used py_compile to compile single file)
> jython -m py_compile hello.py
Run with java
> java -classpath d:\P\jython253\jython.jar;. hello$py
Hello
Note the dot in classpath. It is required for java to find your compiled class in current directory.
来源:https://stackoverflow.com/questions/15441023/issues-understanding-how-to-use-jython