通过java代码强制使用bash命令创建目录

通过java代码强制使用bash命令创建目录,java,linux,bash,sh,Java,Linux,Bash,Sh,我有麻烦了。我试图通过linux服务器中的java代码使用bash命令强制创建一个文件夹。我的代码如下- String command = "/root/new_scripts/makedir.sh /webroot/Own"; try { Runtime runtime = Runtime.getRuntime(); Process process = runtime.exec(command); return "true"; }

我有麻烦了。我试图通过linux服务器中的java代码使用bash命令强制创建一个文件夹。我的代码如下-

String command = "/root/new_scripts/makedir.sh /webroot/Own";
    try {
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(command);
        return "true";
    } catch (Exception ex) {
        return ex.toString();
    }
并且makedir.sh文件包含

#!/bin/bash
mkdir $1
但是它不能创建目录

并尝试使用java代码创建一个目录,如下所示-

String s = null;
    try {
        Process p = Runtime.getRuntime().exec("mkdir /webroot/Own");
        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("\n\n\nHere is the standard output of the command:\n");
        while ((s = stdInput.readLine()) != null) {
            System.out.println(s);
        }
        // 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 (Exception ex) {
        System.out.println("\n\n\nexception happened - here's what I know: ");
        ex.printStackTrace();
    }
但它给了我以下的错误-

以下是命令的标准输出:

以下是命令的标准错误(如果有):

mkdir:无法创建目录“/webroot/Own”:权限被拒绝

为什么不使用直接调用系统调用的Java函数

它可能会因为权限问题而失败。

为什么不使用直接调用系统调用的Java函数呢


它可能因为权限问题而失败。

尝试更改项目目录权限:
chmod+w/
然后使用
mkdir
命令。

尝试更改项目目录权限:
chmod+w/
然后使用
mkdir
命令。

用Java处理进程的输出不是最简单的要做的事。如果只是为了调试,我会这样修改
makedir.sh

mkdir $2 > err.txt 2>err.txt

运行java代码,然后检查err.txt-希望在其中找到类似“权限被拒绝”。对于生产代码,您希望java应用程序知道
makedir.sh
的故障。请看一篇很棒的文章,了解如何正确阅读流程流的技巧

用Java处理进程的输出不是最容易的事情。如果只是为了调试,我会这样修改
makedir.sh

mkdir $2 > err.txt 2>err.txt

运行java代码,然后检查err.txt-希望在其中找到类似“权限被拒绝”。对于生产代码,您希望java应用程序知道
makedir.sh
的故障。请看一篇很棒的文章,了解如何正确阅读流程流的技巧

如果系统调用失败,它会向您提供一个解释原因的错误代码。如果系统调用失败,它会向您提供一个解释原因的错误代码。请将进程的输出捕获到stdout和stderr,并在您的帖子中提供。提示:您不需要调用外部脚本,因为mkdir在Linux中是一个可执行文件。只需使用command
mkdir/webroot/v3custom
.String command=“mkdir/webroot/v3”/尝试{Runtime=Runtime.getRuntime();Process Process=Runtime.exec(command);}catch(Exception ex){}Trsy
strace
-ing您的Java虚拟机;你会明白为什么你的
mkdir
失败了。@Pritom:你没有像我在第一次评论中所问的那样更新你的问题。检查相同的建议、解决方案和更正确的方法。请将过程的输出捕获到stdout和stderr,并在您的帖子中提供。提示:您不需要调用外部脚本,因为mkdir在Linux中是一个可执行文件。只需使用command
mkdir/webroot/v3custom
.String command=“mkdir/webroot/v3”/尝试{Runtime=Runtime.getRuntime();Process Process=Runtime.exec(command);}catch(Exception ex){}Trsy
strace
-ing您的Java虚拟机;你会明白为什么你的
mkdir
失败了。@Pritom:你没有像我在第一次评论中所问的那样更新你的问题。签出相同的建议、解决方案和更正确的内容。这是不可能的,有没有其他想法强制创建一个类似于强制删除目录的目录?这是不可能的,有没有其他想法强制创建一个类似于强制删除目录的目录?这是为了拒绝权限,但是有没有办法强制创建文件夹?这是针对拒绝权限的,但是有没有办法强制创建文件夹?