我正在尝试使用Python中的jar运行java文件。 我首先运行命令:
java -classpath ".:/Users/blablalba/jackson-all-1.9.0.jar" parseJason
它完美无缺。 然后我写了一个小的python脚本来执行java文件。 (更新:我根据以下建议做了更改。
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] change to
cmd = ['java',
'-classpath', '.:/Users/blablalba/jackson-all-1.9.0.jar',
'parseJason']
proc = subprocess.Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
stdout,stderr = proc.communicate(stdin)
compile_java('parseJason.java')
execute_java('parseJason.java', 'data.json','output_010.csv')
Data.json是输入文件名,output_010.csv是输出文件名。我的java文件通过读取scanner.system.in来获取这两个参数。
然后我运行命令:
python parseJson.py
我收到以下错误:
import org.codehaus.jackson.map.ObjectMapper;
^
parseJason.java:380: error: cannot find symbol
ObjectMapper mapper = new ObjectMapper();
^
symbol: class ObjectMapper
location: class parseJason
parseJason.java:380: error: cannot find symbol
ObjectMapper mapper = new ObjectMapper();
^
symbol: class ObjectMapper
location: class parseJason
3 errors
Traceback (most recent call last):
File "parseJson.py", line 15, in <module>
compile_java('parseJason.java')
File "parseJson.py", line 5, in compile_java
subprocess.check_call(['javac', java_file])
File "/Users/***/anaconda2/lib/python2.7/subprocess.py", line 541, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['javac', 'parseJason.java']' returned non-zero exit status 1
任何人都有我在这里缺少的想法吗?谢谢!
答案 0 :(得分:2)
Command '['java', 'parseJason.java']'
该命令应与之前的命令相同
parseJason
,而不是parseJason.java
尝试
cmd = ['java',
'-classpath', '.:/Users/blablalba/jackson-all-1.9.0.jar',
'parseJason']