本文介绍了java restartApplication方法仅重新启动一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用在堆栈溢出时发现的restartApplication方法,但是由于某种原因,它只会重新启动我的应用程序一次.例如,在下面的简单JOptionPane程序中,如果用户输入字母"a",它将重新启动该程序.程序重新启动后,如果用户再次输入"a",它将终止执行.如何使它能够连续重新启动?

I was trying to use a restartApplication method that I found on stack overflow, but for some reason it only will restart my application once. For example, in this simple JOptionPane program below, if the user enters the letter "a", it will restart the program. Once the program restarts, if the user types in "a" again, it just terminates the execution. How can I enable it to restart itself continuously?

我添加了一些println()语句以查看是否可以获得更多信息,它只是确认该程序在我第二次键入字母"a"后立即结束.

I added in some println() statements to see if I could get any more info, and it just confirmed that the program is ending right after I type in the letter "a" on the second time around.

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JOptionPane;


public class JOptionTest{
public static void restartApplication()
{
  final String javaBin = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";
  final File currentJar = new File("C:\\Documents and Settings\\My Documents\\hello3.jar");//UpdateReportElements.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);
  try {
    builder.start();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
  System.exit(0);
}


public static void main(String[] args){
    String str = JOptionPane.showInputDialog(null, "Enter some text",1);
    System.out.println(str);
    //String a= "a";
    if (str.equals("a")){
        System.out.println(str+ "right about to restart");
        restartApplication();
    }
}
}

推荐答案

看到这一行?

您一次调用restartApplication,结束时退出Java进程.

you are calling restartApplication a Single time and when it ends you exit the java process.

如果要连续重新启动,请删除此行并可能永远迭代:

If you want to restart continuously, then remove this line and probably iterate forever:

 public static void restartApplication()
 {
  while(true){

       final String javaBin = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";
       final File currentJar = new File("C:\\Documents and Settings\\XBBKKYL\\My      Documents\\hello3.jar");//UpdateReportElements.class.getProtectionDomain().getCodeSource().get           Location().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);
    try {
        builder.start();
    } catch (IOException e) {
       e.printStackTrace();
    }
  }
}

我还没有测试过,这只是一个主意

I have not tested this, it's just an idea

这篇关于java restartApplication方法仅重新启动一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 07:32