How do I redirect terminal output from a C program to System.out with JNI?

I am invoking a C library via JNI that prints to stdout. How can I redirect this output to System.out?


Asked by: Justin366 | Posted: 28-01-2022






Answer 1

System.out is stdout. Is there some more fundamental problem you're having (mixed up output, perhaps?).

Since another member has also mentioned that last point - I should explain further:

System.out and stdout both correspond to file descriptor #1.

However both Java's OutputStream (and derived classes) and C's stdio library have their own (independent) buffering mechanisms so as to reduce the number of calls to the underlying write system call. Just because you've called printf or similar, it's not guaranteed that your output will appear straightaway.

Because these buffering methods are independent, output from within Java code could (in theory) get mixed up or otherwise appear out-of-order relative to output from the C code.

If that's a concern, you should arrange to call System.out.flush() before calling your JNI function, and in your C function (if it's using stdio rather than the low-level write call) you should call fflush(stdout) before returning.

Answered by: Marcus916 | Posted: 01-03-2022



Answer 2

As Alnitak wrote, you should print to stdout. You should note that it can take a while for the message to appear on the screen. In case the timing is important, you should print a timestamp with the message when you print to stdout.

Answered by: Elise415 | Posted: 01-03-2022



Similar questions

In Java, how can I redirect System.out to null then back to stdout again?

I've tried to temporarily redirect System.out to /dev/null using the following code but it doesn't work. System.out.println("this should go to stdout"); PrintStream original = System.out; System.setOut(new PrintStream(new FileOutputStream("/dev/null"))); System.out.println("this should go to /dev/null"); System.setOut(original); System.out.println("this should go to stdout"); // This is not getting printe...


swing - Java System.out redirect issue

I've written an recursive directory search that, for each dir that matches a certain pattern, will execute a .bat. Now I want to redirect messages printed to the java console (no need to write standardErr) to an JTextArea. The following code is for the redirection part of the GUI. taConsole = new JTextArea(); taConsole.setEditable(false); PrintStream printStream = new PrintStream(new CustomOutputStream(taCo...


linux redirect java System.out to file

I running jar via "start-stop-daemon". My code: PIDFILE=/var/run/$NAME.pid USER=root MAIN="/usr/share/folder/jarfile.jar" DAEMON="/usr/bin/java" ARGS="-server -Djava.awt.headless=true -jar $MAIN" start() { start-stop-daemon --start --pidfile "$PIDFILE" --chuid "$USER" --background --make-pidfile --startas "$DAEMON" -- $ARGS } stop() { start-stop-daemon --stop --quiet --pidfile $PIDFILE if [ -e...


java - How to redirect System.out to a log file using Logback?

How can I use Logback to capture System.out messages in a Java program? For example, I would like to use this code: System.out.println("test: console out to file instead"); ... and capture it's output to a file. Can this be done using the logback.xml config file?


java - How to redirect System.out using different thread in JavaFX with FXML

I have multiple tabs and one of them is my console output. I am trying to redirect System.out/err to TextArea loggerPane in that console output Tab using JavaFX. My question is how can I do that in a different thread because while it's outputting I can't navigate to the console tab. Main App: public initRootLayout(){ FXMLoader loader = new FXMLLoader(); ...


java - How to redirect System.out to Play Logger?

This question already has answers here:


java - Why the awkward Design of System.out?

Has anyone got an idea regarding the motivation behind the awkward design of the java.lang.System.out? Awkwardness: First, the out member is exposed (Encapsulation anyone?). Second, it is final but can be changed via setOut() (contradicts final).


java - System.out not working when calling jar-file from Windows command line

I have this class: public class Test { public static void main(String[] args) { System.out.println("Hello World!"); } } And I used Eclipse's "Export to runnable JAR" function to make it into a executable jar-file (test.jar). When I run this program in Eclipse, it prints out "Hello World!", but when I call the jar-file from the command line, nothing happens. I have t...


logging - Removing access to System.out in java

I maintain an application which acts as a container for multiple individual programs. These programs have their own dedicated logging facility, i.e. everything they log does to a special log file. Nevertheless, application developers seem to love to throw System.out.println and e.printStackTrace calls all over, making it impossible to maintain a clean console when running the cont...


console - Finding Errant Output to System.out in Large Java Program

We have a large java code base [~1 M Lines]. Buried (somewhere) in the code base is some old debug output to System.out that we want to remove (its cluttering things up). The problem is: out code base is so large that we can't easily find where the output is coming from. What we want is a way to see where System.out.println is getting called from (like a stack trace from an exception or some such).


System.out to a file in java

I'm running an application from inside another one for testing purposes. I want to redirect the output for the tested app to a file, so I can have a log after each test. Is there a way to redirect the output of an app to a file from the command line in java?


command line - Java execution details in System.out

As I remember there is a magic command line option in Java that turns on writing of operations that are currently executed to the console. The output looks like byte code. -verbose does not match as it prints only class loading, while this option outputs information like memory allocation, setting local variables etc. It was very detailed, like 10 lines for "Hello world". I didn't find it here...


java - Warning in System.out when an error is logged in a custom log file

currently we're running quite a few applications on our WebSphere Portal Server, and most of the have their own log-file, so the System.out won't be too overloaded. However, once an error happens in an application it's easy to miss. If an errors is logged, I would like to get a warning in the System.out with a notice to take a look at the related log-file. For now I did this by wrapping the log4j logger into a class with e...


java - Are System.out, stdout and cout the exact same thing?

Are System.out, stdout and cout the EXACT same thing in Java, C and C++ respectively? Why have three different names for the same thing (especially when C, C++ and Java have much in common)? Also, I know what they are used for but what are they exactly, under the hood, I mean?


java - System.out with Ant

I'm googling without success to figure out how to make ANT print System.out.println/System.out.print messages in the console. Messages simply don't appear. I haven't found any simple way of doing this. Is there any? Thanks


In Java, how can I redirect System.out to null then back to stdout again?

I've tried to temporarily redirect System.out to /dev/null using the following code but it doesn't work. System.out.println("this should go to stdout"); PrintStream original = System.out; System.setOut(new PrintStream(new FileOutputStream("/dev/null"))); System.out.println("this should go to /dev/null"); System.setOut(original); System.out.println("this should go to stdout"); // This is not getting printe...






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top