Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2008

Tip: Looking for answers? Try searching our database.

Runtime.getRuntime.exec() without exiting the main program

Thread view: 
knguyen - 13 Feb 2008 17:53 GMT
Hi all,

I am having a problem using Runtime.getRuntime.exec(). My codes are as
follow

import java.io.*;
public class Test {
public static void main(String[] args) throws Exception {
   Process p = Runtime.getRuntime().exec(aCommand);
   while (true) {}
}

}

for some reason, the command is not executed until the program exits.
This causes trouble only in Windows because I tested with Linux and it
works fine.

Thanks you
Andrea Francia - 13 Feb 2008 18:16 GMT
> Hi all,
>
[quoted text clipped - 15 lines]
>
> Thanks you

Try this:

import java.io.*;
public class Test {
public static void main(String[] args) throws Exception {
    Process p = Runtime.getRuntime().exec(aCommand);
    p.waitFot();
}
}
Daniele Futtorovic - 13 Feb 2008 19:17 GMT
>> Hi all,
>>
[quoted text clipped - 25 lines]
> }
> }

:-)

<http://java.sun.com/javase/6/docs/api/java/lang/Process.html#waitFor()>
knguyen - 13 Feb 2008 19:53 GMT
On Feb 13, 2:18 pm, Daniele Futtorovic
<da.futt.newsLOVELYS...@laposte.net> wrote:

> >> Hi all,
>
[quoted text clipped - 29 lines]
>
> <http://java.sun.com/javase/6/docs/api/java/lang/Process.html#waitFor()>

Thank you. I forgot to mention I also tried it but I'd like to have
both running. I am trying to invoke a media player, so I would like my
application to do something else while the media is running.
Andrea Francia - 13 Feb 2008 20:09 GMT
> On Feb 13, 2:18 pm, Daniele Futtorovic
> <da.futt.newsLOVELYS...@laposte.net> wrote:
[quoted text clipped - 28 lines]
> both running. I am trying to invoke a media player, so I would like my
> application to do something else while the media is running.

Then use

import java.io.*;
public class Test {
  public static void main(String[] args) throws Exception {
    Process p = Runtime.getRuntime().exec(aCommand);
    doSomething();
  }
}

The problem of while(true) {} is that consumes CPU for do nothing, and
if the scheduling is not preemptive it continue to consume CPU.

Instead of while(true) {} you can use this:

while(true) {
  try{
    Thread.sleep(10000);
  } catch(InterruptedException e) {
  }
}
Arne Vajhøj - 15 Feb 2008 03:25 GMT
>>>>>     while (true) {}

> The problem of while(true) {} is that consumes CPU for do nothing, and
> if the scheduling is not preemptive it continue to consume CPU.

while(true) is not good with preemptive scheduling either (but not
quite as bad).

Arne
Andrea Francia - 13 Feb 2008 20:01 GMT
>> import java.io.*;
>> public class Test {
[quoted text clipped - 7 lines]
>
> <http://java.sun.com/javase/6/docs/api/java/lang/Process.html#waitFor()>

ops!
Daniele Futtorovic - 14 Feb 2008 10:55 GMT
> Hi all,
>
[quoted text clipped - 13 lines]
> This causes trouble only in Windows because I tested with Linux and it
> works fine.

You should provide at least either precise code or a precise
description. You did neither, so it's quite hard to understand your problem.

> the command is not executed until the program exits

That's not clear. Does it get executed after the program exits? And what
does your program do?

The only thing the program, as you posted it, "does" is an endless loop.
Is that really the way it is in your program? I suppose not, for it
doesn't make any sense.

You will have to provide more information. In the mean time, I suggest
you read up on Processes:

"Using Runtime.exec to Invoke Child Processes"
<http://java.sun.com/developer/JDCTechTips/2003/tt0304.html>

"FROM RUNTIME.EXEC() TO PROCESSBUILDER"
<http://java.sun.com/developer/JDCTechTips/2005/tt0727.html>

"When Runtime.exec() won't"
<http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1>

(Even if you already know the basics explained in the first two, pay
close attention to the last one)

df.
Daniel Pitts - 14 Feb 2008 18:49 GMT
> Hi all,
>
[quoted text clipped - 15 lines]
>
> Thanks you
If your command produces output, it could block until you read from the
p.getInputStream();  Same with it expecting output.

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Roedy Green - 15 Feb 2008 19:33 GMT
On Wed, 13 Feb 2008 09:53:33 -0800 (PST), knguyen
<nguyen.h.khanh@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>Hi all,
>
[quoted text clipped - 15 lines]
>
>Thanks you

see http://mindprod.com/jgloss/exec.html
for the proper syntax.

--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.