This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

how to finish the process forcibly?

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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");
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

Henry
 
Aayush Singhal
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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....
 
Sheriff
Posts: 28344
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Sheriff
Posts: 22816
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Aayush Singhal
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22816
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Marshal
Posts: 80063
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22816
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 87
Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just close the outputstream of the process. This will work



Regards
Prem
 
Rob Spoor
Sheriff
Posts: 22816
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic