【问题标题】:How to run linux command from JAVA? [duplicate]如何从 JAVA 运行 linux 命令? [复制]
【发布时间】:2016-03-12 15:27:28
【问题描述】:

我想制作一个在后台运行 bash 进程的 java web 界面。那么,在 java 中,我如何运行像“ls -al”这样的 linux 命令或运行 bash 脚本。有没有可以在JAVA中调用OS命令的包或者java插件?

谢谢大家

【问题讨论】:

    标签: java linux bash plugins runcommand


    【解决方案1】:

    正如我在这里展示的那样:Compiling a C Source through javacode using gcc

    您可以使用ProcessBuilder 来做到这一点:

    String directory = "/home/user";
    String[] commands = {"ls", "-al"};
    
    ProcessBuilder pb = new ProcessBuilder();
    pb.directory(new File(directory));
    pb.command(commands);
    
    Process p = pb.start();
    
    OuputStream out = p.getOutputStream(); // to send other commands/data
    InputStream in = p.getInputStream(); // to read console response
    // process the streams
    

    【讨论】:

      【解决方案2】:

      如上图here

      您可以使用Runtime.exec() 方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-06
        • 1970-01-01
        • 2012-04-19
        • 2012-08-27
        • 2012-07-20
        • 2017-10-06
        • 1970-01-01
        相关资源
        最近更新 更多