Skip to main content

jxta2.4.1 platform does not stop when calling System.exit()

2 replies [Last post]
jernejt
Offline
Joined: 2007-08-30
Points: 0

Hi All;

I am trying to stop the jxta platform. Since it does not provide any other means of stopping I am calling System.exit(). However this does not terminate the application on some computers (e.g. my Compaq nc8430). If I run from command line it does not stop and after closing console window it reports that the program is not responding. I tried it both with Java 1.5 and 1.6. A sample application is as follows:

package jernej.testiranje.exiting;

import net.jxta.peergroup.NetPeerGroupFactory;

public class StoppingExample {

public static void main(String[] args){

StoppingExample s=new StoppingExample();
try {
s.startJxta();
} catch (Exception e) {
System.out.println("Could not create and close the platform!");
e.printStackTrace();
}
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println("Now lets try to kill the platform with System.exit()");
try{
System.exit(1);
}
catch(Exception ex){
System.out.println("Cannot exit system! Reason: ");
ex.printStackTrace();
}
}

public void startJxta() throws Exception{
try{
//We create our thread that only prints out something every second.
Thread printer=new Thread(new ThreadJob());
printer.start();

//This line starts many jxta threads that obviosly cannot be killed with System.exti() - just guessing!
NetPeerGroupFactory npgf1 = new NetPeerGroupFactory();
System.out.println("We just created the platform net peer group factory!");

}
catch(Exception ex){
System.out.println("Could not create the peer platform! Exiting...");

}
}

/**
* Inner class that represents the job of the thread that we created.
* @author Administrator
*
*/
public class ThreadJob implements Runnable{
public void run() {
while(true){
System.out.println("I am still alive!");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

}

}

The interesting thing about this example is that System.exit() does stop the thread that prints out sth. every second but is not able to "kill" jxta threads...

Any suggestions?

Cheers;
Jernej

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
bondolo
Offline
Joined: 2003-06-11
Points: 0

System.exit() unconditionally terminates the program and there normally isn't anything that JXTA or any other program can do that would keep Java running.

If you replace the call to System.exit(1) with a call to Runtime.getRuntime().halt(1) does the program quit?

jernejt
Offline
Joined: 2007-08-30
Points: 0

That is what I thought also. But in my case it does not terminate the program. What is interesting is that if I comment out the line NetPeerGroupFactory npgf1 = new NetPeerGroupFactory(); then the program terminates normally. So the problem must be somehow related to many jxta threads that are created in this line.

Any other hints? The thing is critical since we are using jxta for a commercial project (SnackTV)!

Cheers;
Jernej

P.S.: Runtime.getRuntime().halt(1) or Runtime.getRuntime().exit(1) do not work either!