我正在编写一个脚本来编译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解决它? 我的意思是:我如何设置路径?
答案 0 :(得分:8)
proc = subprocess.Popen(cmd, shell=True, env = {'PATH': '/path/to/javac'})
或
cmd = '/path/to/javac/javac ' + java_file
proc = subprocess.Popen(cmd, shell=True)
答案 1 :(得分: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];