From: Jeff A. <ja...@fa...> - 2018-10-01 19:02:54
|
Hi Ted. Can you put the classpath at the end like that? Last thing is usually the class. Although why is java not complaining about main? Possibly it thinks HelloWorld$py is an inner class. Or is the $py being treated as a variable="" by the shell. It hardly matters: it still has to be a class that contains a main(), and one compiled from Python does not.If you want to see what it contains, you can javap it. You can make a class you run like that, but you have to write it in Java, and reference the .py file, or maybe have the text of the program in there as a string. It would be something like this: package demo_jython; import org.python.util.PythonInterpreter; public class ListDemo { public static void main(String[] args) { PythonInterpreter interp = new PythonInterpreter(); interp.exec("print 6*7"); interp.execfile("HelloWorld.py"); } } You would then run that in the normal way for a Java program, except you need the dependencies on the path, say by putting the jython.jar or jython-standalone.jar on the class path. Jeff Allen On 30/09/2018 20:05, Ted Larson Freeman wrote: > Hi, Jeff and Adam. > > Thanks for your replies. My goal is to see if I can produce a Java > .class file using Jython, which can then be run using Java on the > command line. A coworker was able to do this with Scala in less than > 15 minutes (including installation), and I wanted to see if I could do > the same thing with Jython. (I can run Jython itself with the script > below just fine.) > > I'm still missing something, as I get the same error with either the > CLASSPATH evironment variable or when explicitly setting it on the > command line. For example: > > % ls > HelloWorld.py HelloWorld$py.class > % java HelloWorld$py -classpath /home/ted/jython2.7.0 > Error: Could not find or load main class HelloWorld > > Any further suggestions are greatly appreciated. > > Thanks! > > Ted > > PS. In case it helps, here is some info on my environment: > > % jython --version > Jython 2.7.0 > % java -version > openjdk version "1.8.0_171" > OpenJDK Runtime Environment (build > 1.8.0_171-8u171-b11-0ubuntu0.17.10.1-b11) > OpenJDK 64-Bit Server VM (build 25.171-b11, mixed mode) > % uname -srv > Linux 4.13.0-46-generic #51-Ubuntu SMP Tue Jun 12 12:36:29 UTC 2018 > > > > Sent with ProtonMail <https://protonmail.com> Secure Email. > > ‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐ > On Sunday, September 30, 2018 10:51 AM, Jeff Allen > <ja...@fa...> wrote: > >> The normal way to use Jython would be like CPython: >> >> $ jython HelloWorld.py >> >> That's with an appropriate PATH, of course, or you say explicitly >> where the launcher is as part of the command. If you just wanted to >> run the the Python code, that would be the way. The compiled >> $py.class would only ever live in memory. >> >> But maybe you know this and have something more complicated in mind. >> You can run the class you made from a Java program (class with >> main()) that you write. But you could run the .py file from a Java >> program too, and I think more easily. Either way, you need the jython >> JAR on the path as Adam says. What do you have in mind that makes you >> want to compile it as a separate step like this? >> >> >> Jeff Allen >> >> On 29/09/2018 21:13, Ted Larson Freeman via Jython-users wrote: >>> I have just installed Jython 2.7, and am looking for a simple >>> example of compiling a Python script down to a Java .class file. >>> Following a suggestion on Stack Overflow, I tried to compile a file >>> called HelloWorld.py, which contains this: >>> >>> class HelloWorld(object): >>> def hello(self): >>> print 'Hello, World!' >>> >>> if __name__ == '__main__': >>> h = HelloWorld() >>> h.hello() >>> >>> Using these Jython commands (in the same working directory): >>> >>> import py_compile >>> py_compile.compile('HelloWorld.py') >>> >>> That produces a file called HelloWorld$py.class, but when I try to >>> run it with java from the command line, I get this error: >>> >>> Error: Could not find or load main class HelloWorld >>> >>> Please let me know what commands I should use. >>> >>> Thanks. >>> >>> Ted > |