1

I am trying to pass scm command programmatically using java and trying to get the result of that particular command on eclipse console but i am unable to perform so,here is the approach that i followed :

public SCMCommand(final String repositoryURI, final String userId, final String password,
    final String scmtoolspath, String streamName) {
        this.repositoryURI = repositoryURI;
        this.userId = userId;
        this.password = password;
        this.scmtoolspath = scmtoolspath;
        this.streamName = streamName;
    }

    public void setCommand() throws IOException
    {
        String line;
        String tempPath1 = this.scmtoolspath + "\\scm";
        this.command = "cmd /c " + tempPath1 + " show operations -r " + this.repositoryURI + " -u "+           this.userId + " -P " + this.password + " " + this.streamName;
        System.out.println(this.command);
        ProcessBuilder processBuilder = new ProcessBuilder();

        // Run this on Windows, cmd, /c = terminate after this run
        processBuilder.command(this.command);

        try {
            Process process = processBuilder.start();

            // blocked :(
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

            int exitCode = process.waitFor();
            System.out.println("\nExited with error code : " + exitCode);
        } 
        catch (IOException e) {
            e.printStackTrace();
        } 
        catch (InterruptedException e) {
            e.printStackTrace();
        }
    }   
}

Please refer the image for my output

Your Help will be appreciated, Thanks in advance!!!!

Also the output what i am getting on my console

java.io.IOException: Cannot run program "cmd /c C:\AB_TOOLS\ALMAddons\CodeReviewMetrics\libs\SCMTools\jazz\scmtools\eclipse\scm show operations -r ****8** -u **** -P **** streamName": CreateProcess error=2, The system cannot find the file specified
Build Result: All Streams Loaded Successfully
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
    at com.bosch.airbag.StreamHistory.SCMCommand.setCommand(SCMCommand.java:52)
    at com.bosch.airbag.StreamHistory.App.main(App.java:75)
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>(ProcessImpl.java:386)
    at java.lang.ProcessImpl.start(ProcessImpl.java:137)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
    ... 2 more

Important Notes -The path of the file is correct -when i am running the given command i.e" cmd /c C:\AB_TOOLS\ALMAddons\CodeReviewMetrics\libs\SCMTools\jazz\scmtools\eclipse\scm show operations -r ****8** -u **** -P **** streamName" -its running fine on command prompt but not on eclipse ide

Rajat Krishnan
  • 111
  • 2
  • 9
  • please refer the image attached – Rajat Krishnan Dec 27 '19 at 06:19
  • I could not see it. Please use texts instead of images: https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question – vahdet Dec 27 '19 at 06:23
  • @vahdet ok,sir now i have edited as per your wish – Rajat Krishnan Dec 27 '19 at 06:37
  • 1
    I think you are using the ProcessBuilder API in a wrong way. See: https://stackoverflow.com/questions/10954194/start-cmd-by-using-processbuilder You need to pass the command and the arguments to the command in an array of strings (an not in a single string). – laborg Dec 27 '19 at 06:44
  • @laborg you mean something like this " String [] cmd= new String[] {"cmd /c ",tempPath1 ," show operation ","-r ",this.repositoryURI ," -u ",this.userId, " -P ",this.password," ",this.streamName };" ? – Rajat Krishnan Dec 27 '19 at 08:30
  • @RajatKrishnan Yes something along this line. I think the first `/c` also needs to be an argument, as it is in the page I linked above. – laborg Dec 27 '19 at 08:41
  • @laborg its throwing the same error even after putting the commands in string – Rajat Krishnan Dec 27 '19 at 08:44
  • @laborg and also in this ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/C", "start"); also in above processbuilder where should i pass my command and how because cmd.exe is to open command line prompt and /C is used to print the string command – Rajat Krishnan Dec 27 '19 at 08:48
  • @laborg also if possible can you share the code as my command is working fine on command prompt but not eclipse ide, maybe i am not able to catch what exactly you are trying to say – Rajat Krishnan Dec 27 '19 at 08:53
  • @RajatKrishnan you might also need to use double backslash \\ instead of a single one(\) when defining paths in Java for Windows. Example: `C:\\AB_TOOLS\\ALMAddons\\CodeReviewMetrics\\...` . Than at least it should find the scm file. – laborg Dec 27 '19 at 08:58
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/204924/discussion-between-rajat-krishnan-and-laborg). – Rajat Krishnan Dec 27 '19 at 08:59
  • @laborg "Cannot run program "cmd/c C:\\AB_TOOLS\\ALMAddons\\CodeReviewMetrics\\libs\\SCMTools\\jazz\\scmtools\\eclipse\scm" still the same error – Rajat Krishnan Dec 27 '19 at 09:06
  • @laborg thanks,i got the solution for this thread – Rajat Krishnan Dec 28 '19 at 18:10

0 Answers0