1
votes

I need to write a java program which when executed pushes a command into the terminal

I tried using runtime.exec(); but not working fine for me

what i want is "/home/raj/Desktop/java -jar test.jar" to be executed in terminal

Can any one help me to sort it out.

3
Wouldn't it be more convenient to import the jar you are trying to execute and execute the main method from Java?Eugenio Cuevas
I tried using runtime.exec(); can you please expand on this?npinti
Runtime.exec() is the way to do it. Why is it failing for you? Are you getting any exceptiion?Dandy
String test = "/home/raj/Desktop"; Process p =Runtime.getRuntime().exec(test); only this i wantRaj
yes while using "cd" comment some exception is thrown why is it...can u give the correct command for using cd /home/raj/DesktopRaj

3 Answers

7
votes

If you want to actually start a terminal window (rather than just executing the java process) you will need to launch xterm (or something similar) and tell xterm to run java for example

String command= "/usr/bin/xterm -e /home/raj/Desktop/java -jar test.jar"; 
Runtime rt = Runtime.getRuntime();      
Process pr = rt.exec(command);
1
votes

Please refer following example .with list of arguments to java program.

Process proc = null;
try {
    String cmd[] = {"gnome-terminal", "-x", "bash", "-c", "ls; echo '<enter>'; read" };


    proc = Runtime.getRuntime().exec(cmd, null, wd);
} catch (IOException e) {
    e.printStackTrace();
}
1
votes

You can use full path of the jar file as an argument to "java"

String command= "java -jar /home/raj/Desktop/test.jar"; 
Runtime rt = Runtime.getRuntime();      
Process pr = rt.exec(command);