2
votes

My problem is when to call jar file using

Runtime.getRuntime().exec() method, my .jar is not executing and showing its output

Coding is like that

public static void main(String[] args) {

  String execJar = "java -jar C:\test.jar";       

  try {

      Process p = Runtime.getRuntime().exec(execJar);

  } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
}

When I used this "java -jar C:\test.jar" in command prompt, my .jar is not executing thus not showing System.out output. Does anybody know how I can make this work?

Thanks.

4
Yet another fragile implementation of creating a Process! Do yourself a favor and go through the Java World article linked from the exec info. page. It might not solve the problem, but at least you will have more information. After that, split the arguments into a String[] and use a ProcessBuilder to create the Process.Andrew Thompson
@AndrewThompson if your comment was an answer, I'd +1 it.Fildor
@Fildor I'm afraid it does not quite qualify as an answer (unfortunately).Andrew Thompson

4 Answers

0
votes
 ProcessBuilder pb = new ProcessBuilder("java", "-Xmx1024m", "-Xms1024m",
         "-DTOOLS_DIR=C://", "-Daoi=whole", 
         "-jar", "C://calc.jar");
try {
    pb.start();
} catch (IOException ex) {
    System.out.print("EEE"+ex);
}

This is easy to follow as it has simple paths so you can try it and let it run easily

0
votes

When you execute a process from within Java, it will have it's own standard out and standard error streams in that particular process. To access those, you have to get the corresponding streams from the Process object you have created.

p.getOutputStream(); // System.out
p.getErrorStream();  // System.err
0
votes

Where do you expect the System.out to go ?.

When the process is spawned, the input/output and error streams are opened between the spawning and spawned process. You should consume the input and error (these represent the process output, despite the confusing name), otherwise your spawned process will block, waiting for the streams' contents to be consumed.

See this answer for more info.

0
votes
  ProcessBuilder pb = new ProcessBuilder("java", "-Xmx1024m", "-Xms1024m",
         "-DTOOLS_DIR=F://Net Beans Work Space//calc//dist", "-Daoi=whole", 
         "-jar", "F://Net Beans Work Space//calc//dist//calc.jar");

  pb.start();

Use this code it will surely run your jar file what you have to change is the paths in above code please reply it will work for you I will be thankful for you kind reply