Hi!
I'm having a problem - I'm calling an external Process with -
Process p = Runtime.getRuntime().exec("cmd /c ...");
-
(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:
or like this: -
boolean finished = false;
-
while(!finished)
-
{
-
try
-
{
-
p.exitValue();
-
finished = true;
-
}
-
catch(IllegalThreadStateException itse){}
-
}
-
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
12 2085
Hi!
I'm having a problem - I'm calling an external Process with -
Process p = Runtime.getRuntime().exec("cmd /c ...");
-
(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:
or like this: -
boolean finished = false;
-
while(!finished)
-
{
-
try
-
{
-
p.exitValue();
-
finished = true;
-
}
-
catch(IllegalThreadStateException itse){}
-
}
-
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.
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: -
try
-
{
-
Runtime rt = Runtime.getRuntime();
-
Process proc = rt.exec("cmd /c ...");
-
InputStream stderr = proc.getErrorStream();
-
InputStreamReader isr = new InputStreamReader(stderr);
-
BufferedReader br = new BufferedReader(isr);
-
String line = null;
-
System.out.println("<ERROR>");
-
while ( (line = br.readLine()) != null)
-
System.out.println(line);
-
System.out.println("</ERROR>");
-
int exitVal = proc.waitFor();
-
System.out.println("Process exitValue: " + exitVal);
-
}
-
catch (Throwable t)
-
{
-
t.printStackTrace();
-
}
-
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).
Thanks for the reply, that article is very interesting, but somehow it won't work nevertheless. My code looks like this now: -
try
-
{
-
Runtime rt = Runtime.getRuntime();
-
Process proc = rt.exec("cmd /c ...");
-
InputStream stderr = proc.getErrorStream();
-
InputStreamReader isr = new InputStreamReader(stderr);
-
BufferedReader br = new BufferedReader(isr);
-
String line = null;
-
System.out.println("<ERROR>");
-
while ( (line = br.readLine()) != null)
-
System.out.println(line);
-
System.out.println("</ERROR>");
-
int exitVal = proc.waitFor();
-
System.out.println("Process exitValue: " + exitVal);
-
}
-
catch (Throwable t)
-
{
-
t.printStackTrace();
-
}
-
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?
Sorry but which construct are you talking about?
I mean everything I've added. Sorry for being a bit unclear about that.
Greetings,
Nepomuk
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.
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
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
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
Hi
I seem to have the same pb, would you mind sharing your final working solution.
Thanks in advance
Tibor
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
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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...
| |