如有这样的Python脚本
#test.py
print "hello"
python test.py
hello
Java中调用该Python脚本
ProcessBuilder builder = new ProcessBuilder();
builder.command("python", "test.py");
Process process = builder.start();
process.waitFor();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
bufferedReader.lines().forEach(System.out::println);
能正确输出hello
然后在test.py
中引入了结巴分词
# test.py
import jieba
print "hello"
print("/".join(jieba.cut("hello world")))
print("/".join(jieba.cut("你好 世界")))
#
python test.py 2>/dev/null
hello
hello/ /world
你好/ /世界
此时Java中调用该Python脚本 中文分词的结果读取不到 不知何故?
java InvokePythonCommand
hello
hello/ /world
应该是Python2对中文支持不好的缘故 改成Python3就正常了
builder.command("python3", "test.py");
此时运行Java程序
java InvokePythonCommand
hello
hello/ /world
你好/ /世界
我试过了你的代码,没有问题,能正确的输出 —— 检查下你写好 test.py 之后是否保存了。
