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();
}

}

2

2 Answers 2

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";
1
  • 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! May 2, 2015 at 8:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.