Java 使用 Java 进程构建器执行 bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24814966/
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-09-18 10:54:03  来源:igfitidea点击:

Java Execute a bash script using Java process builder

javalinuxbashshell

提问by DeepSidhu1313

I'm trying to execute a bash script from Java and it returns error /bin/bash: '/home/nika/NetBeansProjects/Parallel Framework/process-executor.sh': No such file or directory, I'm working on ubuntu 14.04 with netbeans8 & jdk8.

我正在尝试从 Java 执行 bash 脚本并返回错误/bin/bash: '/home/nika/NetBeansProjects/Parallel Framework/process-executor.sh': No such file or directory,我正在使用 netbeans8 和 jdk8 处理 ubuntu 14.04。

Here is my code:

这是我的代码:

public class Process {
public static void main(String[] args) {
        try {
            ProcessBuilder pb = null;
            Process p;
            String cmd2 = "";
            String workingDir = System.getProperty("user.dir");
            System.out.println(""+workingDir);
            String scriptloc="'"+workingDir+"/process-executor.sh'";
            String cmd[] = {"/bin/bash",scriptloc , "workspace/ForDemo.java", "ForDemo.java", "ForDemo"};

            for (int i = 0; i <= cmd.length-1; i++) {
                cmd2 += " "+cmd[i];
            }
            System.out.println("" + cmd2);
            pb = new ProcessBuilder(cmd);
            pb.directory(new File(workingDir));

            p = null;
            try {
                p = pb.start();
            } catch (IOException ex) {
                Logger.getLogger(Process.class.getName()).log(Level.SEVERE, null, ex);
            }

            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

            BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

            // read the output from the command
            System.out.println("Here is the standard output of the command:\n");

            String s = null;
            String output = "";
            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);


            }
            output = "";

            // read any errors from the attempted command
            System.out.println("Here is the standard error of the command (if any):\n");
            while ((s = stdError.readLine()) != null) {
                System.out.println(s);
            }
        } catch (IOException ex) {
            Logger.getLogger(Process.class.getName()).log(Level.SEVERE, null, ex);
        }
}
}

But when I execute this command from terminal it executes the script bin/bash '/home/nika/NetBeansProjects/Parallel Framework/process-executor.sh' workspace/ForDemo.java ForDemo.java ForDemo

但是当我从终端执行此命令时,它会执行脚本 bin/bash '/home/nika/NetBeansProjects/Parallel Framework/process-executor.sh' workspace/ForDemo.java ForDemo.java ForDemo

I have another problem with my script it doesn't execute the cdcommand and says '/home/nika/NetBeansProjects/Parallel Framework/workspace/ForDemo.java/': No such file or directory

我的脚本有另一个问题,它不执行cd命令并说'/home/nika/NetBeansProjects/Parallel Framework/workspace/ForDemo.java/': No such file or directory

Contents of my script are

我的脚本内容是

#!/bin/bash 

 PATH=/bin:/usr/bin:/usr/local/bin
 WORK=${PWD}/workspace/
 echo "'${WORK}'"
 cd  "'${WORK}/'"
 javac  
 java  
 echo ""

My directory hierarchy is like

我的目录层次结构就像

-Parallel Framework
-- process-executor.sh
-- workspace
--- ForDemo.java (directory)
---- ForDemo.java

-并行框架——process
- executor.sh——
工作空间
——ForDemo.java(目录)
——ForDemo.java

回答by morgano

Don't use single quotes in the path to your script in this case, i. e. fix your scriptloc variable like this:

在这种情况下,不要在脚本路径中使用单引号,即像这样修复 scriptloc 变量:

String scriptloc= workingDir + "/process-executor.sh";

Single quotes are necessary if you were executing this in the command line (to escape the space character in your path) but it is not necessary in this case as you are already specifying implicitly in your cmd[]array that such path is just one "unit"

如果您在命令行中执行此操作(以转义路径中的空格字符),则单引号是必需的,但在这种情况下没有必要,因为您已经在cmd[]数组中隐式指定此类路径只是一个“单元”