从java / Child Shell执行mount命令无法访问已安装的目录

时间:2014-04-22 10:05:04

标签: java shell centos mount

我有一个需要从java代码执行的python脚本。我使用以下命令:

 ScriptResult result = executor.executeCommand(Script,
            params.getParams(), false);

但是executor.executeCommand()会分叉一个新的shell。我之前运行了一个mount命令,并且可以从父shell访问/ mnt / folder1的内容,但是无法从新分叉的子shell访问它。

我尝试在executor.executeCommand()步骤之前使用以下命令执行mount命令:

     String cmd = new String("/bin/mount ip:/folder1 /mnt/folder1");
     Process p = new ProcessBuilder(cmd).start();

以及

    String cmd = new String("mount"); String[] arg = new
    String[]{" ip:/folder1 ,"/mnt/folder1"}; Process pr =
 Runtime.getRuntime().exec(cmd); 

这两个都会出现以下错误:

    java.io.IOException: Cannot run program "mount ip:/folder1 /mnt/folder1":
    error=2, No such file or directory                      
   at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)                      
   at Caused by: java.io.IOException: error=2, No such file or directory

我创建了一个 myscript.sh ,并将mount命令和python脚本命令复制到myscript中。我调用myscript throgh java代码,它成功了。但我应该从java代码调用mount命令,而不是通过单独的脚本。有什么方法我错过了。提前谢谢。

1 个答案:

答案 0 :(得分:1)

尝试更改

 String cmd = new String("/bin/mount ip:/folder1 /mnt/folder1");

 Process p = new ProcessBuilder(cmd).start();

String[] cmd = new String[]{"/bin/mount", "ip:/folder1", "/mnt/folder1"};

Process p = new ProcessBuilder(cmd).start();