-1

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.

Chinna
  • 156
  • 1
  • 1
  • 11
  • Read the error stream to find out what is happend. maybe Java is not in PATH. – Jens Jul 15 '16 at 10:15
  • Try to print `process.getErrorStream()` in the same way as inputstream. Probably you are getting an error message in that stream. – Codebender Jul 15 '16 at 10:15
  • 1
    The only explanation I can conjure up is that `java` is not on your `PATH` variable. If so, when your Java code ran `java -version` it actually errored out. – Tim Biegeleisen Jul 15 '16 at 10:15
  • Please do update the question once you got the error message. – Samuel Toh Jul 15 '16 at 10:17
  • System.getProperty("java.specification.version") as suggested in http://stackoverflow.com/questions/2591083/getting-version-of-java-in-runtime – chf Jul 15 '16 at 10:30
  • @Codebender thanks for the reply. I printed out process.getErrorStream() i got the followoing **java version "1.7.0_72" Java(TM) SE Runtime Environment (build 1.7.0_72-b14) Java HotSpot(TM) Client VM (build 24.72-b04, mixed mode)**. java version which i was excpecting is in ErrorStream. do you know why i am getting there? – Chinna Jul 15 '16 at 10:43
  • @SamuelToh thanks for the reply. I printed out process.getErrorStream() i got the followoing java version "1.7.0_72" Java(TM) SE Runtime Environment (build 1.7.0_72-b14) Java HotSpot(TM) Client VM (build 24.72-b04, mixed mode). java version which i was excpecting is in ErrorStream. do you know why i am – Chinna Jul 15 '16 at 10:47
  • @Chinna i did some research for you and came across an earlier post which talks about Java's strange implementation for the `-version` option. See my answer. Let me know if it helps. – Samuel Toh Jul 15 '16 at 11:03

1 Answers1

2

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

Community
  • 1
  • 1
Samuel Toh
  • 18,006
  • 3
  • 24
  • 39