Java ProcessBuilder showing console of started java application?

回眸只為那壹抹淺笑 提交于 2019-12-06 06:07:39

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.

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.

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.

#!/bin/bash
# To open a process in a new window.
gnome-terminal -x ./your-real-shell-script-here.sh "$@"

the real script will have your java execution in it, such as:

#!/bin/bash
java -jar your-jar-file.jar "$@"

I think you can use javaw to run on windows, so you might only need the one shell script.

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:

java -classpath="..." com.example.appName arg1

If you want to run in a different manner, sorry to say, logging to Console is not for you. Instead, log using one of:

  • log4j
  • slf4j
  • logback
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!