2

I have made a java class that calls other java classes and then prints to a screen. i am trying to build a python interface that calls my java class, runs the one method within it, and terminates. i cannot get access to my method. i have used the line "import directory.directory2.myClass.java (and .jar and .class) i made the .jar file from both the raw code and from the .class file. none of these seem to be working. i set sys.path.append to point to the directory where the java files are. Do i need to convert my java class file to a python module? and if so how do i do that?

2 Answers 2

4

Jython supports loading Java classes as if they were Python modules. It searches the directories in sys.path for .class files.

First, make sure your Java class has already been compiled with javac.

Then, do a sys.path.append(d), where d is the directory containing the package. So if your class says package foo.bar; at the top, and resides at mydir/foo/bar/myclass.java, then you must have mydir in sys.path (NOT one of the subdirs of mydir).

Finally, import the class via something like from foo.bar import myclass. The names between must match up between Python and Java! You'll be doing "from [package] import [class]".

Sign up to request clarification or add additional context in comments.

3 Comments

it still is saying no module named directory.directory2, i made sure the path was set to the directory where i have my jython file, i even tried both with and without the sys.path.append. it wont find the directory
You must, as I said, set sys.path to contain the folder which holds directory. You also have to make sure that myClass is in package directory.directory2.
Does this somehow not work if the package happens to start with com.?
0

You should do this:

from directory.directory2 import myClass
myObject = myClass()
myObject.myMethod()

5 Comments

is that the .java file .class file or a .py file
i am also getting an error saying "from directory.directory2 import myClass" ImportError: no module named directory.directory2
that is the .py file you should have myClass in the CLASSPATH
i do not follow. i set sys.path.append to point to where the java classes are. myClass is a .java file. how do i then make that into a .py file to be called by my main python program
you have to your .java compiled with javac as said Borealid

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.