0

I need to write a Java code which can perform the below mentioned points:

  1. Need to go to a specified directory, say "G:\Java\Workspace".
  2. Once reached this directory, need to execute "sonar-runner" command.

Please note that I have to write the code in such a way that the execution of the mentioned points should be through command prompt.

I am getting the below mentioned error msg. Also find the code snippet which I have used. Can anyone just look at this and let me know the mistake that I am doing here?

I am getting the below Exception:

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    

My code is :

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();
        }
    }
}
G. Ann - SonarSource Team
  • 22,346
  • 4
  • 40
  • 76
Nitish
  • 1
  • 2

1 Answers1

0

You're providing a relative path to the executable (sonar-runner), and it isn't found:

The system cannot find the file specified

This is a question of the $PATH value inherited by your child process. I suspect you'll find it empty. Other answers exist to tell you how to supply it.

Community
  • 1
  • 1
G. Ann - SonarSource Team
  • 22,346
  • 4
  • 40
  • 76