java调用python

2016-09-23  本文已影响0人  Cindy小隐
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]
上一篇下一篇

猜你喜欢

热点阅读