为什么在使用ProcessBuilder时Java控制台不显示? [英] Why doesn't the Java console show when using ProcessBuilder?
问题描述
我使用以下命令从另一个Java应用程序启动Java应用程序。
I use the following to launch a Java application from another Java app.
ProcessBuilder pb = new ProcessBuilder(javaPath + javaCommand, maxMemStr,
minMemStr, stackSizeStr, jarCommand, jarfile, jarArg);
try {
Process p = pb.start();
} catch (IOException ex) {
Logger.getLogger(launch.class.getName()).log(Level.SEVERE, null, ex);
}
其中javaCommand是java或javaw(除非大多数情况下,javaPath为空,用户指向备用路径)。问题是,应用程序启动后,即使我确认进程列表中包含Java,它也不会显示控制台。
where javaCommand is either java or javaw (javaPath is empty most of the time unless a user points to an alternate path). The problem is, after the app launches, even when I verify the process list to contain java, it doesn't show the console.
是因为PrcoessBuilder不会调用命令外壳?有没有办法以编程方式显示控制台?
Is it because PrcoessBuilder doesn't invoke the command shell? Is there a way to show the console programatically?
预先感谢。
推荐答案
这是因为命令控制台本身是一个附加到另一个进程的std-in / out / -err流并将其显示在屏幕上的进程。单独启动Java时,没有其他进程将处理这些流,因此缺少命令控制台。为了获得所需的结果,您将需要启动命令控制台的新实例,然后让其运行您的自定义Java命令。
This is because the "command console" itself is a process that attaches to the std-in/-out/-err streams of another process and displays them on the screen. When you launch Java all by itself, no other processes will be handling those streams, hence the lack of a command console. To get the results you want, you will need to launch a new instance of the command console and subsequently have it run your custom java command.
也许有更好的方法为此...但是我认为解决方案将取决于平台。在Windows中,您可以执行以下操作:
There may be a better way to do this... but I think the solution to this is going to be platform-dependent. In Windows, you could do something like:
ProcessBuilder pb = new ProcessBuilder("start", "\"JAwesomeSauce\"", "cmd.exe",
"/k", javaPath + javaCommand, maxMemStr, minMemStr, stackSizeStr, jarCommand,
jarfile, jarArg);
try {
Process p = pb.start();
} catch (IOException ex) {
Logger.getLogger(launch.class.getName()).log(Level.SEVERE, null, ex);
}
我假设您可以在Linux / Mac中执行类似的操作您正在使用S。
I assume you could do something similar in Linux/Mac if that's the O/S you're using.
这篇关于为什么在使用ProcessBuilder时Java控制台不显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!