简体   繁体   中英

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;
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" runs some command in a new console, closing the console when the command exits.

  • gnome-terminal -e "bash -c \\"some command" ; sleep 10\\" runs some 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM