aspose file tools*
The moose likes Java in General and the fly likes how to finish the process forcibly? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to finish the process forcibly?" Watch "how to finish the process forcibly?" New topic
Author

how to finish the process forcibly?

Aayush Singhal
Ranch Hand

Joined: Jan 06, 2011
Posts: 47
When I run the below code, It gives an exception "FileNotFound" at the last line. But when i check it in the explorer, the file C:\\TestApTestApplication\\processList.txt already exists.

What am I missing here??

Process p = Runtime.getRuntime().exec("cmd /c WMIC /OUTPUT:C:\\TestApplication\\processList.txt PROCESS WHERE \"CommandLine='java -Xms140m -Xmx200m -jar c:/cuss/airlines/vs/lib/cussvs.jar c:/cuss/airlines/vs/resources/config.xml'\" get ProcessId");

System.out.println("here");
Thread.sleep(5000);
System.out.println("here");
FileInputStream fis = new FileInputStream("C:\\TestApTestApplication\\processList.txt");
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

The command line seems to indicate a different path to the file than the Java code is using. Are you showing us your real code, or am I just wasting my time pointing this out?


[Jess in Action][AskingGoodQuestions]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40


Since this really isn't about threads and synchronization, moving this to the java in general forum.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Aayush Singhal
Ranch Hand

Joined: Jan 06, 2011
Posts: 47
Ernest Friedman-Hill wrote:The command line seems to indicate a different path to the file than the Java code is using. Are you showing us your real code, or am I just wasting my time pointing this out?


I am quoting the real code. It's a mistake while copying it on the forum...

Trouble is when I do not write the last line (one that initiates the reading part), the new file is created with the correct data.
But when i add the code to read the .txt file, it says, "FIleNotFound". Is it because the Runtime.getRuntime.exec() returns a process that couldnt get completed??? That's why i posted it in Threads and Processes section....
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Well, since you've posted the real code:

Your command line application appears to write to this file: C:\TestApplication\processList.txt

And then your Java application appears to try to read from this file: C:\TestApTestApplication\processList.txt

This would explain why the Java code can't find the latter file.
Aayush Singhal
Ranch Hand

Joined: Jan 06, 2011
Posts: 47
Thanks Paul. But it was a copy-paste mistake while adding the code to the forum. I've used the correct paths and all..
The actual trouble is :

If I delete the file from the actual path manually, the First command creates the new File with the correct data but while reading it back, it gives the FileNotFoundException.

If I don't delete the file and re-run the program, it updates the file with the new data but while reading it back, retrieves the previous data.

So I guess the reading starts before the WMIC /OUTPUT: (command that I run from Runtime.getRuntime().exec()) finishes the writing part....

How do I make sure that the writing part is completed and then only the reading begins.

Runtime.getRuntime().exec() runs in a separate process everytime. so is it causing the problem??
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

When Runtime.exec() won't. Read it, definitely. The only thing that makes that article old is that it doesn't mention ProcessBuilder. You should definitely switch to that instead of Runtime.exec(), because a) it's easier to use, and b) it allows you to redirect the error output of the process to its standard output, which means you only need to read from one stream.

Also, use p.waitFor() (in combination with reading the streams as the article suggests) instead of Thread.sleep(5000). What if your process needs more than 5 seconds?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Aayush Singhal
Ranch Hand

Joined: Jan 06, 2011
Posts: 47
ya, I read that already and tried almost everything that i could understand in that (except the ProcessBuilder. will search for it right now).... but still i can't end the process....

This is my entire code.




When I run it, the file C:\\TestApplication\\processList1.txt gets created with size = 0 KB and the console ooutput shows the two "here" only (the one before the do-while and the one just inside it). so i make out that do-while loop runs only once but even in that it never reaches the 4th line in do-while loop. it gets stuck in "line=br.readLine();". And then when i terminate the program, the file C:\\TestApplication\\processList1.txt gets its correct size 1KB and the correct data (Process Id \n 1234).

Even the output of my command is very small, so i don't think BufferedReader is causing any problem.... How do i do it???
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Please UseCodeTags next time. I've added them now.

Maybe the application is writing data to its error stream as well, and gets blocked because the error stream buffer is full. That's why I suggested using ProcessBuilder with redirecting the stream.

Also, your loop looks odd. Both System.out and the file will get a trailing null. You can solve that by changing the loop to this:
Aayush Singhal
Ranch Hand

Joined: Jan 06, 2011
Posts: 47
Rob Spoor wrote:Please UseCodeTags next time. I've added them now.

Maybe the application is writing data to its error stream as well, and gets blocked because the error stream buffer is full. That's why I suggested using ProcessBuilder with redirecting the stream.

Also, your loop looks odd. Both System.out and the file will get a trailing null. You can solve that by changing the loop to this:


Will take care of the codeTag next time...
I had used the StreamGobbler classes first that creat a separate thread for output and error stream both and flushes them out, but it was not working. so i chose to flush them in the main thread itself as shown in my code. (it doesnot show the errorStream flushing coz I removed it coz I never reached there.. )

about the while loop... i totally agree to your point about the trailing null. I had done it the same way but i just wanted to know what was happening so i was just playing with everything that i thought might be the problem.

so please tell me, is it necessary to build a separate thread for ERROR and OUTPUT streams of Runtime.exec() ?? coz it was not working that way either....

Is there some problem with the command itself?? coz the command itself has "/OUTPUT:C:\\TestApplication\\processList1.txt " which redirects the output to a file. then i redirect it again to FileStream using process.getOutputStream??? I am totally confused..
You guys take your time.... please do not take too much stress as i m leaving it for now.... will do it from fresh mind later sometime...

Thanks for all the help
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38873
    
  23
No, you don't require a thread for the error stream and the input stream. You require two new threads. Not the main thread. It's all explained in the link Rob gave you.
And it's not "coz", please.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

To be honest, Campbell, one of the threads can be the main thread. If you read all data in the main thread and then call waitFor(), waitFor() will not block on a full output buffer - because you've just read all that. But yes, a different thread for the other stream is required, unless you combine them into one with ProcessBuilder.
prem pillai
Ranch Hand

Joined: Nov 02, 2007
Posts: 87

Just close the outputstream of the process. This will work



Regards
Prem
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Strangely enough closing the output stream is required or something, because without it p.waitFor() doesn't end, even if I read all of the data from its streams. I guess that's caused by the executed command, because if I replace it with a simple "cmd /c dir C:\\" I don't need it.

But that code is still flawed because there is still the potential of the process' stream buffers being filled and the call to p.waitFor() blocking. So add the reading from p.getInputStream() and p.getErrorStream() (if no redirecting from the latter to the former is set up) and that will work.
Aayush Singhal
Ranch Hand

Joined: Jan 06, 2011
Posts: 47
prem pillai wrote:Just close the outputstream of the process. This will work



Regards
Prem



This one did did the trick Thanks a ton Prem!!!

will take care about emptying out the streams too...

Thanks a lot everyone.

Edit : P.S. was just wondering how does anyone figure this one out that this one small thing is required to make it work?? Where and how do I learn to do things like this?? Because I tried a lot of experiments in this program but still nothing was happening...
 
 
subject: how to finish the process forcibly?