Wednesday, November 29, 2017

JAVA - How restart java application

public void restartApplication() throws URISyntaxException, IOException
{
  /* where is java and local app ? */    

  final String javaBin = System.getProperty( "java.home" ) + File.separator + 
                         "bin" + File.separator + "java";
  final File currentJar = new File( getClass().getProtectionDomain().getCodeSource()
                          .getLocation().toURI() );

  /* is it a jar file ? */

  if( !currentJar.getName().endsWith(".jar") ) return;

  /* build command: java -jar application.jar */

  final ArrayList command = new ArrayList();
  command.add( javaBin );
  command.add( "-jar" ); command.add( currentJar.getPath() );

  /* run it - and close current */

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

No comments:

Post a Comment