从Python编译Java
我正在写一个脚本,想要在Python里面编译一个.java文件。
但是出现了这个错误:
import subprocess
def compile_java(java_file):
cmd = 'javac ' + java_file
proc = subprocess.Popen(cmd, shell=True)
compile_java("Test.java")
错误:
javac is not recognized as an internal or external command windows 7
我知道怎么在Windows的命令行(CMD)里解决这个问题。但是在Python里我该怎么做呢?
我的意思是:我该怎么设置路径呢?
2 个回答
1
你还可以发送一些参数:
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
接收这些变量的Java代码看起来会是这样的:
public class JavaTest {
public static void main(String[] args) throws Exception {
String variable1 = args[0];
String variable2 = args[1];
String variable3 = args[2];
10
proc = subprocess.Popen(cmd, shell=True, env = {'PATH': '/path/to/javac'})
或者
cmd = '/path/to/javac/javac ' + java_file
proc = subprocess.Popen(cmd, shell=True)