problem with destroying a process
This is my First program with Process and ProcessBuilder.
I want to play an mp3 file for about 10 sec then stop it.
here is the code
import java.io.*;
class ProcessDemo{
publicstaticvoid main(String[] args){
try{
ProcessBuilder process=new ProcessBuilder("realplay","/home/usrname/xxx.mp3");
Process p=process.start();
}catch(IOException e){
System.out.println("some error");
}
try{
Thread.sleep(10000);
p.destroy();
}catch(InterruptedException e){
System.out.println("another error");
}
}
}
it gives me following error
ProcessDemo.java:14: cannot find symbol
symbol : variable p
location: class ProcessDemo
p.destroy();
^
1 error
when I change the linep.destroy(); just after the lineProcess p=process.start(); it compiles and run.(Though its actually doesnot showing real player as it is being destroyed as soon as its created). What is the problem?
well I guess I got the problem. Its instde the 1st try block i initialised the Process object so after the try block it has no effect so java compiler can not recognize it. If it is the case then how can I do what I want?
Yes, you got the problem. If you don't want to declare it inside the try block then what are the other options you have?
> This is my First program with Process and
> ProcessBuilder.
> I want to play an mp3 file for about 10 sec then stop
> it.
>
> here is the code
> > import java.io.*;
>
> class ProcessDemo {
> public static void main(String[] args){
> try{
> ProcessBuilder process=new
> ProcessBuilder("realplay","/home/usrname/xxx.mp3");
> Process p=process.start();
>
> }catch(IOException e) {
> System.out.println("some error");
> }
> try {
> Thread.sleep(10000);
> p.destroy();
> }catch(InterruptedException e){
> System.out.println("another error");
> }
> }
> }
>
>
>
> it gives me following error
> ProcessDemo.java:14: cannot find symbol
> symbol : variable p
> location: class ProcessDemo
> p.destroy();
> ^
> 1 error
>
> when I change the linep.destroy(); just after
> the lineProcess p=process.start(); it compiles and
> run.(Though its actually doesnot showing real player
> as it is being destroyed as soon as its created).
> What is the problem?
Declare the p outside the try block.
You are having basic syntax problems with scope:
Before:
try {
X x = new X();
//...
} catch (Exception e) {
x.method(); //cannot find symbol
} finally {
x.method(); //cannot find symbol
}
x.method(); //cannot find symbol
After:
X x = null;
try {
x = new X();
//...
} catch (Exception e) {
if (x != null)
x.method();
else {
//something else?
}
} finally {
if (x != null)
x.method();
else {
//something else?
}
}
if (x != null)
x.method(); //cannot find symbol
else {
//something else?
}
By changing your logic and moving things around, you may be able to
get rid of those x != null tests.
Second take: here is how I would rewrite your code. Is it simpler?
import java.io.IOException;
public class ProcessExample {
public static void main(String[] args) {
try {
startThenDestroy();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
public static void startThenDestroy() throws IOException, InterruptedException {
ProcessBuilder process=new ProcessBuilder("realplay","/home/usrname/xxx.mp3");
Process p=process.start();
Thread.sleep(10000);
p.destroy();
}
}
well I have declired the Process p=null before the try block. But it didnt work then I changed the two try blocks in a nested one. Now my program is running but the real player is not stoping after 10 sec.
try{
Process p=process.start();
try {
Thread.sleep(10000);
p.destroy();
}catch(InterruptedException e){}
}catch(IOException e){}
Your original code would work. Just put the Process p=null; outside the block in the first code you posted and it would work fine.
> > try{
> Process p=process.start();
> try {
> Thread.sleep(10000);
>p.destroy();
>}catch(InterruptedException e){}
> ch(IOException e){}
>
Nasty looking code, that. See reply #5.
As for destroy working or not, sometimes a process P1 will fork off another
process P2 that is the actual application, then P1 terminates. If that
is the case here, it will not be so easy to stop the application you launch.
Sorry I didnt look the previous reply. Is there a way to delete post? And u says sometimes its too hard to destroy a process . (CRYING) I want a easy way. LOL. Have to search and study a lot myself I gues.Thanks for your repliesMessage was edited by: tanmoy_rajguru
> Have to search and study a lot myself I gues.> Thanks for your repliesIt's better than depending on others (especially your professors) .