如何在 Linux 中打开命令终端?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/597927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open a command terminal in Linux?
提问by user48094
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
我想使用 Java 代码在 Linux 机器上打开终端(命令提示符)。我知道如何在 windows 中打开命令提示符。我在 windows 中使用了以下代码
String command= "cmd c/start cmd.exe"
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);
I need the same thing in Linux.
我在 Linux 中需要同样的东西。
Thanks for your answers. I would like to run a sh script also.
感谢您的回答。我也想运行一个 sh 脚本。
Whether the following code works.
以下代码是否有效。
String command= "usr/bin/xterm myshell.sh";
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);
回答by Alnitak
There's no single standard "terminal" command on Linux - the ones available depend on which GUI is present (i.e. whether KDE, or Gnome, etc).
Linux 上没有单一的标准“终端”命令——可用的命令取决于存在的 GUI(即 KDE 还是 Gnome 等)。
You shouldbe able to rely on xterm
being present, but on modern Linux variants that's not the terminal of choice:
您应该能够依靠xterm
存在,但现代 Linux 变体不是首选的终端:
String command= "/usr/bin/xterm";
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);
Of course, "xterm" might not be in that particular path...
当然,“xterm”可能不在那个特定的路径中......
回答by John Nilsson
There isn't actually a "the" terminal. What you really want is shell. Most Linuxes uses BaSH as a shell, but to keep on the safe side you should restrict yourself to /bin/sh
实际上没有“the”终端。你真正想要的是shell。大多数 Linux 使用 BaSH 作为 shell,但为了安全起见,您应该将自己限制在 /bin/sh
Edit: Hmm I probably missunderstood the question. If you want an interactive terminal you should look into using a toolkit providing a component for that.
编辑:嗯,我可能误解了这个问题。如果你想要一个交互式终端,你应该考虑使用一个提供组件的工具包。
Edit2: Maybe your needs can be served by VTE which is a GTK+ component for embedding a terminal.
Edit2:也许您的需求可以通过 VTE 来满足,这是一个用于嵌入终端的 GTK+ 组件。
回答by paxdiablo
Under Gnome, it's gnome-terminal
.
在 Gnome 下,它是gnome-terminal
.
Under KDE, it's konsole
.
在 KDE 下,它是konsole
.
Or you could use the more generic terminal program xterm
.
或者您可以使用更通用的终端程序xterm
。
You'll probably want to use options with most of this, so look up the man pages for the one you want.
您可能希望在其中的大部分内容中使用选项,因此请查找所需的手册页。
回答by batbrat
In Linux, there are a number of terminal emulatorswhich 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:-
在 Linux 中,有许多终端模拟器允许您与各种shell交互。每个 shell 基本上都是一个理解 Linux 命令的命令解释器(我认为 GNU 和 Unix 命令更正确......)。终端仿真器为 shell 提供了一个界面(窗口)和一些其他使用命令提示符的工具。要打开终端窗口,您只需像这样修改命令字符串:-
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.
我所做的基本假设是您想要打开xterm,它几乎可以在任何系统上使用(当然安装了 X)。您可能想打开另一个终端模拟器,如 rxvt、eterm、aterm、gnome-terminal 或 konsole。命令字符串也可以修改为使用不同的 shell,如zsh。如果您选择的终端不存在,我建议您捕获异常并通过要求用户安装它来处理它。更好的解决方案是接受用户首选 shell 的命令行参数,或者使用用户可以更改的配置文件,以使您的脚本打开他/她选择的 shell。
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.
注意
1. 正如其他人已经指出的那样,xterm(或您选择的任何其他终端)可能不在指定的路径中(/usr/bin/...),甚至可能没有安装,因此您可能必须使用一些花哨的命令字符串(例如:在启动之前通过 grep 流水线查找以获取 xterm 的路径),这不是一个好主意。我认为最好的方法是让用户配置整个事情。
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 -
2.我对这个答案发表了评论(通过 ypnos),建议我避免使用绝对路径,而是依赖于 PATH 环境变量中的命令。我不得不说我同意。在这种情况下,命令字符串应该是 -
String command = "xterm"
Do look at the comment, because it also points out the problem with using find.
请查看评论,因为它也指出了使用 find 的问题。
回答by overstood
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.
我肯定会提供一种简单的方法来在一个可以轻松编辑的地方配置所需终端的路径。例如,可能在配置 XML 文件中。
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.
“/usr/bin/xterm”应该在大多数机器上,但我不一定会打赌它。
回答by begray
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.
- 尝试识别用户的桌面环境并确定它是默认的终端应用程序。您可以在此处阅读有关它的内容。
- 检查环境变量 $TERM
- 询问用户她的偏好并将其保存在配置文件中。
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:
找出用户的桌面环境及其默认终端可能对您来说太复杂了,所以我会使用最后两个选项。您可以使用以下代码从 $TERM 运行应用程序:
String command = System.getenv().get("TERM");
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);
回答by KevinDTimm
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";
字符串命令="/bin/sh";
- or even more 'guaranteed' -
- 甚至更多的“保证”-
String command= "sh";
字符串命令=“sh”;
回答by KevinDTimm
xtermwill most probably be available on most Linux-based operating systems and if present will be found in the path variable.
xterm很可能在大多数基于 Linux 的操作系统上可用,如果存在,将在路径变量中找到。
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 -eoption 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".
该-e选项可用于调用任意脚本或其他程序,例如:javac的。在这种情况下,您将进行如下赋值: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.
" ; le_exec"
如果您不希望窗口在执行后立即关闭,您将需要该部件,否则将其丢弃。分号用于分隔命令,而 le_exec 只是一个等待用户按 Enter 的简单脚本。
However, if your script needs arguments, then you would need to replace the String myScript by and array of Strings.
但是,如果您的脚本需要参数,那么您需要将字符串 myScript 替换为字符串数组。