是否可以在 Java 中制作命令提示符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5804769/
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
Is it possible to make command prompt in Java?
提问by Jeel Shah
I would just like to know whether it is possible to make a command prompt in Java. My friend asked to make it, I wanted to know if it was possible or not. If it is possible, can someone suggest me some api or something? Thank you.
我只想知道是否可以在 Java 中创建命令提示符。我的朋友要求制作它,我想知道它是否可能。如果可能的话,有人可以建议我一些 api 之类的吗?谢谢你。
EDIT: I want to make it similar to windows command prompt
编辑:我想让它类似于 windows 命令提示符
EDIT 2: I would like to make a SWING GUI application and put a command prompt inside of it.
编辑 2:我想制作一个 SWING GUI 应用程序并在其中放置一个命令提示符。
回答by Alba Mendez
You can run commands in Java using the Process API. You can also getthe output and writeinput to the runned process. For more info, see this tutorial.
您可以使用 Process API 在 Java 中运行命令。您还可以获取输出并将输入写入运行的进程。有关详细信息,请参阅本教程。
But if you want to make a terminal emulator(such as those in Linux) in Java,
I recommend having a look at JCTermor JTA.
回答by SPee
You must be careful how you start it.
你必须小心你如何开始它。
If you start your program with java.exe then the console (input/output) is shown. With System.out.println("mymessage");
you can print (output) text to the console. With System.in
you can read from the console. This delegates to the java.io.Console
class (available throug System.console()
).
如果您使用 java.exe 启动程序,则会显示控制台(输入/输出)。随着System.out.println("mymessage");
您可以打印(输出)文本控制台。随着System.in
您可以从控制台读取。这委托给java.io.Console
班级(可通过System.console()
)。
If you start your program with javaw.exe, then you don't see the console. You must then create your own screen to allow input/output. This is the default on Windows.
如果使用 javaw.exe 启动程序,则不会看到控制台。然后您必须创建自己的屏幕以允许输入/输出。这是 Windows 上的默认设置。
回答by Noor Khan
yes it's possible in java
是的,在java中是可能的
your have to do some research on Java.lang & IO
你必须对 Java.lang & IO 做一些研究
回答by Reporter
Check the class java.lang.Runtime
. It provides a couple of exec()
methods.
检查类java.lang.Runtime
。它提供了几种exec()
方法。
回答by Wyzard
Java can do console I/O and it can launch processes, so yes, it's possible. You'd use methods of System.in
and System.out
to display a prompt and read commands, and a ProcessBuilderto execute programs.
Java 可以执行控制台 I/O 并且可以启动进程,所以是的,这是可能的。你会使用的方法System.in
,并System.out
显示一个提示和读取命令,和的ProcessBuilder执行程序。