无法使用Java执行特定的shell命令-权限工作正常
我在使用Java执行shell命令时遇到问题。我正在调用4个命令,它们应该从无法使用Java执行特定的shell命令-权限工作正常,java,shell,runtime.exec,Java,Shell,Runtime.exec,我在使用Java执行shell命令时遇到问题。我正在调用4个命令,它们应该从runFortifyScan方法中运行sourceanalyzer可执行文件,并填充我创建的fpr和pdf文件夹 import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Set; public class fortifyrunUtil { HashMap
runFortifyScan
方法中运行sourceanalyzer
可执行文件,并填充我创建的fpr
和pdf
文件夹
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Set;
public class fortifyrunUtil {
HashMap<String, Details> projectDetails = new HashMap();
public fortifyrunUtil() {
this.projectDetails.put( "bi-dashboard-test", new Details( "bi-dashboard-test", "testuser@123.com" ) );
}
public void runFortifyScan() {
Set<String> projects = this.projectDetails.keySet();
for ( String project : projects ) {
try {
Details details = this.projectDetails.get( project );
String command = "/Applications/HP_Fortify/HP_Fortify_SCA_and_Apps_4.30/bin/sourceanalyzer -64 -b \"" + details.projectname + "\"" + " -clean";
System.out.println( command );
String output = this.executeCommand( command );
command = "/Applications/HP_Fortify/HP_Fortify_SCA_and_Apps_4.30/bin/sourceanalyzer -64 -b \"" + details.projectname + "\"" + " -source " + "\"1.6\" " + System.getProperty( "user.dir" ) + "/" + details.projectname;
System.out.println( command );
output = this.executeCommand( command );
command = "/Applications/HP_Fortify/HP_Fortify_SCA_and_Apps_4.30/bin/sourceanalyzer -64 -b \"" + details.projectname + "\"" + " -format " + "\"fpr\" -f " + System.getProperty( "user.dir" ) + "/fpr/" + details.projectname + ".fpr -scan";
System.out.println( command );
output = this.executeCommand( command );
command = "/Applications/HP_Fortify/HP_Fortify_SCA_and_Apps_4.30/bin/ReportGenerator -template \"DeveloperWorkbook.xml\" -format \"pdf\" -f " + System.getProperty( "user.dir" ) + "/pdf/" + details.projectname + ".pdf" + " -source " + System.getProperty( "user.dir" ) + "/fpr/" + details.projectname + ".fpr";
System.out.println( command );
output = this.executeCommand( command );
} catch ( Exception details ) {
// empty catch block
System.out.println( "Error while executing fortify command for " + project );
}
}
}
private String executeCommand( String command ) {
StringBuffer output = new StringBuffer();
try {
Process p = Runtime.getRuntime().exec( command );
p.waitFor();
BufferedReader reader = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
String line = "";
while ( (line = reader.readLine()) != null ) {
output.append( String.valueOf( line ) + "\n" );
}
} catch ( Exception e ) {
e.printStackTrace();
}
return output.toString();
}
}
class Details {
String projectname;
String owner;
public Details( String projectname, String owner ) {
this.projectname = projectname;
this.owner = owner;
}
}
导入java.io.BufferedReader;
导入java.io.InputStreamReader;
导入java.util.HashMap;
导入java.util.Set;
公共类fortifyrunUtil{
HashMap projectDetails=新建HashMap();
公共fortifyrunUtil(){
this.projectDetails.put(“bi仪表板测试”),新的详细信息(“bi仪表板测试”testuser@123.com" ) );
}
公共无效runFortifyScan(){
Set projects=this.projectDetails.keySet();
用于(字符串项目:项目){
试一试{
Details Details=this.projectDetails.get(项目);
String command=“/Applications/HP\u-Fortify/HP\u-Fortify\u SCA\u和\u-Apps\u 4.30/bin/sourceanalyzer-64-b\”“+details.projectname+“\”“+”-clean”;
System.out.println(命令);
字符串输出=this.executeCommand(命令);
command=“/Applications/HP\u-Fortify/HP\u-Fortify\u-SCA\u和\u-Apps\u 4.30/bin/sourceanalyzer-64-b\”“+details.projectname+”+“+”-source“+”1.6\”“+System.getProperty(“user.dir”)+“/”+details.projectname;
System.out.println(命令);
输出=this.executeCommand(命令);
command=“/Applications/HP\u-Fortify/HP\u-Fortify\u SCA\u and\u-Apps\u 4.30/bin/sourceanalyzer-64-b\”“+details.projectname+“\”“+”-format“+”fpr\“-f”+System.getProperty(“user.dir”)+“/fpr/“+details.projectname+”.fpr-scan”;
System.out.println(命令);
输出=this.executeCommand(命令);
command=“/Applications/HP\u-Fortify/HP\u-Fortify\u SCA\u and\u-Apps\u 4.30/bin/ReportGenerator-template\“DeveloperWorkbook.xml\”-format\“pdf\”-f“+System.getProperty(“user.dir”)+”/pdf/“+details.projectname+”.pdf“+”-source”+System.getProperty(“user.dir”)+“/fpr/“+details.projectname+”;
System.out.println(命令);
输出=this.executeCommand(命令);
}捕获(异常详细信息){
//空挡块
System.out.println(“执行“+项目”的强化命令时出错);
}
}
}
私有字符串执行命令(字符串命令){
StringBuffer输出=新的StringBuffer();
试一试{
进程p=Runtime.getRuntime().exec(命令);
p、 waitFor();
BufferedReader=新的BufferedReader(新的InputStreamReader(p.getInputStream());
字符串行=”;
而((line=reader.readLine())!=null){
output.append(String.valueOf(line)+“\n”);
}
}捕获(例外e){
e、 printStackTrace();
}
返回output.toString();
}
}
课程详情{
字符串projectname;
字符串所有者;
公共详细信息(字符串项目名称、字符串所有者){
this.projectname=projectname;
this.owner=所有者;
}
}
executeCommand
方法起作用sourceanalyzer
可执行文件是
嗯我看到您试图在命令字符串中使用shell参数引号。这在Java中不起作用,我怀疑这是您看到的行为的根本原因 相反,您应该手动拆分参数并将其作为
字符串[]
传递。例如:
String BIN = "/Applications/HP_Fortify/HP_Fortify_SCA_and_Apps_4.30/bin/";
String command[] = new String[] {
"BIN + "sourceanalyzer",
"-64",
"-b",
details.projectname, // This can contain spaces!!
"-clean"};
output = this.executeCommand(command);
...
private String executeCommand(String[] command) {
...
Process p = Runtime.getRuntime().exec( command );
...
}
(即使这不是问题的原因,您也应该解决它。您当前的方法将导致将带有文字引号的参数传递给子进程。如果您尝试引用的某些参数包含空格,情况会更糟。)
如果以上不是问题所在,另一种可能是您在Linux上运行时,SELinux处于“强制”模式,这会阻止命令的执行…,“遇到问题”的意思是“[…],从而否定错误的原因。”--什么错误?请在您的问题中包含您问题的详细信息。@OldProgrammer,当我从终端运行相同的命令时,它并没有执行和生成文件(pdf),这是应该的。@ErwinBolwidt,很抱歉提到“错误”,我没有得到任何错误,只是文件(pdf)没有按预期生成。我可以通过终端生成文件,也可以打印出上面程序生成的系统命令,并将它们粘贴到终端,这意味着这些命令也是正确的。只是当我运行Java程序时,文件并没有按预期创建。