2

I have a java program in which i have this code,

JOptionPane.showConfirmDialog(null, "TEST");
String pathToJar = ClassRewriter.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()+"ClassRewriter.class";
System.out.println(pathToJar);
ProcessBuilder pb = new ProcessBuilder("javaw "+pathToJar);
Process process = pb.start();

I have a java program in a class called classrewriter, and i am trying to start that program from within its main method but it doesnt seem to work.

ngrashia
  • 9,869
  • 5
  • 43
  • 58
Popgalop
  • 737
  • 2
  • 9
  • 26
  • This question might be relevant: http://stackoverflow.com/questions/1320476/execute-another-jar-in-a-java-program – Anderson Green Jun 22 '13 at 22:09
  • Possible duplicate [Executing a different Jar file from another java program](http://stackoverflow.com/a/6599961/1563878) – Gaston Flores Jun 22 '13 at 22:24
  • The command line should be more like "javax -jar pathToJar" – MadProgrammer Jun 22 '13 at 22:36
  • Read (and implement) *all* the recommendations of [When Runtime.exec() won't](http://www.javaworld.com/jw-12-2000/jw-1229-traps.html). That might solve the problem. If not, it should provide more information as to the reason it failed. Then ignore that it refers to `exec` and build the `Process` using a `ProcessBuilder`. Also break a `String arg` into `String[] args` to account for arguments which themselves contain spaces. – Andrew Thompson Jun 22 '13 at 23:02

1 Answers1

3
public int runCommand(String command) throws Exception 
    {
    Process s= Runtime.getRuntime().exec(command);
    return s.exitValue();
}

So you write:

 runCommand("java -jar "+pathToJar);

But it isn't recommend, because it isn't supported on all OSes (for example linux or mac).

ngrashia
  • 9,869
  • 5
  • 43
  • 58
barwnikk
  • 950
  • 8
  • 14