【发布时间】:2014-05-14 21:45:20
【问题描述】:
我有一个名为 VcctlWrapper.jar 的 javaw 应用程序,它启动嵌入式码头会话、打开浏览器窗口并允许用户将数据输入到 derby 数据库等。该应用程序当前正在使用以下命令启动:
start javaw -classpath "lib/derby.jar:lib/derbytools.jar" -jar "VcctlWrapper.jar"
目前,当用户完成应用程序后,终止 javaw.exe 应用程序的唯一方法似乎是打开任务管理器并手动结束相关进程。我想找到一种方法让 javaw.exe 在用户关闭 Web 浏览器时自动终止。这可能吗?
构成 VcctlWrapper 的源代码非常短,因此我将其全部包含在此处:
package vcctl;
import java.awt.*;
import java.net.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.handler.*;
import org.eclipse.jetty.webapp.*;
public class webServiceRunner extends javax.swing.JFrame {
private URI uri;
private Desktop desktop;
private Desktop.Action action = Desktop.Action.OPEN;
/** Creates new form webServiceRunner */
public webServiceRunner() {
// initComponents();
try {
startJettyServer();
} catch (Exception ex) {
}
try {
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
try {
uri = new URI( "http://localhost:8080/vcctl" );
} catch (URISyntaxException urisex) {
}
desktop.browse( uri );
}
} catch (IOException ioex) {
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 152, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 63, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
protected static void startJettyServer() throws Exception {
Server server = new Server(8080);
WebAppContext webapp = new WebAppContext();
Path currentRelativePath = Paths.get("");
String s = currentRelativePath.toAbsolutePath().toString();
System.out.println("Current relative path is: " + s);
String warPath = s + "/vcctl.war";
webapp.setContextPath("/vcctl");
webapp.setWar(warPath);
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[]{ webapp, new DefaultHandler()});
server.setHandler(handlers);
server.start();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new webServiceRunner().setVisible(false);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
}
【问题讨论】:
-
如果你不想打开任务管理器,你可以从你的控制台调用
taskkill /f /im javaw.exe。这不是一个解决方案,但比打开任务管理器要快。 -
同意,它更快。但是我有这个应用程序的用户,他们不想“在幕后”做任何事情。他们会期望当 Web 浏览器关闭时进程会被终止。
标签: java embedded-jetty