如何使用Java执行系统命令(linux/bsd)

发布于 2024-07-18 05:25:59 字数 729 浏览 5 评论 0原文

我试图以便宜的方式在 Java 中执行本地系统命令 (uname -a)。 我希望获取 uname 的输出并将其存储在字符串中。 这样做的最佳方法是什么? 当前代码:

public class lame {

    public static void main(String args[]) {
        try {
            Process p = Runtime.getRuntime().exec("uname -a");
            p.waitFor();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line=reader.readLine();

            while (line != null) {    
                System.out.println(line);
                line = reader.readLine();
            }

        }
        catch(IOException e1) {}
        catch(InterruptedException e2) {}

        System.out.println("finished.");
    }
}

I am attempting to be cheap and execute a local system command (uname -a) in Java. I am looking to grab the output from uname and store it in a String. What is the best way of doing this? Current code:

public class lame {

    public static void main(String args[]) {
        try {
            Process p = Runtime.getRuntime().exec("uname -a");
            p.waitFor();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line=reader.readLine();

            while (line != null) {    
                System.out.println(line);
                line = reader.readLine();
            }

        }
        catch(IOException e1) {}
        catch(InterruptedException e2) {}

        System.out.println("finished.");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

温柔嚣张 2024-07-25 05:25:59

你的方式与我可能做的事情相去不远:

Runtime r = Runtime.getRuntime();
Process p = r.exec("uname -a");
p.waitFor();
BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";

while ((line = b.readLine()) != null) {
  System.out.println(line);
}

b.close();

当然,处理你关心的任何异常。

Your way isn't far off from what I'd probably do:

Runtime r = Runtime.getRuntime();
Process p = r.exec("uname -a");
p.waitFor();
BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";

while ((line = b.readLine()) != null) {
  System.out.println(line);
}

b.close();

Handle whichever exceptions you care to, of course.

这是最好的方法。
您还可以使用 ProcessBuilder ,它有一个可变参数构造函数,因此您可以节省一两行代码

That is the best way to do it.
Also you can use the ProcessBuilder which has a variable argument constructor, so you could save a line or two of code

柳若烟 2024-07-25 05:25:59

你正在做的事情看起来不错。 如果您的命令仅返回单个字符串,则不需要 while 循环,只需将 reader.readLine() 值存储在单个 String 变量中。

另外,你可能应该对这些例外做一些事情,而不是仅仅接受它们。

What you are doing looks fine. If your command is only returning a single string, you don't need the while loop, just store the reader.readLine() value in a single String variable.

Also, you probably should do something with those exceptions, rather than just swallowing them.

抱猫软卧 2024-07-25 05:25:59

我知道这已经很老了,但仍然......

在这里阅读文章:
http://www.javaworld .com/article/2071275/core-java/when-runtime-exec---won-t.html
据我了解,您应该首先读取所执行命令的输出和错误流,然后才 waitFor 进程的返回值。

I know this is very old but still...

Reading the article here:
http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html
It is my understanding that you should first read the output and error streams of your executed command and only then waitFor the return value of your process.

江湖彼岸 2024-07-25 05:25:59

我知道这个问题很老了,但我只是想添加一些可能对某些人有用的信息。

如果您只想从 java 代码运行 uname 命令,最好使用 System 类来获取有关系统的信息。

它不仅可以消除运行终端命令的依赖性,而且还可以独立于操作系统运行。

System Class 可以为您提供以下信息

键:值

--> os.version :操作系统版本
--> os.name:操作系统名称

--> os.arch:操作系统架构

--> java.compiler :您正在使用的编译器的名称

--> java.ext.dirs :扩展目录路径

--> java.library.path :加载时搜索库的路径

--> user.dir : User 当前工作目录

--> user.name :用户的帐户名

--> java.vm.version :JVM 实现版本

--> java.vm.name :JVM 实现名称

--> java.home:Java安装目录

--> java.runtime.version:JVM版本

(来源 - https://www.geeksforgeeks.org/getproperty-and-getproperties-methods-of-system-class-in-java/)

例如:如果我运行的是基于 Linux 的系统,比如说 Ubuntu,以下命令将为我提供有关它的信息。

System.getProperty("os.name");

I know this questionis very old, but I just wanted to add some information that might come handy to some people.

If you just want to run uname command from java code, better use the System class to get information about the system.

It will not only remove the dependency of running the terminal command, but it will also work independently of the Operating System.

System Class can give you the following information

Keys : Values

--> os.version : OS Version
--> os.name : OS Name

--> os.arch : OS Architecture

--> java.compiler : Name of the compiler you are using

--> java.ext.dirs : Extension directory path

--> java.library.path : Paths to search libraries whenever loading

--> user.dir : Current working directory of User

--> user.name : Account name of User

--> java.vm.version : JVM implementation version

--> java.vm.name : JVM implementation name

--> java.home : Java installation directory

--> java.runtime.version : JVM version

(Source - https://www.geeksforgeeks.org/getproperty-and-getproperties-methods-of-system-class-in-java/)

ex : If I am running a Linux based system, Say Ubuntu, the following command will give me the information about it.

System.getProperty("os.name");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文