我會去使用調度程序或服務來運行程序。但是,如果你想使用一個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