Home > Java > javaTutorial > How Can I Restart a Java AWT Application?

How Can I Restart a Java AWT Application?

DDD
Release: 2024-11-08 01:30:02
Original
1079 people have browsed it

How Can I Restart a Java AWT Application?

Restarting a Java AWT Application

In Java, like in C#, it is possible to restart an application. When an event handler is attached to a button, the following method can be used to restart the application:

public void restartApplication()
{
  final String javaBin = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";
  final File currentJar = new File(MyClassInTheJar.class.getProtectionDomain().getCodeSource().getLocation().toURI());

  /* is it a jar file? */
  if(!currentJar.getName().endsWith(".jar"))
    return;

  /* Build command: java -jar application.jar */
  final ArrayList<String> command = new ArrayList<String>();
  command.add(javaBin);
  command.add("-jar");
  command.add(currentJar.getPath());

  final ProcessBuilder builder = new ProcessBuilder(command);
  builder.start();
  System.exit(0);
}
Copy after login

This method locates the Java executable, finds the application (a jar file in this case), builds a command to restart the jar, and then executes it. This process terminates the current application and starts it again, effectively restarting it.

The above is the detailed content of How Can I Restart a Java AWT Application?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template