如何从Java代码运行Unix shell脚本?

问题描述 投票:142回答:17

从Java运行Unix命令非常简单。

Runtime.getRuntime().exec(myCommand);

但是可以从Java代码运行Unix shell脚本吗?如果是,从Java代码中运行shell脚本是一个好习惯吗?

java unix shell
17个回答
171
投票

你应该真的看看Process Builder。它真的是为这种东西而建的。

ProcessBuilder pb = new ProcessBuilder("myshellScript.sh", "myArg1", "myArg2");
 Map<String, String> env = pb.environment();
 env.put("VAR1", "myValue");
 env.remove("OTHERVAR");
 env.put("VAR2", env.get("VAR1") + "suffix");
 pb.directory(new File("myDir"));
 Process p = pb.start();

2
投票

至于我,一切都必须简单。对于运行脚本只需要执行

new ProcessBuilder("pathToYourShellScript").start();

2
投票

以下是如何从Java运行Unix bash或Windows bat / cmd脚本的示例。可以在脚本上传递参数,并从脚本接收输出。该方法接受任意数量的参数。

public static void runScript(String path, String... args) {
    try {
        String[] cmd = new String[args.length + 1];
        cmd[0] = path;
        int count = 0;
        for (String s : args) {
            cmd[++count] = args[count - 1];
        }
        Process process = Runtime.getRuntime().exec(cmd);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        try {
            process.waitFor();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        while (bufferedReader.ready()) {
            System.out.println("Received from script: " + bufferedReader.readLine());
        }
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
        System.exit(1);
    }
}

在Unix / Linux上运行时,路径必须类似Unix(使用'/'作为分隔符),在Windows上运行时 - 使用'\'。 Hier是一个bash脚本(test.sh)的示例,它接收任意数量的参数并将每个参数加倍:

#!/bin/bash
counter=0
while [ $# -gt 0 ]
do
  echo argument $((counter +=1)): $1
  echo doubling argument $((counter)): $(($1+$1))
  shift
done

打电话的时候

runScript("path_to_script/test.sh", "1", "2")

在Unix / Linux上,输出是:

Received from script: argument 1: 1
Received from script: doubling argument 1: 2
Received from script: argument 2: 2
Received from script: doubling argument 2: 4

Hier是一个简单的cmd Windows脚本test.command,它计算输入参数的数量:

@echo off
set a=0
for %%x in (%*) do Set /A a+=1 
echo %a% arguments received

在Windows上调用脚本时

  runScript("path_to_script\\test.cmd", "1", "2", "3")

输出是

Received from script: 3 arguments received

1
投票

有可能,只需执行任何其他程序。只需确保您的脚本具有正确的#! (she-bang)行作为脚本的第一行,并确保对文件有执行权限。

例如,如果它是一个bash脚本,将#!/ bin / bash放在脚本的顶部,也就是chmod + x。

还有,如果它是好的做法,不是它,特别是对于Java,但如果它节省了大量时间移植大型脚本,并且你没有得到额外的报酬;)节省你的时间,执行脚本,并在您的长期待办事项列表上将移植到Java。


1
投票
  String scriptName = PATH+"/myScript.sh";
  String commands[] = new String[]{scriptName,"myArg1", "myArg2"};

  Runtime rt = Runtime.getRuntime();
  Process process = null;
  try{
      process = rt.exec(commands);
      process.waitFor();
  }catch(Exception e){
      e.printStackTrace();
  }  

1
投票

这是一个迟到的答案。但是,我想到了为了让未来的开发人员从Spring-Boot应用程序中执行shell脚本而付出的努力。

  1. 我在Spring-Boot工作,我无法从我的Java应用程序中找到要执行的文件,而且它正在抛出FileNotFoundFoundException。我必须将文件保存在resources目录中,并且必须在启动应用程序时将文件设置为在pom.xml中进行扫描,如下所示。 <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>**/*.xml</include> <include>**/*.properties</include> <include>**/*.sh</include> </includes> </resource> </resources>
  2. 之后,我在执行文件时遇到了麻烦,而且它正在返回error code = 13, Permission Denied。然后我必须通过运行此命令使文件可执行 - chmod u+x myShellScript.sh

最后,我可以使用以下代码片段执行该文件。

public void runScript() {
    ProcessBuilder pb = new ProcessBuilder("src/main/resources/myFile.sh");
    try {
        Process p;
        p = pb.start();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

希望能解决某人的问题。


0
投票

就像它运行的Solaris 5.10一样,这个./batchstart.sh有一个技巧我不知道如果你的操作系统接受它使用\\. batchstart.sh而不是。这个双斜线可能有所帮助。


0
投票

我想

System.getProperty("os.name"); 

如果支持,则检查操作系统可以管理shell / bash脚本。如果需要使代码可移植。


0
投票

用于linux

public static void runShell(String directory, String command, String[] args, Map<String, String> environment)
{
    try
    {
        if(directory.trim().equals(""))
            directory = "/";

        String[] cmd = new String[args.length + 1];
        cmd[0] = command;

        int count = 1;

        for(String s : args)
        {
            cmd[count] = s;
            count++;
        }

        ProcessBuilder pb = new ProcessBuilder(cmd);

        Map<String, String> env = pb.environment();

        for(String s : environment.keySet())
            env.put(s, environment.get(s));

        pb.directory(new File(directory));

        Process process = pb.start();

        BufferedReader inputReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        BufferedWriter outputReader = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
        BufferedReader errReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));

        int exitValue = process.waitFor();

        if(exitValue != 0) // has errors
        {
            while(errReader.ready())
            {
                LogClass.log("ErrShell: " + errReader.readLine(), LogClass.LogMode.LogAll);
            }
        }
        else
        {
            while(inputReader.ready())
            {
                LogClass.log("Shell Result : " + inputReader.readLine(), LogClass.LogMode.LogAll);
            }
        }
    }
    catch(Exception e)
    {
        LogClass.log("Err: RunShell, " + e.toString(), LogClass.LogMode.LogAll);
    }
}

public static void runShell(String path, String command, String[] args)
{
    try
    {
        String[] cmd = new String[args.length + 1];

        if(!path.trim().isEmpty())
            cmd[0] = path + "/" + command;
        else
            cmd[0] = command;

        int count = 1;

        for(String s : args)
        {
            cmd[count] = s;
            count++;
        }

        Process process = Runtime.getRuntime().exec(cmd);

        BufferedReader inputReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        BufferedWriter outputReader = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
        BufferedReader errReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));

        int exitValue = process.waitFor();

        if(exitValue != 0) // has errors
        {
            while(errReader.ready())
            {
                LogClass.log("ErrShell: " + errReader.readLine(), LogClass.LogMode.LogAll);
            }
        }
        else
        {
            while(inputReader.ready())
            {
                LogClass.log("Shell Result: " + inputReader.readLine(), LogClass.LogMode.LogAll);
            }
        }
    }
    catch(Exception e)
    {
        LogClass.log("Err: RunShell, " + e.toString(), LogClass.LogMode.LogAll);
    }
}

并用于;

ShellAssistance.runShell("", "pg_dump", new String[]{"-U", "aliAdmin", "-f", "/home/Backup.sql", "StoresAssistanceDB"});

要么

ShellAssistance.runShell("", "pg_dump", new String[]{"-U", "aliAdmin", "-f", "/home/Backup.sql", "StoresAssistanceDB"}, new Hashmap<>());

23
投票

我想说从Java运行shell脚本不符合Java的精神。 Java应该是跨平台的,运行shell脚本会将其用途限制在UNIX。

话虽如此,绝对可以从Java中运行shell脚本。您使用的是与您列出的完全相同的语法(我自己没有尝试过,但是尝试直接执行shell脚本,如果这不起作用,请执行shell本身,将脚本作为命令行参数传递) 。


21
投票

我想你已回答了自己的问题

Runtime.getRuntime().exec(myShellScript);

至于它是否是一个好习惯......你想用一个你不能用Java做的shell脚本做什么?


20
投票

你也可以使用Apache Commons exec library

示例:

package testShellScript;

import java.io.IOException;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;

public class TestScript {
    int iExitValue;
    String sCommandString;

    public void runScript(String command){
        sCommandString = command;
        CommandLine oCmdLine = CommandLine.parse(sCommandString);
        DefaultExecutor oDefaultExecutor = new DefaultExecutor();
        oDefaultExecutor.setExitValue(0);
        try {
            iExitValue = oDefaultExecutor.execute(oCmdLine);
        } catch (ExecuteException e) {
            System.err.println("Execution failed.");
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("permission denied.");
            e.printStackTrace();
        }
    }

    public static void main(String args[]){
        TestScript testScript = new TestScript();
        testScript.runScript("sh /root/Desktop/testScript.sh");
    }
}

为了进一步参考,还给出了Apache Doc的一个例子。


11
投票

是的,有可能这样做。这对我有用。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.omg.CORBA.portable.InputStream;

public static void readBashScript() {
        try {
            Process proc = Runtime.getRuntime().exec("/home/destino/workspace/JavaProject/listing.sh /"); //Whatever you want to execute
            BufferedReader read = new BufferedReader(new InputStreamReader(
                    proc.getInputStream()));
            try {
                proc.waitFor();
            } catch (InterruptedException e) {
                System.out.println(e.getMessage());
            }
            while (read.ready()) {
                System.out.println(read.readLine());
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

4
投票

这是我的例子。希望它有意义。

public static void excuteCommand(String filePath) throws IOException{
    File file = new File(filePath);
    if(!file.isFile()){
        throw new IllegalArgumentException("The file " + filePath + " does not exist");
    }
    if(this.isLinux()){
        Runtime.getRuntime().exec(new String[] {"/bin/sh", "-c", filePath}, null);
    }else if(this.isWindows()){
        Runtime.getRuntime().exec("cmd /c start " + filePath);
    }
}
public static boolean isLinux(){
    String os = System.getProperty("os.name");  
    return os.toLowerCase().indexOf("linux") >= 0;
}

public static boolean isWindows(){
    String os = System.getProperty("os.name");
    return os.toLowerCase().indexOf("windows") >= 0;
}

3
投票

是的,这是可能的,你已经回答了!关于良好实践,我认为最好从文件中启动命令,而不是直接从代码中启动命令。因此,您必须使Java在现有的.bat,.sh,.ksh ...文件中执行命令列表(或一个命令)。以下是在文件“MyFile.sh”中执行命令列表的示例:

    String[] cmd = { "sh", "MyFile.sh", "\pathOfTheFile"};
    Runtime.getRuntime().exec(cmd);

3
投票

为避免必须对绝对路径进行硬编码,可以使用以下方法查找并执行脚本(如果它位于根目录中)。

public static void runScript() throws IOException, InterruptedException {
    ProcessBuilder processBuilder = new ProcessBuilder("./nameOfScript.sh");
    //Sets the source and destination for subprocess standard I/O to be the same as those of the current Java process.
    processBuilder.inheritIO();
    Process process = processBuilder.start();

    int exitValue = process.waitFor();
    if (exitValue != 0) {
        // check for errors
        new BufferedInputStream(process.getErrorStream());
        throw new RuntimeException("execution of script failed!");
    }
}

3
投票

ZT Process Executor库是Apache Commons Exec的替代品。它具有运行命令,捕获输出,设置超时等功能。

我还没有使用它,但它看起来相当充分。

文档中的一个示例:执行命令,将stderr泵入记录器,将输出返回为UTF8字符串。

 String output = new ProcessExecutor().command("java", "-version")
    .redirectError(Slf4jStream.of(getClass()).asInfo())
    .readOutput(true).execute()
    .outputUTF8();

其文档列出了Commons Exec的以下优点:

  • 改进了流的处理 读/写流 将stderr重定向到stdout
  • 改进了超时处理
  • 改进了退出代码的检查
  • 改进的API 一个用于非常复杂的用例的衬垫 一个衬里将进程输出转换为String 访问可用的Process对象 支持异步进程(未来)
  • 使用SLF4J API改进了日志记录
  • 支持多个流程
© www.soinside.com 2019 - 2024. All rights reserved.