I need to write a Java code which can perform the below mentioned points:
- Need to go to a specified directory, say "
G:\Java\Workspace
". - Once reached this directory, need to execute "s
onar-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();
}
}
}