简体   繁体   English

如何在Linux中打开命令终端?

[英]How to open a command terminal in Linux?

I want to open the terminal (command prompt) on a Linux machine using Java code. 我想使用Java代码在Linux机器上打开终端(命令提示符)。 I know how to open command prompt in windows.The following code i have used in windows 我知道如何在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);

In Linux, there are a number of terminal emulators which allow you to interact with various shells . 在Linux中,有许多终端模拟器允许您与各种shell进行交互。 Each shell is basically a command interpreter that understands Linux commands (GNU & Unix commands is more correct I suppose...). 每个shell基本上都是一个了解Linux命令的命令解释器(我猜想GNU和Unix命令更正确......)。 A terminal emulator provides an interface (window) for the shell and some other facilities for using the command prompt. 终端仿真器为shell提供了一个接口(窗口)以及一些其他使用命令提示符的工具。 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). 我做的基本假设是你要打开xterm ,它几乎可以在任何系统上使用(当然安装了X)。 You might want to open another terminal emulator like rxvt, eterm, aterm, gnome-terminal or konsole. 您可能想要打开另一个终端模拟器,如rxvt,eterm,aterm,gnome-terminal或konsole。 The command string can also be modified to use different shells like zsh . 也可以修改命令字符串以使用不同的shell,如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. 更好的解决方案是接受用户首选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. 1.正如其他人已经指出的那样,xterm(或您选择的任何其他终端)可能不在指定的路径中(/ usr / bin / ...),甚至可能没有安装,所以您可能不得不使用一些花哨的命令字符串(例如:在启动之前通过grep查找通过grep获取xterm的路径),这不是一个好主意。 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. 2.我对这个答案(由ypnos)发表了评论,建议我避免使用绝对路径,而是依赖于PATH环境变量中的命令。 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. 请查看注释,因为它还指出了使用find的问题。

xterm will 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 -e option can be used to invoke either a script or another program, for example: javac. -e选项可用于调用脚本或其他程序,例如:javac。 In this case you would make an assignment like: myScript = "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. 如果您不希望窗口在执行后立即关闭,则需要" ; le_exec"部分,否则丢弃它。 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只是一个等待用户按Enter键的简单脚本。

However, if your script needs arguments, then you would need to replace the String myScript by and array of Strings. 但是,如果您的脚本需要参数,那么您需要替换字符串myScript和字符串数组。

There's no single standard "terminal" command on Linux - the ones available depend on which GUI is present (ie whether KDE, or Gnome, etc). 在Linux上没有单一的标准“终端”命令 - 可用的命令取决于存在哪个GUI(即KDE或Gnome等)。

You should be 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”可能不属于那个特定的路径......

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. 您可能希望在大多数情况下使用选项,因此请查找您想要的手册页。

There isn't actually a "the" terminal. 实际上并没有“终端”。 What you really want is shell. 你真正想要的是shell。 Most Linuxes uses BaSH as a shell, but to keep on the safe side you should restrict yourself to /bin/sh 大多数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. 编辑2:也许您的需求可以由VTE提供,VTE是用于嵌入终端的GTK +组件。

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 检查环境变量$ 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: 您可以使用以下代码从$ TERM运行应用程序:

String command = System.getenv().get("TERM");
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);

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”应该出现在大多数机器上,但我不一定会把它放在农场上。

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"; String command =“/ bin / sh”;

  • or even more 'guaranteed' - 甚至更“保证” -

String command= "sh"; String command =“sh”;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM