如何获取linux终端名称

k75qkfdt  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(633)

我想用linux中的终端执行一个命令。现在为了做到这一点,我不能硬代码命令,我做的windows。如何从java中以字符串形式编程获取终端名称?

new ProcessBuilder(new String[] {"xfce4-terminal", "--title="+windowTitle, "--hold", "-x", "java", "-jar", decodedPath, "run"}).start();

注意字符串“xfce4 terminal”。这取决于linux的发行版本。有什么可靠的方法可以获得java命令的终端exe。在我看来,它应该由system.getproperty(“os.terminal”)实现,但是,这并不存在。

eni9jsuy

eni9jsuy1#

这里有一个跨平台的方法来确定操作系统终端。支持windows、mac和linux

public static String osName = System.getProperty("os.name");
    public static String[] windows_terminals = new String[]
    {
        "cmd",
        "powershell",//seems to freak out and seems to be beta even in 2020 with all it's bugs
    };

    public static String[] mac_terminals = new String[]
    {
        "bin/bash"  
    };

    public static String[] linux_terminals = new String[]
    {
            "/usr/bin/gcm-calibrate",
            "/usr/bin/gnome-terminal",
            "/usr/bin/mosh-client",
            "/usr/bin/mosh-server",
            "/usr/bin/mrxvt",           
            "/usr/bin/mrxvt-full",        
            "/usr/bin/roxterm",          
            "/usr/bin/rxvt-unicode",        
            "/usr/bin/urxvt",             
            "/usr/bin/urxvtd",
            "/usr/bin/vinagre",
            "/usr/bin/x-terminal-emulator",
            "/usr/bin/xfce4-terminal",   
            "/usr/bin/xterm",
            "/usr/bin/aterm",
            "/usr/bin/guake",
            "/usr/bin/Kuake",
            "/usr/bin/rxvt",
            "/usr/bin/rxvt-unicode",
            "/usr/bin/Terminator",
            "/usr/bin/Terminology",
            "/usr/bin/tilda",
            "/usr/bin/wterm",
            "/usr/bin/Yakuake",
            "/usr/bin/Eterm",
            "/usr/bin/gnome-terminal.wrapper",
            "/usr/bin/koi8rxterm",
            "/usr/bin/konsole",
            "/usr/bin/lxterm",
            "/usr/bin/mlterm",
            "/usr/bin/mrxvt-full",
            "/usr/bin/roxterm",
            "/usr/bin/rxvt-xpm",
            "/usr/bin/rxvt-xterm",
            "/usr/bin/urxvt",
            "/usr/bin/uxterm",
            "/usr/bin/xfce4-terminal.wrapper",
            "/usr/bin/xterm",
            "/usr/bin/xvt"
    };

    public static String getTerminal()
    {
        String[] cmds = getTerminals(osName);
        for(String cmd : cmds)
        {
            try 
            {
                Runtime.getRuntime().exec(cmd + " cd " + System.getProperty("user.dir"));
                return cmd;
            } 
            catch (Throwable e) {}
        }
        return null;
    }

    public static String[] getTerminals(String os)
    {
        return os.contains("windows") ? windows_terminals : os.contains("mac") ? mac_terminals : os.contains("linux") ? linux_terminals : null;
    }

相关问题