我会去使用调度程序或服务来运行程序。但是,如果你想使用一个bat文件,并做到这一点编程,我提出了以下可能的方法:
在Java程序中,你可以得到PID程序,然后将其写入文件:
public static void writePID(String fileLocation) throws IOException
{
// Use the engine management bean in java to find out the pid
// and to write to a file
if (fileLocation.length() == 0)
{
fileLocation = DEFAULT_PID_FILE;
}
String pid = ManagementFactory.getRuntimeMXBean().getName();
if (pid.indexOf("@") != -1)
{
pid = pid.substring(0, pid.indexOf("@"));
}
BufferedWriter writer = new BufferedWriter(new FileWriter(fileLocation));
writer.write(pid);
writer.newLine();
writer.flush();
writer.close();
}
然后,您可以编写一个停止.bat文件,该文件将在Windows中终止正在运行的程序。你可以这样做:
setlocal
IF EXIST app.pid FOR /F %%i in ('type app.pid') do TASKKILL /F /PID %%i
IF EXIST app.pid DEL app.pid
endlocal
当然,app.pid是由上面的Java方法编写的文件。
我不知道你将如何编写一个脚本来启动一个Java程序,并恢复终止控制。我想知道是否有人为此提供解决方案。
@danben当然,会做...感谢 – Aakash 2010-06-10 15:55:18