I want to run a command in Termninal(Ubuntu) using java program
After surfing in internet i found out way to execute commands using java
Following is the code to find ldd version in ubuntu
String[] command = { "ldd", "--version" };
ProcessBuilder probuilder = new ProcessBuilder(command);
Process process = probuilder.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
process.waitFor();
// System.out.println(br.readLine());
while ((line = br.readLine()) != null) {
System.out.println(line);
}
In the above code i trying to find out "ldd verison" using java in ubuntu. which worked fine i am got ldd version.
But when i tried to find java version of my ubuntu in the same way. The code is returning nothing because br.readline is null. iwas abel to find out ldd vesion, why not java version?
Following is the code to find out java version in ubuntu using java
String[] command = { "java", "-version" };
ProcessBuilder probuilder = new ProcessBuilder(command);
Process process = probuilder.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
process.waitFor();
// System.out.println(br.readLine());
while ((line = br.readLine()) != null) {
System.out.println(line);
}
Some one please help me to find out how to find java version using java code in ubuntu.
Unfortunately this is the way Java was implemented. It appears that Java's -version
option gets printed through the stderr
instead of the usual stdout
Reference: Why does 'java -version' go to stderr?
This phenomenon explains why your subprocess ran the command but seem everything is printed through the stderr channel it seems as thou it didn't failed executing the command but when you dump the error stream through process.getErrorStream()
you see the version text instead.
You should be able to get the Java version number through the ' java.version ' system property.
Example:
System.out.println( System.getProperty("java.version") );
Output:
1.8.0_51
You might be interested in the other Java related properties that you can print - like runtime and product build version etc.
See: http://www.oracle.com/technetwork/java/javase/versioning-naming-139433.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.