Compiling java from python

I am writing a script to compile a .java file from within python But the error

import subprocess def compile_java(java_file): cmd = 'javac ' + java_file proc = subprocess.Popen(cmd, shell=True) compile_java("Test.java") 

Error:

 javac is not recognized as an internal or external command windows 7 

I know how to fix a problem for CMD on windows. But how can I solve this for python? I mean: how to set the path?

+4
source share
2 answers
 proc = subprocess.Popen(cmd, shell=True, env = {'PATH': '/path/to/javac'}) 

or

 cmd = '/path/to/javac/javac ' + java_file proc = subprocess.Popen(cmd, shell=True) 
+8
source

You can also send arguments:

  variableNamePython = subprocess.Popen(["java", "-jar", "lib/JavaTest.jar", variable1, variable2, variable3], stdout=subprocess.PIPE) JavaVariableReturned = variableNamePython.stdout.read() print "The Variable Returned by Java is: " + JavaVariableReturned 

The Java code to get these variables will look like this:

 public class JavaTest { public static void main(String[] args) throws Exception { String variable1 = args[0]; String variable2 = args[1]; String variable3 = args[2]; 
+1
source

All Articles