0

I am using Python to execute the JVM for a specified Java class, like so:

import subprocess
output = subprocess.check_output("java MyJavaClass")

However, the Java class is not in the same directory as the Python script, so this does not work. It is in a bin directory two directories up.

So I was hoping it would be possible to do something like below, but it doesn't work:

output = subprocess.check_output("java ../../bin/MyJavaClass")

Any ideas?

asked May 1, 2012 at 11:51
1
  • What is the working directory when that Python code runs? os.getcwd() Commented May 1, 2012 at 11:55

2 Answers 2

3

You need to set the classpath, like this:

java -classpath ../../bin MyJavaClass

Please note, that if your class belongs to a certain package, you have to use the FQN (Full Qualified Name):

java -classpath ../../bin my.package.MyJavaClass
answered May 1, 2012 at 11:57
Sign up to request clarification or add additional context in comments.

Comments

1

Try

output = subprocess.check_output("java MyJavaClass", cwd="../../bin/")

When running Java, the directory structure implies a package structure, so it is required to execute java from the correct directory (unless using classpath).

answered May 1, 2012 at 12:01

1 Comment

Accepted this answer since it keeps the "directory changing" in Python. Both good answers though.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.