How to open a command terminal in Linux?

后端 未结 8 2163
情歌与酒
情歌与酒 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: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);
    

提交回复
热议问题