473,396 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

When does Process end?

Nepomuk
3,112 Expert 2GB
Hi!
I'm having a problem - I'm calling an external Process with
Expand|Select|Wrap|Line Numbers
  1. Process p = Runtime.getRuntime().exec("cmd /c ...");
  2.  
(of course, without the dots but with a command instead) and have to wait for that Process to be completed before progressing. However, if I try waiting with:
Expand|Select|Wrap|Line Numbers
  1. p.waitFor();
or like this:
Expand|Select|Wrap|Line Numbers
  1. boolean finished = false;
  2. while(!finished)
  3. {
  4.     try
  5.     {
  6.         p.exitValue();
  7.         finished = true;
  8.     }
  9.     catch(IllegalThreadStateException itse){}
  10. }
  11.  
it goes into an infinite loop or something. Can anyone at least give me a hint? I'm feeling totally lost right now...

Greetings,
Nepomuk
Sep 20 '07 #1
12 2085
r035198x
13,262 8TB
Hi!
I'm having a problem - I'm calling an external Process with
Expand|Select|Wrap|Line Numbers
  1. Process p = Runtime.getRuntime().exec("cmd /c ...");
  2.  
(of course, without the dots but with a command instead) and have to wait for that Process to be completed before progressing. However, if I try waiting with:
Expand|Select|Wrap|Line Numbers
  1. p.waitFor();
or like this:
Expand|Select|Wrap|Line Numbers
  1. boolean finished = false;
  2. while(!finished)
  3. {
  4.     try
  5.     {
  6.         p.exitValue();
  7.         finished = true;
  8.     }
  9.     catch(IllegalThreadStateException itse){}
  10. }
  11.  
it goes into an infinite loop or something. Can anyone at least give me a hint? I'm feeling totally lost right now...

Greetings,
Nepomuk
Read this javaworld article on Runtime.exec pitfalls.
Sep 20 '07 #2
Nepomuk
3,112 Expert 2GB
Read this javaworld article on Runtime.exec pitfalls.
Thanks for the reply, that article is very interesting, but somehow it won't work nevertheless. My code looks like this now:
Expand|Select|Wrap|Line Numbers
  1. try
  2. {            
  3.     Runtime rt = Runtime.getRuntime();
  4.     Process proc = rt.exec("cmd /c ...");
  5.     InputStream stderr = proc.getErrorStream();
  6.     InputStreamReader isr = new InputStreamReader(stderr);
  7.     BufferedReader br = new BufferedReader(isr);
  8.     String line = null;
  9.     System.out.println("<ERROR>");
  10.     while ( (line = br.readLine()) != null)
  11.         System.out.println(line);
  12.     System.out.println("</ERROR>");
  13.     int exitVal = proc.waitFor();
  14.     System.out.println("Process exitValue: " + exitVal);
  15. }
  16. catch (Throwable t)
  17. {
  18.     t.printStackTrace();
  19. }
  20.  
I basically copied the code from that article and changed the command that it should call. But it still hangs.

Just in case it's of any importance, the program I'm calling is gunzip and the command works fine without that construct. With the construct, it gets stuck while unpacking after 20KB (of the 9.47MB that it should be).

Any other ideas?

Greetings,
Nepomuk

*EDIT:* I found, that as soon as I exit my program manually, the extracted file is complete (so it jumps from 20KB to 9.47MB).
Sep 20 '07 #3
r035198x
13,262 8TB
Thanks for the reply, that article is very interesting, but somehow it won't work nevertheless. My code looks like this now:
Expand|Select|Wrap|Line Numbers
  1. try
  2. {            
  3.     Runtime rt = Runtime.getRuntime();
  4.     Process proc = rt.exec("cmd /c ...");
  5.     InputStream stderr = proc.getErrorStream();
  6.     InputStreamReader isr = new InputStreamReader(stderr);
  7.     BufferedReader br = new BufferedReader(isr);
  8.     String line = null;
  9.     System.out.println("<ERROR>");
  10.     while ( (line = br.readLine()) != null)
  11.         System.out.println(line);
  12.     System.out.println("</ERROR>");
  13.     int exitVal = proc.waitFor();
  14.     System.out.println("Process exitValue: " + exitVal);
  15. }
  16. catch (Throwable t)
  17. {
  18.     t.printStackTrace();
  19. }
  20.  
I basically copied the code from that article and changed the command that it should call. But it still hangs.

Just in case it's of any importance, the program I'm calling is gunzip and the command works fine without that construct. With the construct, it gets stuck while unpacking after 20KB (of the 9.47MB that it should be).

Any other ideas?

Greetings,
Nepomuk
Sorry but which construct are you talking about?
Sep 20 '07 #4
Nepomuk
3,112 Expert 2GB
Sorry but which construct are you talking about?
I mean everything I've added. Sorry for being a bit unclear about that.

Greetings,
Nepomuk
Sep 20 '07 #5
r035198x
13,262 8TB
So you mean the problem is that the process is not exiting or is it that the process ends but there is no output produced when it is successful.
Sep 20 '07 #6
Nepomuk
3,112 Expert 2GB
So you mean the problem is that the process is not exiting or is it that the process ends but there is no output produced when it is successful.
I can only guess what happens with it while runtime, but as the result is correct, as soon as I quit my program manually (-> the full 9.47MB are there), it seems to have some temporary data somewhere (in a Buffer maybe?) and it puts it in the place it belongs, when it's closed (or it's damn fast as soon as I attempt to close it).

I just checked and according to the task manager, gunzip runs until I close my program. Am I somehow stopping it from closing?

Greetings,
Nepomuk
Sep 20 '07 #7
JosAH
11,448 Expert 8TB
I can only guess what happens with it while runtime, but as the result is correct, as soon as I quit my program manually (-> the full 9.47MB are there), it seems to have some temporary data somewhere (in a Buffer maybe?) and it puts it in the place it belongs, when it's closed (or it's damn fast as soon as I attempt to close it).

I just checked and according to the task manager, gunzip runs until I close my program. Am I somehow stopping it from closing?

Greetings,
Nepomuk
You shouldn't try to capture the process' error output stream *and* wait for the
process to complete in just one thread. Use separate threads for the output
capturing, the completion of the process (use the join() method) and the final
return from the outer method call. A bit of wait/notify etc. will do the job.

kind regards,

Jos
Sep 20 '07 #8
Nepomuk
3,112 Expert 2GB
You shouldn't try to capture the process' error output stream *and* wait for the
process to complete in just one thread. Use separate threads for the output
capturing, the completion of the process (use the join() method) and the final
return from the outer method call. A bit of wait/notify etc. will do the job.

kind regards,

Jos
Thanks, that did the trick.

Greetings,
Nepomuk
Sep 21 '07 #9
Hi
I seem to have the same pb, would you mind sharing your final working solution.
Thanks in advance

Tibor
Oct 17 '07 #10
Nepomuk
3,112 Expert 2GB
Hi
I seem to have the same pb, would you mind sharing your final working solution.
Thanks in advance

Tibor
I'm not able to post the code, as I have no access to it any more (it was for work, but my contract was only for the semester holidays and they are over), but as far as I remember, I basically used 2 new Threads for cleaning the Streams as suggested on this site. (Posted by r035198x in Answer #2.) Look for the StreamGobbler.

Greetings,
Nepomuk
Oct 17 '07 #11
r035198x
13,262 8TB
I'm not able to post the code, as I have no access to it any more (it was for work, but my contract was only for the semester holidays and they are over), but as far as I remember, I basically used 2 new Threads for cleaning the Streams as suggested on this site. (Posted by r035198x in Answer #2.) Look for the StreamGobbler.

Greetings,
Nepomuk
So you're still alive?
I thought you'd gotten kidnapped by terrorists.
Oct 17 '07 #12
Nepomuk
3,112 Expert 2GB
So you're still alive?
I thought you'd gotten kidnapped by terrorists.
Na, they looked more like aliens from outa space ;-)
Just been quite busy lately, but ever so often I come and have a look around here. :-D

Greetings,
Nepomuk
Dec 7 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Ana | last post by:
Hi! I have problems with the following scenario: My application is developed using C# under .NET. It must run on all Windows versions starting from Windows 98. The user must open different...
29
by: keredil | last post by:
Hi, Will the memory allocated by malloc get released when program exits? I guess it will since when the program exits, the OS will free all the memory (global, stack, heap) used by this...
6
by: Ana | last post by:
Hi! I have problems with the following scenario: My application is developed using C# under .NET. It must run on all Windows versions starting from Windows 98. The user must open different...
0
by: Abhi | last post by:
Hi- I'm trying to execute the C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\50\bin\OWSADM.EXE programmatically from a shell using the process.Start() method. I'm also...
3
by: David Lozzi | last post by:
Howdy, I've discovered how to create and use a class in ASP.NET. However, when is the best time to use a class? For example, I am currently using session variables to store user information (user...
2
by: David Hearn | last post by:
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. Description: An unhandled exception occurred during the execution of the current...
3
by: jimmyfishbean | last post by:
Hi, My client has the following network structure: 2 Windows 2003 servers : Server 1 - Web server running IIS, ftp import and export folder, ASP.NET SOAP web service and asp code on here....
8
by: Gamma | last post by:
I'm trying to inherit subclass from System.Diagnostics.Process, but whenever I cast a "Process" object to it's subclass, I encounter an exception "System.InvalidCastException" ("Specified cast is...
13
by: Edwin Smith | last post by:
I have a form which displays a DataGridView table generated with the VS2005 tools. The database is a Pervasive v.9 with an ODBC driver. The DataGridView works great except when I'm done and I...
44
by: Smokey Grindle | last post by:
I have a list box on my form, but I need to databind it to a data table that is a private member of the form's class... so I basically have Public Class MyForm priate m_MyTable as new datatable...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.