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.
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?
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.
To summarise: - I want to run a jar file that obviously displays it's output in a command prompt. - 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.
You should have a look at the ProcessBuilder and Process classes. 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?
When we started learning java at school we used
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();
精彩评论