Runtime exec on linux
I want to run a java program (jar) in an other terminal on Linux. Here is my code :
import java.io.BufferedWriter;开发者_如何学Python
import java.io.FileWriter;
import java.io.OutputStreamWriter;
public class Launcher
{
public static void main(String[] args)
{
try
{
Thread.sleep(6000);
Process p;
if( System.getProperty("os.name").toLowerCase().contains("win") )
{
BufferedWriter bw;
p = Runtime.getRuntime().exec("cmd.exe /c start java -Xms512M -Xmx512M -jar craftbukkit.jar");
bw = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
bw.write(launcher + "\r\n");
bw.flush();
}
else
{
Runtime.getRuntime().exec(launcher);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
On Windows there is no problem, the jar is launched in an other console. But on Linux the jar is launched in background and not in an other terminal.
Thx for help!
This is going to depend on the Linux distro and the user's chosen desktop environment.
You need to figure out which console / terminal emulator you are using, look at its command-line options and find the one that allows you to specify a command to be run. For example:
gnome-terminal -e "some command"
runssome command
in a new console, closing the console when the command exits.gnome-terminal -e "bash -c \"some command" ; sleep 10\"
runssome command
in a new console, waiting for 10 seconds before closing.
Other console / terminal emulators will probably do this differently ...
The final step is to use Runtime.exec(...)
or ProcessBuilder
and friends to assemble the composite command.
精彩评论