1

I have a jar named helloDemo which is simply showing a joption pane with the value 'Hello World'. It works fine. And I have another application named wrapperDemo from which's main method I want to execute this jar. Is it possible to do so in java ?!! I have tried some ways after googling but no luck. I have no idea how to do so ? Following is an attempt by adding the jar in libraries. I am using netbeans ide. Here are my attempts bellow :::

public class WrapperDemo {
public static void main(String[] args) throws IOException {        
    ProcessBuilder pb = new ProcessBuilder("/wrapperDemo/Libraries", "-jar", "helloDemo.jar");
    pb.start();
}

}

Sumon Bappi
  • 1,937
  • 8
  • 38
  • 82
  • If you want to execute the jar in the same JVM have a look here [run-a-executable-jar-file-within-java-program-using-class-loaders](http://stackoverflow.com/questions/12214678/run-a-executable-jar-file-within-java-program-using-class-loadershttp://stackoverflow.com/questions/12214678/run-a-executable-jar-file-within-java-program-using-class-loaders#12214934) – SubOptimal May 02 '15 at 08:05
  • @ChetanKinger thanks a lot. your link has a answere for me. But I can not upvote you cause you just commented. – Sumon Bappi May 02 '15 at 08:31

2 Answers2

1

Consider Apache Commons Exec to implement multiplatform launch mechanism.

String line = "java -jar /wrapperDemo/Libraries/helloDemo.jar";
CommandLine cmdLine = CommandLine.parse(line);
DefaultExecutor executor = new DefaultExecutor();
int exitValue = executor.execute(cmdLine);
0

This snippet will help:

Runtime.getRuntime().exec("java -jar /wrapperDemo/Libraries/helloDemo.jar";
Hussein Zawawi
  • 2,907
  • 2
  • 26
  • 44
  • Oh for pity's sake: 1) Use `ProcessBuilder` 2) Break a `String` arg into `String[]` args. - This is less advanced (and more fragile) than the code in the question! – Andrew Thompson May 02 '15 at 08:40