如何使用Java运行.sh文件并获得其输出?
可能重复:如何使用Java运行.sh文件并获得其输出?,java,linux,bash,Java,Linux,Bash,可能重复: String command; String[] commandArray; command = "./new_scripts/setpermission.sh"; commandArray = new String[]{"/bin/sh", command, fileLocation, permission}; try { Proc
String command;
String[] commandArray;
command = "./new_scripts/setpermission.sh";
commandArray = new String[]{"/bin/sh", command, fileLocation, permission};
try {
Process p = Runtime.getRuntime().exec(commandArray);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我使用/root/new\u scripts/setpermission.sh中的一个参数将代码写入.sh文件,并运行它:setpermission.sh
包含:
chmod 777 $1
String command;
String[] commandArray;
command = "./new_scripts/setpermission.sh";
commandArray = new String[]{"/bin/sh", command, fileLocation, permission};
try {
Process p = Runtime.getRuntime().exec(commandArray);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
它通过键入/root/new_scripts/setpermission.sh location从linux控制台成功执行。但当我尝试使用java代码运行它时,使用:
Java完整代码:
String fileLocation = BASE_DIR + domain + SUB_DIR_CAKE + fileName;
File newFile = new File(fileLocation);
System.out.println("Permission file location: " + fileLocation);
if(newFile.exists()) {
String command;
String[] commandArray;
command = "/root/new_scripts/setpermission.sh";
File commandFile = new File(command);
if(commandFile.exists()) {
System.out.println("\n\n\n\n\nFILE EXISTS");
} else {
System.out.println("\n\n\n\n\nFILE NOT EXISTS");
}
commandArray = new String[]{"/bin/sh", command, newFile.toString()};
try {
Process p = Runtime.getRuntime().exec(commandArray);
return "HERE OK";
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
} else {
return "file not exists";
}
String command;
String[] commandArray;
command = "./new_scripts/setpermission.sh";
commandArray = new String[]{"/bin/sh", command, fileLocation, permission};
try {
Process p = Runtime.getRuntime().exec(commandArray);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
它在这里返回OK
请发布您尝试使用的Java代码。但是,通过做一些假设,您可能需要找到并使用“sh”的完全限定路径,因为Java ProcessBuilder不会使用设置的“path”环境变量。尝试以下方法:
String command;
String[] commandArray;
command = "./new_scripts/setpermission.sh";
commandArray = new String[]{"/bin/sh", command, fileLocation, permission};
try {
Process p = Runtime.getRuntime().exec(commandArray);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
知道了?我添加了java代码。所以他的答案是100%正确。。。您需要提供sh
/root/new\u scripts/setpermission的完整路径。sh提供完整路径,但不起作用。是您的。是“/root”文字还是打字错误?如果是文字,这也不起作用。您需要脚本的绝对完整路径。(符号链接没问题)我在问你是否有错别字——因为它与你现在编辑的内容相匹配,所以我假设它不是错别字。也就是说,您现在有两个问题:需要指定“chmod”的完整路径以及.sh脚本的完整路径。(“/root/…”不是绝对路径。)正确的绝对路径将以“/”开头。您是否收到“执行错误”或只是什么都没有发生?嘿,您需要知道root是我的文件夹中的一个新的_脚本。这对我不起作用,有什么建议吗?新的脚本是你的工作目录,然后你只需要给出命令“/setpermission.sh”或“setpermission.sh”没有人,我的工作目录在别处,任何地方,没有指定。那么你把你的sh文件放在哪里了???从rootString命令给出整个路径;字符串[]命令数组;command=“/root/new_scripts/setpermission.sh”;commandArray=新字符串[]{“/bin/sh”,命令,文件位置,权限};尝试{Process p=Runtime.getRuntime().exec(commandArray);返回“hereok”}catch(异常e){e.printStackTrace();返回e.getMessage();}