【发布时间】:2011-12-06 08:38:50
【问题描述】:
我遇到了一些麻烦。我试图通过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': 权限被拒绝
【问题讨论】:
-
请将该过程的输出捕获到 stdout 和 stderr 并在您的帖子中提供。并处理退出代码。
-
提示:您不需要调用外部脚本,因为 mkdir 是 Linux 中的可执行文件。只需使用命令
mkdir /webroot/v3custompritom。 -
String command = "mkdir /webroot/v3";// try { Runtime runtime = Runtime.getRuntime();进程 process = runtime.exec(command); } 捕捉(异常前){ }
-
Trsy
strace-ing 你的 Java VM;你就会明白为什么你的mkdir失败了。