无法使用Java执行特定的shell命令命令 - 权限正常工作

时间:2016-11-20 04:00:52

标签: java shell runtime.exec

我在使用Java执行shell命令时遇到问题。我正在调用4个命令,这些命令应该从sourceanalyzer方法中运行runFortifyScan可执行文件,并填充我创建的fprpdf个文件夹。

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;
    }
}
  1. 它适用于我尝试的其他一些命令,因此,我的executeCommand方法正在运行。
  2. 另外,我确实检查了我正在执行的可执行文件的权限,并将其提交到chmod 777,以便否定导致错误的原因(编辑:忽略该词错误,我想说,否定因为文件没有生成的原因。)
  3. -rwxrwxrwx 1用户名admin 51428 2015年3月17日sourceanalyzer

    1. 另外,我尝试在文件夹中运行简单的脚本 sourceanalyzer可执行文件所在的位置,以及可执行文件的位置 好。
    2. 我尝试过从命令行运行这些命令 Java,它按预期工作。

1 个答案:

答案 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上以“强制执行”模式运行,这就阻止了命令的执行......