4

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.
Mia Clarke
  • 8,134
  • 3
  • 49
  • 62
Dr. Cyanide
  • 520
  • 1
  • 7
  • 21
  • Could you post a simplified version of your code to make it easier for us to help you? – Mia Clarke Mar 15 '13 at 20:01
  • For understanding the basics, my code is literally 'print "Hello World!"'. My goal is to understand the basics, then adapt it to my real project after I understand it. My reasoning for mentioning the full project is in case there's some preferred method of using Jython in similar situations. – Dr. Cyanide Mar 15 '13 at 20:42
  • Ah. I see. So what main is it that Jython says it can't find? Could you post the error message? – Mia Clarke Mar 15 '13 at 22:46
  • 2
    OK, added the error code. In looking around, I might not have jython completely installed correctly. When I try to make a Java program that uses 'import org.python.util.*' it says 'package org.python.util does not exist' – Dr. Cyanide Mar 15 '13 at 22:50

1 Answers1

2

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.

utapyngo
  • 6,946
  • 3
  • 44
  • 65