如何在Linux中打开命令终端? [英] How to open a command terminal in Linux?

查看:169
本文介绍了如何在Linux中打开命令终端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Java代码在Linux机器上打开终端(命令提示符)。
我知道如何在Windows中打开命令提示符。我在windows中使用了以下代码

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

我在Linux中需要相同的东西。

I need the same thing in Linux.

谢谢你的回答。我也想运行一个sh脚本。

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

以下代码是否有效。

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


推荐答案

在Linux中,有很多终端模拟器,允许您与各种 shell 进行交互。每个shell基本上都是一个了解Linux命令的命令解释器(我猜想GNU和Unix命令更正确......)。终端仿真器为shell提供了一个接口(窗口),还提供了一些使用命令提示符的工具。要打开终端窗口,您只需修改命令字符串,如下所示: -

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);
    }
}

我做的基本假设是你想要打开 xterm ,几乎在任何系统上都可用(当然安装了X)。您可能想要打开另一个终端模拟器,如rxvt,eterm,aterm,gnome-terminal或konsole。也可以修改命令字符串以使用不同的shell,如 zsh 。我建议您在您选择的终端不存在时捕获异常并通过要求用户安装它来处理它。更好的解决方案是接受用户首选shell的命令行参数,或使用用户可以更改的配置文件,使脚本打开他/她选择的shell。

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.

注意

1.正如其他人已经指出的那样,xterm(或您选择的任何其他终端)可能不在指定的路径中(/ usr / bin / ...)甚至可能没有安装,所以你可能不得不使用一些花哨的命令字符串(例如:在启动之前通过grep查找通过grep获取xterm的路径),这不是一个好主意。我认为最好的方法是让用户配置整个事情。

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.我得到了对这个答案的评论(通过ypnos),建议我避免使用绝对路径,而是依赖于PATH环境变量中的命令。我不得不说我同意。在这种情况下,命令字符串应为 -

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"

请查看评论,因为它还指出了使用find的问题。

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

这篇关于如何在Linux中打开命令终端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆