Java ProcessBuilder 显示启动的 java 应用程序的控制台?
我有一个 JAVA 应用程序,它启动(使用 ProcessBuilder)另一个 JAVA 应用程序,如下所示:
String val = "something";
ProcessBuilder processBuilder = new ProcessBuilder("java", "-classpath", dir, appName, val);
Process p = processBuilder.start();
现在,这工作正常,appName 使用参数 val 启动,它运行并工作......很棒......问题是没有出现控制台窗口。 .. appName 做了很多输出到控制台,我们需要查看它......我如何使用控制台启动该过程?
我正在尝试诸如(“CMD.exe”,“java”,“-classpath”,dir,appName,val)等之类的东西...但我无法正确...
而且,我无法重定向在流中,我的程序实际上可以启动 5-10 个这样的 appName,每个应用程序都应该有自己的控制台窗口,显示自己的信息。
任何帮助将不胜感激。 谢谢,
I have a JAVA application that launches (using ProcessBuilder) another JAVA application like this:
String val = "something";
ProcessBuilder processBuilder = new ProcessBuilder("java", "-classpath", dir, appName, val);
Process p = processBuilder.start();
Now, this works fine, appName is launched with the parameter val and it runs and works ... great ... the problem is no Console Window appears ... appName does a LOT of outputting to the console and we need to see it ... how can I start the process with a console?
I am trying stuff like ("CMD.exe", "java", "-classpath", dir, appName, val), etc... but I can't get it right ...
Also, I can't redirect the streams, my program can actually start 5-10 of these appName's, each should have their own console window showing their own information.
Any help would be much appreciated.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
控制台窗口通常不是最可靠的日志记录形式。它们仅存储一定量的信息(缓冲区),并且跨平台的行为可能有所不同。
我强烈建议使用 log4j 之类的东西记录到文件,如果您需要实时查看它,请使用一个类似尾部的程序(我看到你正在使用Windows)。
除此之外,鉴于您希望窗口始终可见并为每个日志启动尾部程序可能很烦人,我会在 java swing 中编写自己的日志窗口。
基本思想是不要过多依赖操作系统。
console windows are generally not the most reliable form of logging. they only store a set amount of information (buffer) and can behave differently across platforms.
i strongly suggest logging to a file using something like log4j and if you need to see it real time use a tail like program (i see you're using windows).
in addition to this, seeing as you want the windows visible at all times and launching a tail program for each log might be annoying, i'd write my own log window in java swing.
the basic idea is to not rely on the OS too much.
尝试过 Runtime.getRuntime().exec("cscript java -classpath ..."); ?
不管怎样,考虑使用日志框架(log4j、commons-logging),因为打开 5 个控制台并不是最明智的做法。
Tried
Runtime.getRuntime().exec("cscript java -classpath ...");
?Anyway, consider using a logging framwork (log4j, commons-logging), because opening 5 consoles is not the most clever thing to do.
我通过 Process 调用一些 shell 脚本来打开命令行窗口并启动我需要的任何内容。只要脚本不分离 - 您通常可以停止任何 shell 命令执行此操作 -java 仍将保留正在运行的进程。
我是在linux下做的,但概念应该是相似的。
真正的脚本中会有你的java执行,例如:
我认为你可以使用javaw在windows上运行,所以你可能只需要一个shell脚本。
I call a few shell scripts via Process to open a command line window and launch whatever I need. As long as the scripts don't detach - you can usually stop any shell command from doing this -java will still hold the running process.
I did it in linux but the concept should be similar.
the real script will have your java execution in it, such as:
I think you can use javaw to run on windows, so you might only need the one shell script.
Console 对象仅在您从控制台执行 java.... 时才存在。否则,获取该对象的调用将返回 null。
如果你想看到控制台,你需要打开一个命令 shell 控制台(例如 windows cmd.exe 或 Unix bash shell 窗口)并输入:
如果你想以不同的方式运行,抱歉地说,登录到控制台不是为你。相反,使用以下之一进行记录:
A Console object only exists when you execute java.... from a console. Otherwise, the call to obtain one returns null.
If you want to see a console, you need to open a command shell console (e.g. windows cmd.exe or Unix bash shell window) and type:
If you want to run in a different manner, sorry to say, logging to Console is not for you. Instead, log using one of: