停止运行多个实例的Eclipse / Java

我不是java专家或日食专家。 目前我正在研究一个项目,我需要经常调试/测试。 我使用eclipse运行Button。 但是当我不关闭程序时,eclipse / java会再次打开它(第二个窗口)。 这是一个带有swing jframe的gui应用程序。

在eclipse或java中是否有一个函数来终止先前的构建并在运行它时打开新构建?

尝试以下方式:

  1. 如果您处于调试透视图中,可以右键单击运行实例并单击“终止并重新启动”(您可以从窗口 – >首选项 – >常规 – >键绑定快捷方式)这对我来说非常好。

  2. 此链接表示您可以在任何预定中绑定“终止并重新启动”。 你可以尝试一下,但同样对我没有用。

  3. 使用此插件https://bitbucket.org/mantis78/relaunch-plugin (未尝试)。

在运行/调试应用程序时,在eclipse中创建一个新实例。 有两种选择:

  1. 运行应用程序时,它会在控制台中打开UI /结果。 因此,您必须通过单击其中一个来停止现有应用程序: 控制台视图

  2. 在调试模式下,您必须执行相同的操作。 调试视图

否则你必须在代码中处理这个问题。 检查此线程: 如何将Eclipse-RCP应用程序限制为单个实例?

希望这有用。

只是为了添加另一个,最快的(对我而言)解决方案:

在启动之前,按ctrl-F2

此快捷方式终止当前正在运行/已调试的应用程序。

在Eclipse Neon中,转到Window -> Preferences -> Run/Debug -> Launching然后在Launch Operation部分中检查:

[X] Terminate and Relaunch while launching

启动时终止并重新启动

我觉得问题不在于eclipse,而在于你实现应用程序的方式。 有关详细信息,请参阅以下URL。 如果您的应用程序在eclipse之外运行,上述解决方案虽然有效但会有问题。 每当用户启动并关闭app时,都会启动一个新的jvm实例。

退出关闭按钮

这件事真的很晚,但嘿,迟到总比没有好。 初始化所有内容时使用start()。 如果您正在使用JFrame,请使用start(Jframe frame),以便在程序手动关闭时自动处理。 如果不是,请在手动退出程序时调用onClose(),否则下次不会启动。

 package your.package; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; import java.io.IOException; import javax.swing.JFrame; public class ReLaunch { private static boolean relaunchClose = false; private static File runCheck = new File("run.check"); private static File deleteCheck = new File("delete.check"); /** * Use ReLaunch to stop multiple instances */ public static void start() { //Check to see if application is already running if(runCheck.exists()) { //Mark a request to exit the existing application try { deleteCheck.createNewFile(); } catch (IOException e) { e.printStackTrace(); } //Wait until the existing application terminates itself while(runCheck.exists()); //Remove the delete request so current application doesn't terminate if(deleteCheck.exists()) deleteCheck.delete(); } //Mark that this application is currently running try { runCheck.createNewFile(); } catch (IOException e) { e.printStackTrace(); } //Creates a new thread that checks if a new application is requesting to run new Thread(new Runnable() { public void run() { //Wait until delete request shows up while(!deleteCheck.exists()); //Proof that the application is closed to be re-launched relaunchClose = true; //Unmarks this application as running if(runCheck.exists()) runCheck.delete(); //Terminated so new application can run System.exit(0); } }).start(); } /** * Same as start, but automatically handles when the frame is closed by the user by marking that no applications are running * @param frame JFrame which needs to be closed */ public static void start(JFrame frame) { start(); //Listen for when the window is manually closed frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent event) { onClose(); } }); } /** * Marks this application as not running if it is exited manually */ public static void onClose() { if(!relaunchClose) runCheck.delete(); } } 

只需按下停止按钮。 或通过退出按钮关闭会话