2

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.

Tech Nerd
  • 822
  • 1
  • 13
  • 39
  • See http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html#getOutputStream() – Fildor Apr 30 '13 at 08:39
  • 2
    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](http://stackoverflow.com/tags/runtime.exec/info). 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 Apr 30 '13 at 08:39
  • @AndrewThompson if your comment was an answer, I'd +1 it. – Fildor Apr 30 '13 at 08:45
  • @Fildor I'm afraid it does not quite qualify as an answer (unfortunately). – Andrew Thompson Apr 30 '13 at 14:19

4 Answers4

0

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
NilsH
  • 13,705
  • 4
  • 41
  • 59
0

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.

Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
0
  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

Tech Nerd
  • 822
  • 1
  • 13
  • 39
  • 1
    "-DTOOLS_DIR=F://Net Beans Work Space//calc//dist" it means the dist folder of you containing the .jar file (change it for your path) "F://Net Beans Work Space//calc//dist//calc.jar" it means give it the whole complete path of your .jar file I'm waiting for your answer desperately please let me know if it was helpful – Tech Nerd Apr 30 '13 at 08:54
  • What does this have to do with where system.out ends up? – NilsH Apr 30 '13 at 09:01
  • @Nilsh what are you talking about sorry not getting your point – Tech Nerd Apr 30 '13 at 09:03
  • The problem is to capture the output, not executing the command. Although it would be better using `ProcessBuilder`, that's not the OPs issue. – NilsH Apr 30 '13 at 09:04
  • @Nilish For your kind information the user wants to create a process and execute a .jar ok! – Tech Nerd Apr 30 '13 at 09:05
  • If the question was to just exec the command, then I suggest the OP updates his question to reflect that, as it has nothing to do with where the output ends up. I know perfectly what execute does, thank you... – NilsH Apr 30 '13 at 09:26
0
 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

Tech Nerd
  • 822
  • 1
  • 13
  • 39