我在使用Java执行shell命令时遇到问题。我正在调用4个命令,这些命令应该从sourceanalyzer
方法中运行runFortifyScan
可执行文件,并填充我创建的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;
}
}
executeCommand
方法正在运行。-rwxrwxrwx 1用户名admin 51428 2015年3月17日sourceanalyzer
sourceanalyzer
可执行文件所在的位置,以及可执行文件的位置
好。答案 0 :(得分:0)
我看到你试图在命令字符串中使用shell参数引用。这在Java中不起作用,我怀疑它是你所看到的行为的根本原因。
相反,您应该手动拆分参数并将其作为String[]
传递。例如:
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 );
...
}
(即使这不是你的问题的原因,你应该修复它。你当前的方法将导致带有文字引号的参数传递给子进程。如果你试图引用的一些参数包含空格事情会更糟。)
如果以上不是问题,另一种可能性是你在带有SELinux的Linux上以“强制执行”模式运行,这就阻止了命令的执行......