gpt4 book ai didi

java - 从 exec 运行 java -jar

转载 作者:行者123 更新时间:2023-12-01 12:21:24 24 4
gpt4 key购买 nike

我尝试运行:java -jar/home/user/workspace/maltparser-1.8/maltparser-1.8.jar这是工作。并返回:

-----------------------------------------------------------------------------
MaltParser 1.8
-----------------------------------------------------------------------------
MALT (Models and Algorithms for Language Technology) Group
Vaxjo University and Uppsala University
Sweden
-----------------------------------------------------------------------------

Usage: java -jar maltparser-1.8.jar -f <path to option file> <options> java -jar maltparser-1.8.jar -h for more help and options

help ( -h) : Show options

----------------------------------------------------------------------------- option_file ( -f) : Path to option file

----------------------------------------------------------------------------- verbosity *( -v) : Verbosity level debug - Logging of debugging messages error - Logging of error events fatal - Logging of very severe error events info
- Logging of informational messages off - Logging turned off warn - Logging of harmful situations
-----------------------------------------------------------------------------

Documentation: docs/index.html

现在,我尝试从我的类(class)运行这个 .jar:

public class Main {

public static void main(String[] args) throws IOException, InterruptedException {

Process ps = Runtime.getRuntime().exec(new String[]{"java","-jar","/home/user/workspace/maltparser-1.8/maltparser-1.8.jar"});
ps.waitFor();
java.io.InputStream is=ps.getInputStream();
byte b[]=new byte[is.available()];
is.read(b,0,b.length);
System.out.println(new String(b));
}
}

它什么也没返回给我...我想拦截输出流。我该怎么做?

最佳答案

恕我直言,您读取InputStream的方法有点奇怪,您可能不想等到流填满后再打印某些内容,而且您忽略了错误流。 ..

我更喜欢使用 ProcessBuilder 因为...

  1. 您可以将错误流重定向到 InputStream,这使得管理更加容易
  2. 您可以更改启动命令的工作目录的上下文...

举个例子...

try {
ProcessBuilder pb = new ProcessBuilder(new String[]{"java", "-jar", "Your.jar"});
pb.redirectError();
//pb.directory(new File("you/path"));
Process ps = pb.start();
try (java.io.InputStream is = ps.getInputStream()) {
int read = -1;
while ((read = is.read()) != -1) {
System.out.print((char) read);
}
}
System.out.println("Command exited with: " + ps.waitFor());
} catch (IOException | InterruptedException exp) {
exp.printStackTrace();
}

已更新

由于我不知道的原因,通过 log4j 的 ConsoleAppender 发送的输出似乎没有到达 ProcessInputStream.. .

要解决此问题,您可以使用 ProcessBuilderinheritIO...

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class TestRunJar {

public static void main(String[] args) {
try {
ProcessBuilder pb = new ProcessBuilder(new String[]{"java", "-jar", "/../maltparser-1.8.jar"});
pb.inheritIO();
pb.redirectError();
pb.directory(new File("/..")); // Path to where maltparser-1.8.jar resides
Process ps = pb.start();

InputStreamConsumer stdout = new InputStreamConsumer(ps.getInputStream());
InputStreamConsumer stderr = new InputStreamConsumer(ps.getErrorStream());

stderr.start();
stdout.start();

stderr.join();
stderr.join();

System.out.println("Command exited with: " + ps.waitFor());
} catch (IOException | InterruptedException exp) {
exp.printStackTrace();
}
}

public static class InputStreamConsumer extends Thread {

private InputStream is;
private IOException exp;
private StringBuilder output;

public InputStreamConsumer(InputStream is) {
this.is = is;
}

@Override
public void run() {
int in = -1;
output = new StringBuilder(64);
try {
while ((in = is.read()) != -1) {
output.append((char) in);
}
} catch (IOException ex) {
ex.printStackTrace();
exp = ex;
}
}

public StringBuilder getOutput() {
return output;
}

public IOException getException() {
return exp;
}
}

}

这只是我的测试代码,所以可能有点过头了;)

最终被抛弃了......

-----------------------------------------------------------------------------
MaltParser 1.8
-----------------------------------------------------------------------------
MALT (Models and Algorithms for Language Technology) Group
Vaxjo University and Uppsala University
Sweden
-----------------------------------------------------------------------------

Usage:
java -jar maltparser-1.8.jar -f <path to option file> <options>
java -jar maltparser-1.8.jar -h for more help and options

help ( -h) : Show options
-----------------------------------------------------------------------------
option_file ( -f) : Path to option file
-----------------------------------------------------------------------------
verbosity *( -v) : Verbosity level
debug - Logging of debugging messages
error - Logging of error events
fatal - Logging of very severe error events
info - Logging of informational messages
off - Logging turned off
warn - Logging of harmful situations
-----------------------------------------------------------------------------

Documentation: docs/index.html
Command exited with: 0

关于java - 从 exec 运行 java -jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26644689/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com