我有一个Java webstart进程,它是Windows批处理脚本的一部分.在这种情况下,我在批处理脚本中使用javaws命令.使用"apache commons exec"以编程方式调用此匹配脚本(start.bat).在某些情况下,javaws调用的java进程会挂起,我必须从批处理脚本start.bat开始终止整个进程线程.有没有通过apache commons exec杀死整个进程树的程序方法?
我尝试过使用"execWatchdog.destroyProcess();" 在"start.bat"脚本上.但是它只会杀死start.bat进程而不是整个进程树.
有没有办法通过apache-commons-exec或类似的代码杀死整个进程树?
我已经看到这个问题在c ++中执行相当于"杀死进程树"的Windows,它在c ++中执行相同的任务.我想知道是否有人通过JNI实现了调用Windows本机系统调用.
下面的代码获取所有输出,无论是stdout还是stderr.
String line = String.format("paty/to/script.py");
CommandLine cmd = CommandLine.parse(line);
DefaultExecutor executor = new DefaultExecutor();
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
PumpStreamHandler psh = new PumpStreamHandler(stdout);
executor.setStreamHandler(psh);
int exitvalue = executor.execute(cmd);
String output = stdout.toString();
Run Code Online (Sandbox Code Playgroud)
如何单独获取两个流?
我正在尝试使用 Apache Commons Exec 来运行使用正则表达式的 git 命令。
当我形成CommandLine并打印出来时,它看起来像这样:
[git, --no-pager, grep, --line-number, --untracked, --extended-regexp, "^\s*public void\s+(testFindByAdAccount).*", --, *Test.java]
Run Code Online (Sandbox Code Playgroud)
但是,当我执行此操作时,git 没有返回任何结果,导致退出代码 1。
但是,当我手动运行此命令时,它会返回大量结果并成功。将参数更改--extended-regexp为字符串(如testFindByAdAccount通过 运行时)确实会产生结果Exec,因此我认为 Apache Commons 正在对 regexp 参数执行某些操作,使其无效。有什么想法吗?
编辑:添加可重现的示例
gradlew shadowJar为项目生成jar文件java -jar app/build/libs/app-all.jar$ java -jar app/build/libs/app-all.jar
HELLOOOOOO
WD::: null
[git, --no-pager, grep, --line-number, --untracked, --extended-regexp, "^\s*public void\s+(testAppHasAGreeting)\(\).*", --, *Test.java]
WD::: /Users/rgurney/Src/personal/min-example
Exception in thread "main" java.lang.RuntimeException: org.apache.commons.exec.ExecuteException: Process exited with an …Run Code Online (Sandbox Code Playgroud) 我不知道为什么会这样.我试图通过commons-exec捕获进程的输出,然后我继续挂起.我已经提供了一个示例程序来演示以下这种行为.
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.PumpStreamHandler;
public class test {
public static void main(String[] args) {
String command = "java";
PipedOutputStream output = new PipedOutputStream();
PumpStreamHandler psh = new PumpStreamHandler(output);
CommandLine cl = CommandLine.parse(command);
DefaultExecutor exec = new DefaultExecutor();
DataInputStream is = null;
try {
is = new DataInputStream(new PipedInputStream(output));
exec.setStreamHandler(psh);
exec.execute(cl);
} catch (ExecuteException ex) {
} catch (IOException ex) {
}
System.out.println("huh?");
}
}
Run Code Online (Sandbox Code Playgroud) 我在JBoss 4.2.3中的Web应用程序上使用Apache Commons Exec.每当我调用Apache Exec时,它会输出日志中的所有控制台输出,这是很多输出,它可以轻松地在生产环境中填写我的日志.如何防止此日志打印并仅显示错误日志?
问候
我需要执行一个外部应用程序,它返回大量数据(需要2个多小时才能完成)n并连续输出数据.
我需要做的是异步执行该程序并捕获文件中的输出.我尝试使用java进程构建器,但它似乎挂起并仅在程序退出或强制终止时返回输出.
我试图使用进程构建器并spwaned一个新的线程来捕获输出,但它仍然没有帮助.
然后我读了关于apache commons exec并尝试了同样的事情.然而,这也似乎需要很长时间并返回不同的错误代码(对于相同的输入)
CommandLine cmdLine = new CommandLine("/opt/testsimulator");
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
PumpStreamHandler psh = new PumpStreamHandler(stdout);
ExecuteWatchdog watchdog = new ExecuteWatchdog(60*1000);
Executor executor = new DefaultExecutor();
executor.setStreamHandler(psh);
executor.setWatchdog(watchdog);
try {
executor.execute(cmdLine);
} catch (ExecuteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
任何帮助或工作的例子都会非常有帮助
这就是我正在做的事情:
import org.apache.commons.exec.*;
String cmd = "/bin/sh -c \"echo test\"";
new DefaultExecutor().execute(CommandLine.parse(cmd));
Run Code Online (Sandbox Code Playgroud)
这是输出:
/bin/sh: echo test: command not found
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
Apache Commons Exec是一个线程安全的库吗?