I am having trouble executing a shell command using Java. I am calling 4 commands which are supposed to run the sourceanalyzer
executable from within the runFortifyScan
method, and populate the fpr
and pdf
folders that I have created.
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;
}
}
- It works for some of the other commands I tried, so, my
executeCommand
method is working. - Also, I did check the permission of the executable I am executing, and have bumped it up to chmod 777, so as to negate that being the cause of the error(EDIT : ignore the word error, I meant to say, to negate that being the cause of the files not getting generated).
-rwxrwxrwx 1 username admin 51428 Mar 17 2015 sourceanalyzer
- Also, I have tried running simple scripts from within the folder
where the
sourceanalyzer
executable is, and that's working as well. - I have tried running these commands from the command-line, outside of Java, and it works as expected.