我改變了程序直接啓動程序(第二個進程)。
和我用下面的破壞過程
private static void destroyProcess(final Process process) {
if (process != null) {
Field field;
final Runtime runtime = Runtime.getRuntime();
final Class<? extends Process> getClass = process.getClass();
if (JAVA_LANG_UNIX_PROCESS.equals(getClass.getName())) {
// get the PID on unix/linux systems
try {
field = getClass.getDeclaredField("pid");
field.setAccessible(true);
final Object processID = field.get(process);
final int pid = (Integer) processID;
// killing the task.
runtime.exec("kill -9 " + pid);
} catch (IOException e) {
LOGGER.error("Error in Killing the process:" + e);
} catch (SecurityException e) {
LOGGER.error("Error in Killing the process:" + e);
} catch (NoSuchFieldException e) {
LOGGER.error("Error in Killing the process:" + e);
} catch (IllegalArgumentException e) {
LOGGER.error("Error in Killing the process:" + e);
} catch (IllegalAccessException e) {
LOGGER.error("Error in Killing the process:" + e);
}
}
final String classGetName = getClass.getName();
if (JAVA_LANG_WIN32_PROCESS.equals(classGetName) || JAVA_LANG_PROCESS_IMPL.equals(getClass.getName())) {
// determine the pid on windowsplattforms
process.destroy();
try {
field = getClass.getDeclaredField("handle");
field.setAccessible(true);
final int pid = Kernel32.INSTANCE.GetProcessId((Long) field.get(process));
// killing the task.
runtime.exec("taskkill " + pid);
} catch (SecurityException e) {
LOGGER.error("Error in Killing the process:" + e);
} catch (NoSuchFieldException e) {
LOGGER.error("Error in Killing the process:" + e);
} catch (IOException e) {
LOGGER.error("Error in Killing the process:" + e);
} catch (IllegalArgumentException e) {
LOGGER.error("Error in Killing the process:" + e);
} catch (IllegalAccessException e) {
LOGGER.error("Error in Killing the process:" + e);
}
}
}
}
/**
*
* This interface use to kernel32.
*/
static interface Kernel32 extends Library {
/**
*
*/
Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
/**
*
* GetProcessId.
*
* @param hProcess
* hProcess
* @return return the PID.
*/
int GetProcessId(Long hProcess);
// NOTE : Do not change the GetProcessId method name.
}
我得到這個下面的鏈接幫助:http://www.golesny.de/p/code/javagetpid
您可以運行'jar'直接從您的應用程序(通過'ProcessBuilder'或'Runtime.exec()')而不是使用bat文件來做到這一點?如果是這樣,那麼你可以調用'destroy()'它應該可以工作。 – dic19
什麼說dic19說應該工作。我看到的問題是您正在創建一個流程來創建流程。我沒有看到任何直接的方式來處理第二個流程。它可以完成,但是消除中間人並直接啓動第二個過程會簡單得多。那,你現在正在做的事情應該工作。 – MadConan
是的,但我通過bat文件調用兩個進程。 – Rane