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

K

knguyen

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
 
A

Andrea Francia

knguyen said:
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

Try this:

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

Daniele Futtorovic

knguyen said:
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

Try this:

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

:)

<http://java.sun.com/javase/6/docs/api/java/lang/Process.html#waitFor()>
 
K

knguyen

knguyen said:
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
Try this:
import java.io.*;
public class Test {
public static void main(String[] args) throws Exception {
Process p = Runtime.getRuntime().exec(aCommand);
p.waitFo*t*();
}
}

:)

<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.
 
A

Andrea Francia

knguyen said:
knguyen wrote:
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
Try this:
import java.io.*;
public class Test {
public static void main(String[] args) throws Exception {
Process p = Runtime.getRuntime().exec(aCommand);
p.waitFo*t*();
}
}
:)

<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.

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) {
}
}
 
D

Daniele Futtorovic

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.

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.
 
D

Daniel Pitts

knguyen said:
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
If your command produces output, it could block until you read from the
p.getInputStream(); Same with it expecting output.
 
A

Arne Vajhøj

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
 
R

Roedy Green

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top