24

I have an Eclipse plugin (A) which has a dependency on another plugin (B). Plugin B is simply a wrapper around a jar, which contains a native dll, and performs jni functionality. Given this setup, I have the following code in A's Activator class's start method:

MessageConsole jniConsole = new MessageConsole("Opereffa Output", null);
ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[] { jniConsole });
ConsolePlugin.getDefault().getConsoleManager().showConsoleView(jniConsole);
MessageConsoleStream stream = jniConsole.newMessageStream();
System.setOut(new PrintStream(stream));
System.setErr(new PrintStream(stream));

When plugin A performs its functionality, any use of System.out actually goes to the console within Eclipse. But native code used by JNI also writes to output stream, which I can't grab. During development, output from JNI goes to the console of the Eclipse instance which has launched the running instance, which contains the plugins.

So how do I grab the JNI output and display in the console?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
mahonya
  • 9,247
  • 7
  • 39
  • 68
  • 2
    What function(s) log messages in the native code? printf, fprintf, puts?... Would you be free to rewrite the code, changing the logging function? – Jean-Philippe Pellet Dec 13 '10 at 15:20
  • Since JNI is involved: How portable must be a solution? On what platforms should it work at least? – A.H. Oct 09 '11 at 11:07

3 Answers3

1

You could try using freopen to redirect stdout in exactly the same way as you do in Java, but on the native side. The question is whether this would work if you used it in your own plugin (with a new JNI dll): it may need to be used from within the dll doing the console output, I've no idea of the interaction between streams across DLLs. If stdout refers to a shared stream for the whole process, maybe it would work.

Kothar
  • 6,579
  • 3
  • 33
  • 42
0

You can't, really. The native DLL uses stdio methods that you can't access from Java. If you write to System.out, the Java runtime eventually uses the same methods but for obvious reasons, changes the System.out have no effect to underlying the C runtime.

There is a hardware solution: Get a second monitor so you can see the terminal in which you started Eclipse all the time.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
0

E.g. Eclipse 2019-06 regularly delegates std::printf(...) and std::cout from JNI code to the Console View

Flushing (=actually printed) is done by fflush(stdout);.

Sam Ginrich
  • 661
  • 6
  • 7