Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I wrote a Python program that consists out of five .py script files. I want to execute the main of those python scripts from within a Java Application.

What are my options to do so? Using the PythonInterpreter doesn't work, as for example the datetime module can't be loaded from Jython (and I don't want the user to determine his Python path for those dependencies to work).

I compiled the whole folder to .class files using Jython's compileall. Can I embed these .class files somehow to execute the main file from within my Java Application, or how should I proceed?

share|improve this question
    
I thought Jython was so you could include Java tooling into Python, not the other way around... – cricket_007 Mar 1 at 0:06
    
@cricket_007 Jython is a Python implementation for the JVM. You can program primarily in either language and interoperate. – chrylis Mar 1 at 0:42

Have a look at the ProcessBuilder class in java: https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html.

The command used in the java constructor should be the same as what you would type in a command line. For example:

Process p = new ProcessBuilder("python", "myScript.py", "firstargument").start();

(the process builder does the same thing as the python subprocess module).

Have a look at running scripts through processbuilder

N.B. as for the Jython part of the question, if you go to the jython website (have a look at the FAQ section of their website www.jython.org). Check the entry "use jython from java".

share|improve this answer
    
In this case again, I'd need to assure the user has Python 2.x installed for my script to work, it's registered as "python" command, and finally I'd need to extract the script somewhere on the hard drive. That's not what I want, I'll have a look at Jython. – CrushedPixel Mar 1 at 9:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.