Write to console from a native javafx app created

2019-01-20 05:19发布

问题:

I have a JavaFX app, and I am using the maven plugin javafx-maven-plugin to create an app bundle (app.exe in Windows, generated with Inno Setup).

The app runs in console mode when arguments are given.

The problem is that when running in console mode, I can't see the mesages printed to console. The messages (written with System.out.println) don't appear in the Windows console. But they are generated, because if I redirect the output to a file (app.exe > out.txt) the file contains all the messages.

I have tried to run the .exe with cygwin and even compiled the whole project in Linux, and in both of them the output is correctly displayed in the console. So the problem seems to be only when running the javafx exe with the Windows console (cmd). I guess the stdout is redirected to somewhere. How can I change this?

回答1:

First of all: thanks for using the javafx-maven-plugin, I'm the maintainer of that maven-plugin.

short version: you can't do very much

long version: The problem comes with the native launcher of the JDK and has nothing todo with InnoSetup, nor Maven.

Quoting the source-code itself this is what happens:

Basic approach:
  - Launcher executable loads packager.dll/libpackager.dylib/libpackager.so and calls start_launcher below.
  - Reads app/package.cfg or Info.plist or app/<appname>.cfg for application launch configuration
     (package.cfg is property file).
  - Load JVM with requested JVM settings (bundled client JVM if availble, server or installed JVM otherwise).
  - Wait for JVM to exit and then exit from Main
  - To debug application by set env variable (TODO) or pass "/Debug" option on command line.
  - TODO: default directory is set to user's Documents and Settings.
  - Application folder is added to the library path (so LoadLibrary()) works.

After digging a bit inside the launcher, if found the spot, where the STD-output is retrieved, which gets compiled, because on windows-systems "USE_JLI_LAUNCH" is not set. The real problem with this comes with the condition to only append that console-writer when being compiled with DEBUG-flag

It might be a bug/fluke within the JDK itself, I'll try to find something and might file that as bug on oracle-bug-tracker.

EDIT: after some further digging, there is something I found interesting: the generated EXE-file is a simple windows-executable, no cli-executable as seen in the launcher-source-code, that is the reason you dont see any console-output but having the result when pipelining into some file.

Workaround: create/compile your own native launcher-file using some redirects as described here:

Redirecting cout to a console in windows

https://bobobobo.wordpress.com/2009/03/01/how-to-attach-a-console-to-your-gui-app-in-c/