wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes No text displayed, JTextArea.append();? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "No text displayed, JTextArea.append();?" Watch "No text displayed, JTextArea.append();?" New topic
Author

No text displayed, JTextArea.append();?

Aces Kok Ben
Ranch Hand

Joined: Apr 30, 2011
Posts: 44
Basically, what I was trying to do is to run this Antivirus app called "clamscan.exe" and I want to display out (on a particular JTextArea) whatever the output the Antivirus stream out. Here are my codes:



The thing is, the System.out.println(input.readLine()); could print out the and display the output of the Antivirus while it was scanning, but the jTextArea (a jTextArea). doesn't. Why is this so? The thing I don't understand is, when I kill the process clamscan.exe from the Task manager, the output will then be displayed on the textArea. What happened?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Concurrency in Swing. You'll need a separate thread (or SwingWorker) to read the stream.
Also, read When Runtime.exec() won't. Either use ProcessBuilder and call redirectErrorStream(true), or create a second thread / SwingWorker for reading the error stream.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Aces Kok Ben
Ranch Hand

Joined: Apr 30, 2011
Posts: 44
Rob Spoor wrote:Concurrency in Swing. You'll need a separate thread (or SwingWorker) to read the stream.
Also, read When Runtime.exec() won't. Either use ProcessBuilder and call redirectErrorStream(true), or create a second thread / SwingWorker for reading the error stream.


Mm... I don't really get what you mean. Your words are slightly too technical for me. Can you give me some examples.

EDITED:



I tried running the following codes but it gives me the error of

"Cannot run program "clamscan.exe --database="C:\Users\Aces\workspace\Year2Sem1\InfoSecurityProject\db" --recursive C:\" (in directory "C:\Users\Aces\workspace\Year2Sem1\InfoSecurityProject\mainbin"): CreateProcess error=2, The system cannot find the file specified"

I'm sure the clamwin.exe is there, however, it says it cannot find the file specified.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Aces Kok Ben wrote:Mm... I don't really get what you mean. Your words are slightly too technical for me. Can you give me some examples.

The links that Rob Spoor posted contain a lot of examples. You should read them instead of asking for people to make up new examples.
I tried running the following codes but it gives me the error of

"Cannot run program "clamscan.exe --database="C:\Users\Aces\workspace\Year2Sem1\InfoSecurityProject\db" --recursive C:\" (in directory "C:\Users\Aces\workspace\Year2Sem1\InfoSecurityProject\mainbin"): CreateProcess error=2, The system cannot find the file specified"

I'm sure the clamwin.exe is there, however, it says it cannot find the file specified.

That's perhaps because the code says to use "clamscan.exe" instead of "clamwin.exe". Fix the typo.

However if the typo is in your post here and you really believe that clamscan.exe is "there", then you should figure out where the shell program you're running considers to be "there". Evidently you don't understand where "there" is.
Aces Kok Ben
Ranch Hand

Joined: Apr 30, 2011
Posts: 44
Paul Clapham wrote:
Aces Kok Ben wrote:Mm... I don't really get what you mean. Your words are slightly too technical for me. Can you give me some examples.

The links that Rob Spoor posted contain a lot of examples. You should read them instead of asking for people to make up new examples.
I tried running the following codes but it gives me the error of

"Cannot run program "clamscan.exe --database="C:\Users\Aces\workspace\Year2Sem1\InfoSecurityProject\db" --recursive C:\" (in directory "C:\Users\Aces\workspace\Year2Sem1\InfoSecurityProject\mainbin"): CreateProcess error=2, The system cannot find the file specified"

I'm sure the clamwin.exe is there, however, it says it cannot find the file specified.

That's perhaps because the code says to use "clamscan.exe" instead of "clamwin.exe". Fix the typo.

However if the typo is in your post here and you really believe that clamscan.exe is "there", then you should figure out where the shell program you're running considers to be "there". Evidently you don't understand where "there" is.


I did read the examples and even tried out myself, the problem is I can't understand what it means.

Sorry and I meant clamscan.exe not clamwin.exe (typo error in the previous post)

EDITED:

I took a screenshot of a new set of codes I tried:

http://i53.tinypic.com/25pqzpk.jpg
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

What happens if you execute this command from a command line prompt, in the same directory?
Aces Kok Ben
Ranch Hand

Joined: Apr 30, 2011
Posts: 44
Rob Spoor wrote:What happens if you execute this command from a command line prompt, in the same directory?


It works. I remember you mentioned creating another thread, but I'm not sure if you mean this:



Thread t is based on my Thread I created:

Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1085
    
  10

Let me try and explain what Rob means.

Basically you are calling exec() method and running a process. This starts the process and returns the Process object - with which you can find out the status of the process, send input to process, get output from process - you are using the last one. When you are getting the output from the process, the process is still running. So, its a separate 'thread of execution' you are interacting with. And its a long-running process (SwingWorker API had an advise on why you should not run time-consuming stuff within GUI listeners). And you are doing this all within the 'ActionListener' of the button (which is the swing thread). In Swing, whenever you want to update the GUI via a thread, you can't do it directly. Thats where the SwingWorker and 'Concurrency in Swing' comes in.

You should create the process from within the ActionListener and the rest (while loop and GUI updates) should be done from a SwingWorker.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Also,

1) do not call your class Thread; that causes confusion with java.lang.Thread. Never ever give your classes the same name as any class in the java.lang package.
2) when you call run() on a Runnable or Thread that will not start a new thread, but instead execute the thread's code in the current thread - in this case the Event Dispatcher Thread.
 
GeeCON Prague 2014
 
subject: No text displayed, JTextArea.append();?