ProcessBuilder执行bash脚本

我正在尝试从Java执行bash脚本,它返回错误/ bin / bash:’/ home / nika / NetBeansProjects / Parallel Framework / process-executor.sh’:没有这样的文件或目录,我正在使用ubuntu使用netbeans8& 14.04 jdk8.

这是我的代码:

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:
");

            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):
");
            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’工作区/ForDemo.java ForDemo.java ForDemo

我的脚本存在另一个问题,它不执行cd命令,并显示“ / home / nika / NetBeansProjects / Parallel Framework / workspace / ForDemo.java /”:没有这样的文件或目录

我脚本的内容是

#!/bin/bash 

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

我的目录层次结构就像

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

最佳答案

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值