简体   繁体   中英

Runtime.exec not running but no exception

I want to use java to run a script in a certain location. The script running fine if I run it manually:

     /export/home/trace.sh param1 param2 param3 param4

This is my code to run the script from java:

     try {

      Runtime runtime = Runtime.getRuntime();
      Process proc = null;
      System.out.println("starting search");
      proc = runtime.exec( pathServer + "/trace.sh " + fullDate + " " + fullDateTo + " " + mA.substring(2) + " " + mB.substring(2));
      InputStream inputstream           =   proc.getInputStream();
      InputStreamReader inputstreamreader   =   new InputStreamReader(inputstream);
      BufferedReader bufferedreader         =   new BufferedReader(inputstreamreader);
      System.out.println("end search " + System.currentTimeMillis());
      }
     catch (Exception e)
      {
         System.out.println(e.toString());
         e.printStackTrace();
      }

If the script running, it will generate "txt.dat" in a specified location. But there's no "txt.dat" generated. I'm trying to figure out what the exception is, but no exception catch.

Is my code already correct? How do I know if it's processing the script or not? And how can I catch any exception happened during the process? Thanks.


Update 2

I add some code to print getInputStream and getErrorStream . For getInputStream , I got nothing, which means it's null. For getErrorStream , I get this as a result

         Host key verification failed

I still have no idea what this error means.

The java apps is run in server B.

Generally, the script I'm trying to invoke (in server B) will ssh to server A and run other script in server A. It will generate text.dat and then sftp to server B. When run it manually, txt.dat successfully generated and transferred to server B.


Update 3

Thanks for all help and suggestion. Turns out, my java apps running under different user. When I run it manually, I used another user. So, I have added host key from server B to server A. And it's running successfully now.

Runtime.exec doesn't throw exception if your command fails.

You get the result of execution using the process.exitValue() .

Also you can get your script output reading from the process.getInputStream() which you do create in your code but don't read from for some reason. Put some echo statements in your script to debug.

There are quite a few improvements that you can make to your code.

  1. Use ProcessBuilder to create your process, as opposed to Runtime . It provides you more options and greater control
  2. Pass each component of your command as a separate string to the builder
  3. Fully consume your output and error streams
  4. Wait for the process to actually complete

Something like this:

try {
    ProcessBuilder builder = new ProcessBuilder()
            .command("/export/home/trace.sh", param1, param2, param3, param4);
    builder.redirectErrorStream(true);

    System.out.println("starting search");
    Process proc = builder.start();
    final BufferedReader bufferedreader = new BufferedReader(
            new InputStreamReader(proc.getInputStream()));
    new Thread() {
        @Override
        public void run() {

            try {
                String line = null;
                StringBuffer buffer = new StringBuffer(2048);
                while ((line = bufferedreader.readLine()) != null) {
                    buffer.append(line);
                }
            } catch (final IOException ioe) {
                ioe.printStackTrace();
            }
            // Do something with process output, if needed
        }
    }.start();

    proc.waitFor();
    System.out.println("end search " + System.currentTimeMillis());
} catch (Exception e) {
    e.printStackTrace();
}

You can debug or print the Process.getErrorStream and Process.getInputStream to check for an error reported by the sub-process.

My guess is that you need to invoke /bin/sh as the main program and pass your script as an argument, but I'm not sure. I know that you have to do that on windows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM