简体   繁体   English

使用Java运行时命令未运行

[英]Command is not running while running using java

I want to run a command in Termninal(Ubuntu) using java program 我想使用Java程序在Termninal(Ubuntu)中运行命令

After surfing in internet i found out way to execute commands using java 在网上冲浪后,我发现了使用Java执行命令的方法

Following is the code to find ldd version in ubuntu 以下是在ubuntu中查找ldd版本的代码

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. 在上面的代码中,我试图在ubuntu中使用Java找出“ ldd verison”。 which worked fine i am got ldd version. 工作正常,我得到了ldd版本。

But when i tried to find java version of my ubuntu in the same way. 但是,当我尝试以相同的方式找到我的ubuntu的Java版本时。 The code is returning nothing because br.readline is null. 该代码未返回任何内容,因为br.readline为空。 iwas abel to find out ldd vesion, why not java version? 我是亚伯找出ldd vesion, 为什么不是Java版本?

Following is the code to find out java version in ubuntu using java 以下是使用Java在ubuntu中查找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. 请有人帮助我找出如何在ubuntu中使用Java代码查找Java版本。

Unfortunately this is the way Java was implemented. 不幸的是,这是Java的实现方式。 It appears that Java's -version option gets printed through the stderr instead of the usual stdout 看来Java的-version选项是通过stderr而不是通常的stdout打印的

Reference: Why does 'java -version' go to stderr? 参考: 为什么“ java -version”进入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. 这种现象解释了为什么您的子进程运行命令,但是似乎所有内容都通过stderr通道打印出来了,好像您没有执行命令失败,但是当您通过process.getErrorStream()转储错误流时,您会看到版本文本。

You should be able to get the Java version number through the ' java.version ' system property. 您应该能够通过“ java.version ”系统属性获取Java版本号。

Example: 例:

System.out.println( System.getProperty("java.version") );

Output: 输出:

1.8.0_51 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. 您可能对可以打印的其他Java相关属性感兴趣,例如运行时和产品构建版本等。

See: http://www.oracle.com/technetwork/java/javase/versioning-naming-139433.html 请参阅: http : //www.oracle.com/technetwork/java/javase/versioning-naming-139433.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM