Starting a jar with Processbuilder does not work properly

I have the following code:

ProcessBuilder pb = new ProcessBuilder( "java", "-jar", "test.jar", Integer.toString( jobId ), Integer.toString( software ), Integer.toString( entryPoint ), application );
pb.directory( new File("/home/userName/TestBSC") );
Process proc = pb.start();

When running the jar file from my terminal using this command:

java -jar test.jar 135 3 3 appName

Then it works like a charm. The box pushes some things in my database, so I see that it works. But when I do this from my JavaServlet with the above processBuilder code, I am not getting any data in my database, and I am not getting any errors either.

However, the process itself is running, I checked it with "ps ax" in my terminal. So I wonder where is the difference here? What am I doing wrong?

Does anyone have an idea?

Edit: more Code:

ProcessBuilder pb = new ProcessBuilder( "java", "-jar", "test.jar", Integer.toString( jobId ), Integer.toString( software ), Integer.toString( entryPoint ), application );
pb.directory( new File("/home/userName/TestBSC") );
Process proc = pb.start();

System.out.println( "Job running" );
proc.waitFor(); // wait until jar is finished
System.out.println( "Job finished" );

InputStream in = proc.getInputStream();
InputStream err = proc.getErrorStream();

byte result[] = new byte[ in.available() ];
in.read( result, 0, result.length );
System.out.println( new String( result ) );

byte error[] = new byte[ err.available() ];
err.read( error, 0, error.length );
System.out.println( new String( error ) );

UPDATE:

shell- script . script java .

script :

java -jar test.jar "$1" "$2" "$3" "$4"

, . :

gnome-terminal -x java -jar test.jar "$1" "$2" "$3" "$4"

!! gnome, jar .

, , - , eclipse? . . , , .

+4
2

, , .

. - (.. Stdin, stdout, stderr) , , getOutputStream(), getInputStream() getErrorStream(). . , .

java.lang.Process Documentation

, . , , , , .

, ; process.waitFor(), , , , / , .

:

  • InputStreamConsumerThread /;

    public class InputStreamConsumerThread extends Thread
    {
      private InputStream is;
      private boolean sysout;
      private StringBuilder output = new StringBuilder();
    
      public InputStreamConsumerThread (InputStream is, boolean sysout)
      {
        this.is=is;
        this.sysout=sysout;
      }
    
      public void run()
      {
        try(BufferedReader br = new BufferedReader(new InputStreamReader(is)))
        {
          for (String line = br.readLine(); line != null; line = br.readLine())
          {
            if (sysout)
              System.out.println(line);    
            output.append(line).append("\n");
          }
        }
      }
      public String getOutput(){
        return output.toString();
      }
    }
    

    ;

    String systemProperties = "-Dkey=value";    
    ProcessBuilder pb = new ProcessBuilder( "java", systemProperties, "-jar", "test.jar", Integer.toString( jobId ), Integer.toString( software ), Integer.toString( entryPoint ), application );
    pb.directory( new File("/home/userName/TestBSC") );
    Process proc = pb.start();
    
    InputStreamConsumerThread inputConsumer = 
         new InputStreamConsumerThread(proc.getInputStream(), true);
    InputStreamConsumerThread errorConsumer = 
         new InputStreamConsumerThread(proc.getErrorStream(), true);
    
    inputConsumer.start();
    errorConsumer.start();
    
    System.out.println( "Job running" );
    proc.waitFor(); // wait until jar is finished
    System.out.println( "Job finished" );
    
    String processOutput = inputConsumer.getOutput();
    String processError = errorConsumer.getOutput();
    
    if(!processOutput.isEmpty()){
        //there were some output
    }
    
    if(!processError.isEmpty()){
        //there were some error
    }
    
  • ProcessBuilder . , - , ProcessBuilder.Redirect.INHERIT :

    ProcessBuilder pb = new ProcessBuilder( "java", "-jar", "test.jar", Integer.toString( jobId ), Integer.toString( software ), Integer.toString( entryPoint ), application );
    pb.directory( new File("/home/userName/TestBSC") );
    pb.redirectErrorStream(true); // redirect error stream to output stream
    pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
    Process proc = pb.start();
    
    System.out.println( "Job running" );
    //since process builder will handle the streams for us
    //we can call waitFor() safely
    proc.waitFor(); 
    System.out.println( "Job finished" );
    
  • . ProcessBuilder , , , .

    , -, Java. java.lang.ProcessBuilder java.lang.Process.

    - , , Java "" ( ), stdout stderr stdin? , , , .

    NuProcess JNA API- - Java- .

    NuProcess

    . JRE, Runtime.exec() ProcessBuilder. Apache Commons Exec. (YAPL).

    / stderr to stdout - API () API SLF4J

    ZT Process Executor

API Java Process, JavaWorld Runtime.exec() .

+5

?

UPDATE

:

// Java runtime
Runtime runtime = Runtime.getRuntime();
// Command
String[] command = {"java", "-jar", "test.jar", Integer.toString( jobId ), Integer.toString( software ), Integer.toString( entryPoint ), application};
// Process
Process process = runtime.exec(command, null, new File("/home/userName/TestBSC"));
0

All Articles