运行shell脚本从外部目录:没有这样的文件或目录 [英] Running Shell Script From External Directory: No such file or directory

查看:164
本文介绍了运行shell脚本从外部目录:没有这样的文件或目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想从Java运行shell脚本文件。我的Java的工作空间目录不是脚本目录不同。

I have a shell script file that i want to run from java. My java work space directory is different than the script's directory.

private final String scriptPath = "/home/kemallin/Desktop/";

public void cleanCSVScript() {

    String script = "clean.sh";
    try {
        Process awk = new ProcessBuilder(scriptPath + script).start();
        awk.waitFor();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

和我得到这个错误:

java.io.IOException: Cannot run program "cat /home/kemallin/Desktop/capture-03.csv | awk -F ',' '{ print $1,",", $2,",", $3,",", $4,",", $6}' > clean.csv": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047)
at ShellScript.cleanCSVScript(ShellScript.java:21)
at Main.main(Main.java:15)
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:186)
at java.lang.ProcessImpl.start(ProcessImpl.java:130)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1028)
... 2 more
java.io.FileNotFoundException: /home/kemallin/Desktop/clean.csv (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.io.FileInputStream.<init>(FileInputStream.java:101)
at java.io.FileReader.<init>(FileReader.java:58)
at CSVReader.run(CSVReader.java:25)
at Main.main(Main.java:17)

我GOOGLE了它和每一个解决方案pretty多少表明,我做了正确的事情。

I have googled it and every solution pretty much indicate that i'm doing the right thing.

我试图把脚本文件中的SRC,并在Java项目的仓,但仍然说,没有这样的文件或目录。

I have tried to put the script file in the src and in bin of the Java project but still it says no such file or dir.

我究竟做错了什么?

感谢。

推荐答案

您的程序 clean.sh 不是如Java的理解,即使底层的系统理解的可执行文件它为可执行文件。

Your program clean.sh is not an executable as Java understands it, even though the underlying system understands it as executable.

您需要告诉Java是需要什么样的shell执行你的命令。做(假设你使用庆典,它安装在 /斌/庆典

You need to tell Java what shell is needed to execute your command. Do (assuming you are using bash and it is installed at /bin/bash):

private final String scriptPath = "/home/kemallin/Desktop/";

public void cleanCSVScript() {

    String script = "clean.sh";
    try {
        Process awk = new ProcessBuilder("/bin/bash", scriptPath + script).start();
        awk.waitFor();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

这篇关于运行shell脚本从外部目录:没有这样的文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆