简体   繁体   English

运行.jar文件以显示输出/解析命令

[英]Running a .jar file displaying output/parsing commands

I am wanting to run a .jar file and display the output (I'm assuming I would use a rich text box) however I also want to have a text box that will allow me to send commands to the running jar file as if it were running in a command prompt window. 我想运行一个.jar文件并显示输出(我假设我将使用一个富文本框),但是我还想拥有一个文本框,该文本框可以让我像正在运行的jar文件一样发送命令在命令提示符窗口中运行。

Either this or embedding the command prompt would be ideal. 这或者嵌入命令提示符都是理想的选择。 I have messed around with code and have never got it to work. 我已经弄乱了代码,却从来没有使它起作用。

I would also like to detect certain messages that the jar file returns, for example if a certain command is ran it will display a message depending on whether or not that failed, is there a way to read everything that get returned and then have an event happen if it detects this certain string? 我还想检测jar文件返回的某些消息,例如,如果运行了某个命令,它将显示一条消息,具体取决于是否失败,是否有办法读取返回的所有内容并发生事件如果检测到该特定字符串会发生什么?

How To: Execute command line in C#, get STD OUT results I have been looking at things such as that, when I say I have messed around with code, I have tried the code given in these examples (changing small parts) they never worked. 如何:在C#中执行命令行,获取STD OUT结果我一直在观察诸如此类的内容,当我说自己弄乱了代码时,我尝试了这些示例中给出的代码(更改了小部分),但它们从未起作用。

To summarise: - I want to run a jar file that obviously displays it's output in a command prompt. 概括一下:-我想运行一个jar文件,该文件显然在命令提示符下显示其输出。 - I want to hide the command prompt and display it's output in a rich text box. -我想隐藏命令提示符,并在富文本框中显示其输出。 - The jar file will return string such as 'user 1 has logged in' I want to update a labels text everytime that happens. -jar文件将返回诸如“用户1已登录”之类的字符串,我希望每次发生时都更新标签文本。

You should have a look at the ProcessBuilder and Process classes. 您应该看看ProcessBuilder和Process类。 They allow to run command line programs and read/write in the process streams. 它们允许运行命令行程序并在流程流中进行读取/写入。

Also, if your jar is known in advance, why no simply using the class as if it would be in a library and use the exposed APIs? 另外,如果您的jar是事先知道的,为什么不像在库中那样简单地使用类并使用公开的API?

When we started learning java at school we used 当我们在学校开始学习Java时,我们使用了

String myInput = JOptionPane.showInputDialog(this, "myPrompt");

and

JOptionPane.showMessageDialog(null, "A basic JOptionPane message dialog");

for input and output. 用于输入和输出。 Very simple. 很简单。

Or you can use the command-line: 或者,您可以使用命令行:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in)
String input = br.readLine();

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

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