Close an application
Am having an application that reads from the serial port, when I want to change the port, I want to automatically close the application and automatically start the application again with a new port connection.
When I use
System.exit(0);
, the application never start again. Here is my code
import java.io.IOException;
public class Connection {
private String Port = "";
Connection(String Port){
this.Port = Port;
}
public void changePort(){
try{
System.exit(0);
GUI application = new GUI(Port);
application.getJFrame().setVisible(true);
}catch(IOException e){
}
}
}
what am I doing wrong?
[731 byte] By [
musiigedeo] at [2007-11-20 10:13:38]

# 1 Re: Close an application
Next time, use the "" vB code, since it makes your code look much neater.
Anyways, the reason it's not re-opening is because it gets to System.exit() and that tells it to just shut off. Maybe, if you want to port change, have to move to another method and/or another class. That way you don't find yourself exiting the program.
# 2 Re: Close an application
I see no advantage to completely exiting an application and then trying to restart it. I don't know an easy way to do it, and what's more, I don't really want to know. It sounds to me like you should consider rethinking your underlying design. Why not just simply reinitialize the variables that need to be reinitialized such as by reconstructing classes?
# 3 Re: Close an application
If you really must close and restart the program (and there probably aren't many cases where this is really necessary and it certainly shouldn't be used to get around a bad design/lazy implementation) then it can be done from within a Java application by using different classloaders but it's probably way beyond most peoples abilities. However, there is a simple bodge - use a batch file to start the program, check for a return value and then call itself if a particular value is returned. For example under windows you code write a .bat file as follows:rem runMyProgram.bat
java MyProgram %1 %2 %3 %4 %5 %6 %7 %8 %9
if ERRORLEVEL 1 runMyProgram %1 %2 %3 %4 %5 %6 %7 %8 %9
Then in your program when you want to restart call the exit() method with an argument of 1 eg
System.exit(1);I've no idea if this technique will work under different OS's but it does work under Windows although I would'nt recommend it as a production technique as you are relying on the user to initially start the program using the .bat file.
keang at 2007-11-10 2:16:41 >
