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?

[1767 byte] By [tanmoy_rajgurua] at [2007-11-26 19:43:39]
# 1
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?
tanmoy_rajgurua at 2007-7-9 22:27:03 > top of Java-index,Java Essentials,Java Programming...
# 2
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?
DrClapa at 2007-7-9 22:27:03 > top of Java-index,Java Essentials,Java Programming...
# 3

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

qUesT_foR_knOwLeDgea at 2007-7-9 22:27:03 > top of Java-index,Java Essentials,Java Programming...
# 4

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.

DrLaszloJamfa at 2007-7-9 22:27:03 > top of Java-index,Java Essentials,Java Programming...
# 5

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();

}

}

DrLaszloJamfa at 2007-7-9 22:27:03 > top of Java-index,Java Essentials,Java Programming...
# 6

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

tanmoy_rajgurua at 2007-7-9 22:27:03 > top of Java-index,Java Essentials,Java Programming...
# 7
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.
qUesT_foR_knOwLeDgea at 2007-7-9 22:27:03 > top of Java-index,Java Essentials,Java Programming...
# 8

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

DrLaszloJamfa at 2007-7-9 22:27:03 > top of Java-index,Java Essentials,Java Programming...
# 9
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
tanmoy_rajgurua at 2007-7-9 22:27:03 > top of Java-index,Java Essentials,Java Programming...
# 10
> Have to search and study a lot myself I gues.> Thanks for your repliesIt's better than depending on others (especially your professors) .
qUesT_foR_knOwLeDgea at 2007-7-9 22:27:03 > top of Java-index,Java Essentials,Java Programming...