通过Java代码执行sonar runner命令时出错

通过Java代码执行sonar runner命令时出错,java,child-process,Java,Child Process,我需要编写一个Java代码来执行以下几点: 需要转到指定的目录,例如“G:\Java\Workspace” 到达此目录后,需要执行“sonar runner”命令 请注意,我必须以这样一种方式编写代码,即应该通过命令提示符执行所提到的点 我得到下面提到的错误消息。还可以找到我使用过的代码段。 谁能看看这个,让我知道我在这里犯的错误吗 我得到以下例外情况: java.io.IOException: Cannot run program "sonar-runner" (in directory "G

我需要编写一个Java代码来执行以下几点:

  • 需要转到指定的目录,例如“
    G:\Java\Workspace
  • 到达此目录后,需要执行“s
    onar runner
    ”命令
  • 请注意,我必须以这样一种方式编写代码,即应该通过命令提示符执行所提到的点

    我得到下面提到的错误消息。还可以找到我使用过的代码段。 谁能看看这个,让我知道我在这里犯的错误吗

    我得到以下例外情况:

    java.io.IOException: Cannot run program "sonar-runner" (in directory "G:\Java\Workspace"): CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at com.nitish.swing.code.ExecuteSonar.run(SonarAutomation.java:67)
    at java.util.TimerThread.mainLoop(Unknown Source)
    at java.util.TimerThread.run(Unknown Source)
    Caused by: java.io.IOException: CreateProcess error=2, The system cannot    find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)
    ... 6 more    
    

    您正在提供可执行文件的相对路径(
    sonar runner
    ),但找不到:

    系统找不到指定的文件

    这是您的子进程继承的
    $PATH
    值的问题。我猜你会发现它是空的。告诉你如何供应

    class ExecuteSonar extends TimerTask {
        final String proj_dir = "G:\\Java\\Workspace";
        final String cmd = "sonar-runner";
        Date now;
    
        @Override
        public void run() {
            now = new Date();
            Runtime rt = Runtime.getRuntime();
            System.out.println("The Time is:" + now);
            try {
                rt.exec(cmd, null, new File(proj_dir));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }