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

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

问题描述

我想在 Linux 机器上使用 Java 代码打开终端(命令提示符).我知道如何在 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 流水线查找以获取 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天全站免登陆