1

I have been trying to work on this issue I had by searching thoroughly so as to find out what to do. However, none of the results I've found (at least until now) had suited my requests.

The fact is, I've got an executable JAR I've done. This jar starts an .EXE. Now, the thing is, the EXE will keep on running the whole time, and I want to get whatever has been written in the console so as to write it on a JTextBox as soon as that is read.

Would you mind giving me an example of that? I would like to do it on my own, but my head doesn't seem to find out how.

Thank you very much.

EDIT: what I'm trying to do is a GUI for a gaming server

EDIT 2: for those saying its duplicate... wish it was... tried what the others explained but didn't work, so that's the reason I asked here..

EDIT 3: as I have been looking forward to find what the problem was, I will tell you that what I've done does not have any errors. However, I guess, it may be caused to the fact that the server (written in C++/C) may not output in a 'normal' way. May that be the reason? I hope so. Otherwise, I might be doing something really wrong.

Please notice I use InputStream in order to be able to read.. but well.

  • See [this](http://stackoverflow.com/questions/659796/run-external-program-from-java-read-output-allow-interruption), [this](http://stackoverflow.com/questions/1088941/java-reading-standard-output-from-an-external-program-using-inputstream), or maybe [this](http://stackoverflow.com/questions/11957337/read-from-another-process-output-stream). – Zong Nov 11 '13 at 21:37
  • I've tried already the first one. It doesn't work 'real-time', as console is not read. Second one is not really what I'm exactly looking for. Third one seemed to be fine, though it doesn't really work. (Although im trying to do what comments said, gonna inform you if it worked later). It needs to edit the external's EXE Source code, and I'm not available to do that. – Fernando Martín Besteiro Nov 11 '13 at 21:47
  • @fmbesteiro if you claim this is not a dup, please be more specific as to what you have tired and why **exactly** it does not work for you. what errors are you getting? – Shai Nov 12 '13 at 10:59
  • Edit #3 @Shai . Excuse any further inconveniences, I'm a newcomer. – Fernando Martín Besteiro Nov 12 '13 at 15:13

1 Answers1

0

Basically, you need to start by running the process in some kind of background thread so there is no risk that it will block the Event Dispatching Thread.

Then you need to read the processes InputStream. As the input is read, you need to push these updates to the UI in such away so as not to violate the single thread rules of Swing. That is, you should ensure that all updates are made within the context of the Event Dispatching Thread.

Check out Concurrency in Swing for more details.

In this, I would recommend using something like SwingWorker. It allows you to monitor the process from a background thread, but has easy to use functionality to sync the updates back to the EDT.

Take a look at Printing a Java InputStream from a Process for an example

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Took a look at your answer and decided to modify it so as like this: `code`ProcessBuilder pb = new ProcessBuilder("C:\\\\Archivos de Programa\\MTA Scripting\\server\\MTA Server.exe"); //pb.directory(new File("C:\\\\Archivos de Programa\\MTA Scripting\\server\\MTA Server.exe")); – Fernando Martín Besteiro Nov 11 '13 at 22:37
  • The `directory` parameter simple provides you a means by which you can change the execution context of the process, otherwise it will be executed within the current processes working directory. – MadProgrammer Nov 11 '13 at 22:39
  • Excuse if I'm just really bad, the fact is that I am trying to learn so as not to disturb you helper... The thing is I tried this `code`ProcessBuilder pb = new ProcessBuilder("MTA Server.exe"); pb.directory(new File("C:\\\\Archivos de Programa\\MTA Scripting\\server")); and it throws IOException saying file cannot be executed – Fernando Martín Besteiro Nov 11 '13 at 22:41
  • After doing a workaround on that (creating external jar file and executing in that directory) the text just shows 'Starting...'. – Fernando Martín Besteiro Nov 11 '13 at 22:54
  • Tested it and seems to work fine for me. 1- Is the executable actually outputting anything (and not expecting some input) 2- And did it fail to start?? – MadProgrammer Nov 11 '13 at 23:08
  • Hey, in fact, whenever I launch it normally, the console application outputs some messages (and keeps running as its a server). As I said, what I need is to READ what is being written in the console in real time (like moving the console into a GUI for instance). In this case it was like it started but then never wrote anything, and it did start, as I can see it in tasklist active. – Fernando Martín Besteiro Nov 11 '13 at 23:15
  • The server's not written in Python is it... – MadProgrammer Nov 11 '13 at 23:17
  • It's written in C++ I'm pretty sure. https://code.google.com/p/multitheftauto/ – Fernando Martín Besteiro Nov 11 '13 at 23:20
  • Start by taking the UI out of the equation and see if you can get it to work by just outputting content to the console – MadProgrammer Nov 11 '13 at 23:38
  • Tried something like this: http://stackoverflow.com/questions/3643939/java-process-with-input-output-stream without UI, and even something similar, as you said, but guess what.. no output in java's console :( – Fernando Martín Besteiro Nov 11 '13 at 23:44
  • Maybe the thing is, program does not have any 'EXIT', it's just messages written into console.. and maybe I'm just explaining wrong? – Fernando Martín Besteiro Nov 11 '13 at 23:46
  • I tried it to, obviously don't have GTA installed, but still couldn't get any output reading from the process – MadProgrammer Nov 11 '13 at 23:46
  • Sure, for opening the server you don't need GTA.. The thing is... if program does no output, just prints and prints and prints... is there any way to get what the program printeD? – Fernando Martín Besteiro Nov 11 '13 at 23:48