如何在 runtome 期间运行其他主类并获取其控制台输出?
我有一个 jar 列表,其中包含具有所需主类的 jar。 我尝试运行它主类:
public static void runCommandlineMain(String classPathFile, String args[]) throws Exception{
URL[] urls = getJarUrls(classPathFile);
URLClassLoader classLoader = new URLClassLoader(urls);
Class<?> clazz = classLoader.loadClass("liquibase.commandline.Main");
Method main = clazz.getMethod("main", String[].class);
main.invoke(null, new Object[]{args});
}
但运行后我遵循:
Process finished with exit code -1
仅此而已。 我该如何解决它? 以及如何从此类中获取控制台输出?
I have a list of jars that contains jar with needed main-class.
I try to run it main class:
public static void runCommandlineMain(String classPathFile, String args[]) throws Exception{
URL[] urls = getJarUrls(classPathFile);
URLClassLoader classLoader = new URLClassLoader(urls);
Class<?> clazz = classLoader.loadClass("liquibase.commandline.Main");
Method main = clazz.getMethod("main", String[].class);
main.invoke(null, new Object[]{args});
}
But after run I've follow:
Process finished with exit code -1
and nothing more.
How can i resolve it? And how to get console out put from this class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不太明白你的第一个问题。
但是,要回答你的第二个问题:“如何获得控制台输出”,
请尝试 StreamGobbler 此处
Didn't quite get your first question.
But, to answer your second question: "How do you get console output"
try the StreamGobbler here
我也不确定问题是什么,但根据发布的片段,应该捕获输出的进程只是在不同的线程中运行。 在这种情况下,您应该能够仅重定向 Sytem.out(或分别为 System.err)流。
在调用 Main 类之前调用
,其输出应该重定向到您的自定义 OutputStream。
I'm not sure about what the question is about too, but according to the posted snipped, the process which should have the output captured just runs in a different thread. In this scenario you should be able to just redirect the Sytem.out (or System.err respectively) stream.
Call
before invoking the Main class and its output should get redirected to your custom OutputStream.