如何使用 java 停止 .exe 文件。我有一个 .exe 文件,它以 jsp 页面上的按钮单击开始。现在我不知道如何使用 java 来阻止它。下面是启动 .exe 文件的 java 代码片段。有人能找到阻止它的解决方案吗?
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
try {
res.setContentType("text/html;charset=UTF-8");
PrintWriter out = res.getWriter();
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("C:\\tools\\server\\grd.exe" );
out.println("ServerStarted");
} catch (Throwable t) {
t.printStackTrace();
}
}
最佳答案
您可以终止进程
Process p = ...
p.destroy();
Kills the subprocess. The subprocess represented by this Process object is forcibly terminated.
如果您将来要终止它,则需要跨请求保留对 Process
的引用,可能在 HttpSession
属性中。
在您的 servlet 方法中
Process p = ...
req.getSession(true).setAttribute("process", p);
在未来的请求中(在同一 session 内)
Process p = (Process) req.getSession().getAttribute("process");
关于java - 如何在servlet中停止exe文件进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18766174/