Python - Execute Java - Could not find or load main class
本问题已经有最佳答案,请猛点这里访问。
我按照SO Answer来从python执行Java程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import os.path, subprocess from subprocess import STDOUT, PIPE def compile_java(java_file): subprocess.check_call(['javac', java_file]) def execute_java(java_file, stdin): java_class, ext = os.path.splitext(java_file) cmd = ['java', java_class] process = subprocess.Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT, universal_newlines=True) stdout, stderr = process.communicate(stdin) print(stdout) compile_java(os.path.join('model', 'classification', 'Model.java')) execute_java('Model', '5 6 7 8') |
python代码编译
问题似乎是,您必须将
此python代码对我有用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import os.path,subprocess from subprocess import STDOUT,PIPE def compile_java(java_file): subprocess.check_call(['javac', java_file]) def execute_java(java_file, stdin): java_class,ext = os.path.splitext(java_file) // I changed the following line by adding the directory to the classpath cmd = ['java', '-cp', 'model/classification', java_class] proc = subprocess.Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT) stdout,stderr = proc.communicate(stdin) print ('This was"' + stdout + '"') compile_java(os.path.join('model', 'classification', 'Model.java')) execute_java('Model.java', '5 6 7 8') |
在这种情况下,目录路径相对于python脚本的执行目录!