Dra*_*vic 7 java eclipse batch-file
我有一个.bat启动java程序的Windows 文件.为方便起见,我创建了一个Eclipse外部工具配置,以便直接从IDE启动它,并从Eclipse控制台读取其标准输出.
但是,当我在Console视图中使用terminate按钮(红色方块)从Eclipse终止进程时,程序仍在运行.
如何从Eclipse中删除它(没有创建一个单独的启动配置来搜索它并以编程方式杀死它)?
到目前为止,我发现的最佳解决方法是可重用的外部应用程序启动器:
import java.lang.ProcessBuilder.Redirect;
public class Main {
public static void main(String[] args) throws Exception {
Process process = new ProcessBuilder(args[0])
.redirectOutput(Redirect.INHERIT)
.redirectError(Redirect.INHERIT)
.start();
Thread thread = new Thread(() -> readInput(args[1]));
thread.setDaemon(true);
thread.start();
process.waitFor();
}
private static void readInput(String commandLinePart) {
try {
while (System.in.read() != -1);
killProcess(commandLinePart);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void killProcess(String commandLinePart) throws Exception {
final String space = " ";
String[] commandLine = "wmic process where \"commandLine like '%placeholder%'\" delete"
.replaceAll("placeholder", commandLinePart).split(space);
new ProcessBuilder(commandLine).start();
}
}
Run Code Online (Sandbox Code Playgroud)
其想法是启动此应用程序而不是外部应用程序,并将有关目标应用程序的信息作为命令行参数传递给它。
然后启动器应用程序启动进程,重定向输出和错误流(以便我在 Eclipse 控制台中看到目标应用程序输出),等待目标进程完成并等待来自标准输入的 EOF。
最后一点实际上起到了作用:当我从 Eclipse 控制台终止进程时,标准输入到达 EOF,并且启动器应用程序知道是时候停止目标进程了。
Eclipse 外部工具配置对话框现在如下所示:
Location对于所有配置始终相同,并指向start.bat仅运行启动器应用程序的文件:
java -jar C:\ExternalProcessManager\ExternalProcessManager.jar %1 %2
它还采用两个命令行参数:
test.bat,它只是启动我的测试应用程序:)java -jar Test.jar。Test.jar),以便当我从 Eclipse 控制台终止进程时,启动器应用程序可以唯一地识别并终止目标进程。