How to open a command terminal in Linux?

后端 未结 8 2120
情歌与酒
情歌与酒 2020-11-30 09:45

I want to open the terminal (command prompt) on a Linux machine using Java code. I know how to open command prompt in windows.The following code i have used in windows

相关标签:
8条回答
  • 2020-11-30 09:53

    since you have to assume you know almost nothing about the system you are running this on, I'd say lowest common denominator would be:

    String command= "/bin/sh";

    • or even more 'guaranteed' -

    String command= "sh";

    0 讨论(0)
  • 2020-11-30 09:56

    xterm will most probably be available on most Linux-based operating systems and if present will be found in the path variable.

    Therefore, you will need to do something like this:

    try {
        Runtime r = Runtime.getRuntime();
        String myScript = .....
        String[] cmdArray = {"xterm", "-e", myScript + " ; le_exec"};
        r.exec(cmdArray).waitFor();
    } catch (InterruptedException ex){
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    

    The -e option can be used to invoke either a script or another program, for example: javac. In this case you would make an assignment like: myScript = "javac".

    You will need the " ; le_exec" part if you do not want the window to immediately close after execution, otherwise discard it. The semi-colon is used to separate commands and le_exec is just a simple script that waits for the user to press Enter.

    However, if your script needs arguments, then you would need to replace the String myScript by and array of Strings.

    0 讨论(0)
  • 2020-11-30 09:56

    I think it would be nice if your application will open user's default terminal application.

    Unfortunately, it seems that there is no universal way to determine it.

    In my opinion the most preferrable solution would be (stop whenever you can open something):

    • try to recongnize user's desktop environment and determine it's default terminal application. You can read something about it here.
    • check environment variable $TERM
    • Ask user for her preference and save it in configuration file.

    Finding out user's desktop environment and it's default terminal might be too complicated for your purpose, so I'd use two last options. You can run application from $TERM with code like this:

    String command = System.getenv().get("TERM");
    Runtime rt = Runtime.getRuntime();
    Process pr = rt.exec(command);
    
    0 讨论(0)
  • 2020-11-30 09:57

    Under Gnome, it's gnome-terminal.

    Under KDE, it's konsole.

    Or you could use the more generic terminal program xterm.

    You'll probably want to use options with most of this, so look up the man pages for the one you want.

    0 讨论(0)
  • 2020-11-30 09:58

    I would definitely provide an easy way to configure the path to the desired terminal in a place that can be easily edited. For example maybe in a configuration XML file.

    Different distros will keep this in different places, so it's probably best to check the per distribution documentation for the platforms you're targeting (and to document how to change it).

    "/usr/bin/xterm" should be on most machines, but I wouldn't necessarily bet the farm on it.

    0 讨论(0)
  • 2020-11-30 10:02

    In Linux, there are a number of terminal emulators which allow you to interact with various shells. Each shell is basically a command interpreter that understands Linux commands (GNU & Unix commands is more correct I suppose...). A terminal emulator provides an interface (window) for the shell and some other facilities for using the command prompt. To open a terminal window, you just have to modify your command string like this:-

    import java.io.*;
    
    class TerminalLauncher
    {
        public static void main(String args[]) throws IOException
        {
            String command= "/usr/bin/xterm"; 
            Runtime rt = Runtime.getRuntime();  
            Process pr = rt.exec(command);
        }
    }
    

    The basic assumption I have made is that you want to open xterm, which is available on almost any system (with X installed of course). You might want to open another terminal emulator like rxvt, eterm, aterm, gnome-terminal or konsole. The command string can also be modified to use different shells like zsh. I suggest you catch an exception in case the terminal you chose isn't present and handle it by asking the user to install it. A better solution is to accept command line arguments for the users preferred shell or to use a configuration file which the user can change to make your script open the shell of his/her choice.

    Note
    1. As others have already pointed out, xterm (or any other terminal of your choice) may not be in the path specified (/usr/bin/...) and may not even be installed, so you might have to use some fancy command string (Ex: pipelining find through grep to get the path to xterm before launching), which isn't such a great idea. I think the best way is to let the user configure the whole thing.

    2.I got a comment on this answer (by ypnos), suggesting that I avoid using absolute paths and rather rely on the command being in the PATH environment variable. I have to say I agree. In that case, the command string should be -

    String command = "xterm"
    

    Do look at the comment, because it also points out the problem with using find.

    0 讨论(0)
提交回复
热议问题