java restartApplication方法仅重新启动一次?

编程入门 行业动态 更新时间:2024-10-28 14:25:34
本文介绍了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(); } } }

推荐答案

看到这一行?

System.exit(0);

System.exit(0);

您一次调用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方法仅重新启动一次?

本文发布于:2023-10-23 01:31:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519339.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重新启动   方法   java   restartApplication

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!