美文网首页
java调用python

java调用python

作者: Cindy小隐 | 来源:发表于2016-09-23 18:03 被阅读0次
static void runpython() throws IOException, InterruptedException {
                String[] args = new String[] {"youpath2python\\python.exe","youscriptpath\\test.py","a","b", "c","d" };
                Process process = Runtime.getRuntime().exec(args);
                InputStream inputStream = process.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

                String line;
                try {
                        while ((line = bufferedReader.readLine()) != null) {
                                System.out.println(line);
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                }

        }
# python脚本test.py
import sys
print "script name:", sys.argv[0]
for i in range(1, len(sys.argv)):
    print "Parameter:", i, sys.argv[i]

相关文章

网友评论

      本文标题:java调用python

      本文链接:https://www.haomeiwen.com/subject/duntyttx.html