Reputation: 13
I'm trying to retrieve the version of python form java using ProcessBuilder
.
The command i'm using is:
{process = new ProcessBuilder("C:\\Python27\\python.exe", "-V")}
This command does not return anything.
I'm almost sure this is the correct syntax to retrieve the python version,
{process = new ProcessBuilder("C:\\Python27\\python.exe", "-h")}
returns the python help as expected, but python -V
does not return the python version.
package com.x.x.precheck.python;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Process process = null;
try {
process = new ProcessBuilder("C:\\Python27\\python.exe", "-V")
.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
System.out.printf("Output of running %s is:", Arrays.toString(args));
try {
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Upvotes: 1
Views: 205
Reputation: 845
I tested your code with other programs.It all works fine.for example i gave octave instead of the python and It printed out.Its weird.
Upvotes: 0
Reputation: 11195
It's because, strangely enough, the python2.7 version is displayed in stderr. in python version 3.4 this behaviour will change see http://bugs.python.org/issue18338
so instead of
InputStream is = process.getInputStream();
you should call
InputStream stderr = process.getErrorStream ();
Upvotes: 1