1

I have a requirement to run an autosys batch job and then check for its status till its status changes to SU. I have written the following code, but it gives me the following exception:

CreateProcess error=2, The system cannot find the file specified

Code:

try {
            String[] cmds = {
                    "C:\\Folder_Path\\plink.exe -ssh username@Server -pw Password",
                    "sendevent -E FORCE_STARTJOB -j job-name"};
            Runtime r = Runtime.getRuntime();
            Process p = r.exec(cmds);
            InputStream std = p.getInputStream ();
            OutputStream out = p.getOutputStream ();
            InputStream err = p.getErrorStream ();

            Thread.sleep (5000);
            int value = 0;
            String output = "";
            if (std.available () > 0) {
                System.out.println ("STD:");
                value = std.read ();
                //System.out.print ((char) value);

                while (std.available () > 0) {
                    value = std.read ();
                    output+=(char) value;

                }
            }

            if (err.available () > 0) {
                System.out.println ("ERR:");
                value = err.read ();
                //System.out.print ((char) value);

                while (err.available () > 0) {
                    value = err.read ();
                    output+=(char) value;
                }
            }
            System.out.print (output);
            p.destroy ();
        }
        catch (Exception e) {
            e.printStackTrace ();
        }
Abhisek Mishra
  • 269
  • 3
  • 15

1 Answers1

1

Your cmd should be:

String[] cmds = {"C:\\Folder_Path\\plink.exe", "-ssh", "username@Server", "-pw", "Password", "sendevent", "-E", "FORCE_STARTJOB", "-j", "job-name"};

First argument is executable name. With your cmd, it is giving error as it is unable to find "C:\Folder_Path\plink.exe -ssh username@Server -pw Password" .
Your exe is C:\Folder_Path\plink.exe, so it should be first argument.

NumeroUno
  • 1,100
  • 2
  • 14
  • 34
  • Thanks. I am not getting error anymore. But I am not getting any output too. What if I want to run the SENDEVENT command only after the first command is successful. – Abhisek Mishra Aug 22 '18 at 14:51
  • First thing, you should use ProcessBuilder, it is more robust. I think you can send SENDEVENT on the input stream of sub-process i.e. p.getOutputStream() will return InputStream associated with input of sub process. Obviusly, tis will work if your sub-process can handle in the input during run. – NumeroUno Aug 22 '18 at 14:57