从 Java 代码运行 shell 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30622160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:53:16  来源:igfitidea点击:

Running a shell script from java code

javaexec

提问by Dhirendra Panwar

I am almost dead trying to sort this out.. Can someone help... please?

试图解决这个问题我快要死了..有人可以帮忙吗...拜托?

Below is the code:

下面是代码:

import java.io.*;
import java.lang.Runtime;
import java.util.*;

public class WORKBRO {  

    public static void main(String args[])
    {
        try
        {    
            String target = new String("/home/dhirendra.panwar/Desktop/test.sh");
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec(target);

        } catch (Throwable t)
        {
            t.printStackTrace();
        }
    }
}

采纳答案by hagrawal

Your code is right and I am sure you are not getting exceptions, if you read using proc.getErrorStream()you will not get anything.
Commands 100% get executed that way, having said that now thing is that you are echo'ing something and you need to read it back using BufferedReader.

你的代码是正确的,我相信你没有得到例外,如果你阅读 usingproc.getErrorStream()你不会得到任何东西。
命令 100% 以这种方式执行,已经说过现在你正在回显某些东西,你需要使用BufferedReader.

Check below example which will successfully create a directory called "stackOverflow" and print what you are echo'ing. For the putting it into a log file I am afraid that you can do it using ">", you may have to use some editor command or create file using Java.

检查下面的示例,它将成功创建一个名为“stackOverflow”的目录并打印您正在回显的内容。对于将其放入日志文件,恐怕您可以使用“>”来完成,您可能需要使用一些编辑器命令或使用 Java 创建文件。

Bottom line:Runtime.getRuntime().exec("command")is the correct and defined way to execute Unix commands or scripts from Java and it WORKS.

底线:Runtime.getRuntime().exec("command")是从 Java 执行 Unix 命令或脚本的正确且定义的方法,并且它有效。

test.sh

测试文件

#!/bin/bash
echo "hola"
mkdir stackOverflow

Test.java

测试.java

import java.io.*;
public class Test {

        public static void main(String[] args) throws Exception {
                try {
                        String target = new String("/home/hagrawal/test.sh");
// String target = new String("mkdir stackOver");
                        Runtime rt = Runtime.getRuntime();
                        Process proc = rt.exec(target);
                        proc.waitFor();
                        StringBuffer output = new StringBuffer();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                        String line = "";                       
                        while ((line = reader.readLine())!= null) {
                                output.append(line + "\n");
                        }
                        System.out.println("### " + output);
                } catch (Throwable t) {
                        t.printStackTrace();
                }
        }
}