我不是 java 专家或 eclipse 专家。目前我正在做一个项目,我需要经常调试/测试。我使用 Eclipse 运行按钮。但是当我不关闭程序时,eclipse/java 会再次打开它(第二个窗口)。这是一个带有 swing jframe 的 gui 应用程序。
eclipse 或 java 中是否有一个函数可以终止以前的构建并在运行时打开新的构建?
尝试以下方法:
如果您处于调试视角,您可以右键单击正在运行的实例并单击“终止并重新启动”(您可以从 Window -> Preferences -> General -> Keys 绑定快捷方式)这对我来说效果很好。
该链接说您可以在任何方面绑定“终止并重新启动”。你可以试一试,但对我来说同样没有效果。
在 Eclipse 中,当您运行/调试应用程序时,会创建一个新实例。有两种选择:
当您运行应用程序时,它会在控制台中打开 UI/结果。因此,您必须通过单击以下之一来停止现有应用程序:
在调试模式下,您必须这样做。
否则你必须在代码中处理这个。检查此线程:如何将 Eclipse-RCP 应用程序限制为单个实例?
希望这会有所帮助。
只是添加另一个,最快的(对我来说)解决方案:
启动前,按ctrl-F2
。
此快捷方式终止当前运行/调试的应用程序。
我觉得问题不在于 Eclipse,而在于您实现应用程序的方式。详情请参考以下网址。如果您的应用程序在 Eclipse 之外运行,上述解决方案虽然有效,但会出现问题。每次用户启动和关闭您的应用程序时,都会启动一个新的 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();
}
}
just press the stop button . or close the session by exit button