How to open a command terminal in Linux?

2019-01-07 19:52发布

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

String command= "cmd c/start cmd.exe"
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);

I need the same thing in Linux.

Thanks for your answers. I would like to run a sh script also.

Whether the following code works.

String command= "usr/bin/xterm myshell.sh";
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);

8条回答
干净又极端
2楼-- · 2019-01-07 20:59

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.

查看更多
forever°为你锁心
3楼-- · 2019-01-07 20:59

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";

查看更多
登录 后发表回答