【问题标题】:Java Execute a bash script using Java process builderJava 使用 Java 进程构建器执行 bash 脚本
【发布时间】:2014-09-08 23:58:54
【问题描述】:

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

这是我的代码:

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);
        }
}
}

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

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

我的脚本内容是

#!/bin/bash 

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

我的目录层次结构是这样的

-并行框架
-- 进程执行器.sh
-- 工作区
--- ForDemo.java(目录)
---- ForDemo.java

【问题讨论】:

    标签: java linux bash shell


    【解决方案1】:

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

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

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

    【讨论】:

    • 它执行脚本,但现在我收到此错误。 /home/nika/NetBeansProjects/Parallel Framework/process-executor.sh: line 6: cd: '/home/nika/NetBeansProjects/Parallel Framework/workspace/ForDemo.java/': No such file or directory javac: file not found: ForDemo.java Usage: javac &lt;options&gt; &lt;source files&gt; use -help for a list of possible options Error: Could not find or load main class ForDemo
    • 对不起,我没有看到你已经把你的脚本的源代码,确实,改变这一行:cd "'${WORK}/'" 为这一行:cd "'${WORK}'" 看看会发生什么
    • @DeepSidhu1313 抱歉,我对上一条评论进行了多次编辑,现在我不确定您是否收到了我的最终评论,您现在可以使用了吗?
    猜你喜欢
    • 1970-01-01
    • 2016-03-15
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    • 2013-10-22
    • 1970-01-01
    • 1970-01-01
    • 2015-02-28
    相关资源
    最近更新 更多