无法在spring内的java进程内运行pmd

我想从 spring 作为服务在 java 进程(使用 ProcessBuilder 创建)中运行 pmd。


public class PMDService {


  private ProcessBuilder processBuilder;


  private void createProcess() {

    processBuilder = new ProcessBuilder();

    final Map<String, String> envMap = processBuilder.environment();

    String path = envMap.get("Path");

    path += "../../../../../../../static-code-analyzers/pmd/bin;";

    envMap.put("Path", path);

  }



  public String getCommand(PMDParameters params) {

    final StringJoiner command = new StringJoiner(" ");

    command.add("cmd")

    .add("/c")

    .add("pmd")

    .add("-d")

    .add(params.getSourceCodePath())

    .add("-f")

    .add(params.getOutputFormat())

    .add("-R")

    .add(params.getResultSet())

    .add(">")

    .add(params.getResultsPath());


    return command.toString();

  }



  public void runAnalyzer(PMDParameters params) throws IOException, InterruptedException {

    createProcess();

    processBuilder.command(getCommand(params));

    final Process process = processBuilder.start();

    process.waitFor();

  }



当我使用邮递员测试其余端点时,出现以下错误:


""message": "无法运行程序 \"cmd /c pmd -d C:/bootcamp/Spring/springbootcode/springbootdemo -f xml -R rulesets/java/quickstart.xml > .\": CreateProcess error=2, The系统找不到指定的文件","


在postman中输入json


{

   "sourceCodePath": "C:/bootcamp/Spring/springbootcode/springbootdemo",

    "resultsPath": ".",

    "outputFormat": "xml",

    "resultSet": "rulesets/java/quickstart.xml"

 }


慕丝7291255
浏览 119回答 1
1回答

蓝山帝景

分析在提供的代码段中,程序及其参数作为方法参数作为单个字符串值传递ProcessBuilder ProcessBuilder.command(String... command)。但事实并非如此:相反,程序及其参数应该分开传递。解决方案让我们使用该ProcessBuilder ProcessBuilder.command(List<String> command)方法。我们将适当地准备字符串列表:它将包含程序及其参数。解决方案草案:public List<String> getProgramAndArguments(PMDParameters params) {&nbsp; &nbsp; final List<String> programAndArguments = new ArrayList<String>();&nbsp; &nbsp; programAndArguments.add("cmd");&nbsp; &nbsp; programAndArguments.add("/c");&nbsp; &nbsp; programAndArguments.add("pmd");&nbsp; &nbsp; programAndArguments.add("-d");&nbsp; &nbsp; programAndArguments.add(params.getSourceCodePath());&nbsp; &nbsp; programAndArguments.add("-f");&nbsp; &nbsp; programAndArguments.add(params.getOutputFormat());&nbsp; &nbsp; programAndArguments.add("-R");&nbsp; &nbsp; programAndArguments.add(params.getResultSet());&nbsp; &nbsp; programAndArguments.add(">");&nbsp; &nbsp; programAndArguments.add(params.getResultsPath());&nbsp; &nbsp; return programAndArguments;}public void runAnalyzer(PMDParameters params) throws IOException, InterruptedException {&nbsp; &nbsp; createProcess();&nbsp; &nbsp; processBuilder.command(getProgramAndArguments(params));&nbsp; &nbsp; final Process process = processBuilder.start();&nbsp; &nbsp; process.waitFor();}此外,请通过提供正确的输入来确保params.getResultsPath()具有正确的值:现在是.。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java