0

I am trying to use Runtime.exec to run a class file from my java code but not able to launch the new process on Linux,The same is working on the windows..

I want or launch the process from GUI (which I am running from a jar file named Launch.jar) on a button click.

So I used the following code.

String curpath=System.getProperty("user.dir");
 Runtime.getRuntime().exec(new String[] { "/bin/bash", "-c", "java -classpath"+curpath+File.separator+" Launch.jar LaunchNewProcess" });
Yaroslav
  • 6,476
  • 10
  • 48
  • 89
milan kumar
  • 226
  • 2
  • 16

3 Answers3

1

I think you need to leave a space after -classpath

Runtime.getRuntime().exec(new String[] { "/bin/bash", "-c",
"java -classpath "+curpath+File.separator+" Launch.jar LaunchNewProcess" });

Update: try this:

Runtime.getRuntime().exec(new String[] { "/bin/bash", "-c",
"java -jar "+curpath+File.separator+"Launch.jar" });
iabdalkader
  • 17,009
  • 4
  • 47
  • 74
  • String curpath=System.getProperty("user.dir"); String cmd="java -classpath "+curpath+File.separator+"Launch.jar LauchNewProcess"; ProcessBuilder pb = new ProcessBuilder("bash", "-c", cmd); pb.start(); Still the problem is there,I am able to run using the "java -jar Launch.jar" but not directly with the jar file. – milan kumar Nov 07 '12 at 09:15
1

put a space after -classpath and also remove space on " Launch.jar"

 try{

     Runtime.getRuntime().exec(new String[] { "/bin/bash", "-c", "java -classpath "+curpath+File.separator+"Launch.jar LaunchNewProcess" });

}catch(Exception e){
   e.printStackTrace();
}
someone
  • 6,577
  • 7
  • 37
  • 60
  • Sorry that was a typo,The problem is while runnig from the jar file it is not working,if i am running the jar file as "java -jar Launch.jar" it is working fine. but when i am running the jar file without the command ,it is not working. – milan kumar Nov 07 '12 at 08:44
0

IMHO I suggest you to drop using Runtime.exec and start using ProcessBuilder

There are many examples/tutorials out there, here's one

Evan P
  • 1,767
  • 1
  • 20
  • 37