This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 254139 - Native print statements not appearing with Java JNI/JNA
Summary: Native print statements not appearing with Java JNI/JNA
Status: RESOLVED WORKSFORME
Alias: None
Product: platform
Classification: Unclassified
Component: Output Window (show other bugs)
Version: 8.0.2
Hardware: PC Linux
: P2 normal (vote)
Assignee: Jaroslav Havlin
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-08-05 01:44 UTC by JohnReedlol
Modified: 2015-09-14 13:52 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments
Sample Project (22.00 KB, application/zip)
2015-09-14 13:37 UTC, Jaroslav Havlin
Details

Note You need to log in before you can comment on or make changes to this bug.
Description JohnReedlol 2015-08-05 01:44:19 UTC
In the example below, when my Java code calls native code via JNA (Java Native Access), the print statements from the native code are not appearing in the Netbeans terminal and the application appears to stall.

http://stackoverflow.com/questions/31820668/java-jna-call-will-not-run-native-code-causes-application-to-stall/31821237#31821237

Note that when I run the jar file from the Linux terminal on Ubuntu Linux, printf statements seem to work fine, they only fail to appear in the Netbeans terminal. Can you verify whether this is a Netbeans bug or a bug in JNA?

Priority set to P2 because the only work around, running all Java Native Access applications via the terminal, can be highly impractical.

Product Version: NetBeans IDE 8.0.2 (Build 201411181905)
Updates: Updates available to version NetBeans 8.0.2 Patch 2
Java: 1.8.0_45; Java HotSpot(TM) 64-Bit Server VM 25.45-b02
Runtime: Java(TM) SE Runtime Environment 1.8.0_45-b14
System: Linux version 3.13.0-53-generic running on amd64; UTF-8; en_US (nb)
User directory: /home/user/.netbeans/8.0.2
Cache directory: /home/user/.cache/netbeans/8.0.2
Comment 1 Jaroslav Havlin 2015-09-14 13:37:34 UTC
Created attachment 156147 [details]
Sample Project

Attaching sample project.

NetBeans is not source of the problem here. Printing of the "printf" output is postponed always if the program is not run directly in terminal.

Try calling this command:
java -jar ./ViableExampleJava.jar | less

The output is not displayed in terminal directly, but piped to the less command.
The behavior is the same as when running in NetBeans. Printf output will be shown when the native method finishes.

I've tried similar scenario using JNI, and it works the same.

NetBeans Output Window doesn't emulate a terminal, it just shows process output.




Notes for building native libs and running Java application from terminal:
(Please replace paths with appropriate ones. Commands are applicable for Mac OS X.)

JNA sample:

~/NetBeansProjects/NativeApplication/build/classes$ java -cp /Users/name/Downloads/jna-4.1.0.jar:.:/Users/name/NetBeansProjects/NativeApplication/native/ viableexamplejava.ViableExample

~/NetBeansProjects/NativeApplication/native$ gcc -dynamiclib -I/System/Library/Frameworks/JavaVM.framework/Headers libViableExampleNative.c -framework JavaVM -o darwin/libViableExampleNative.jnilib


JNI sample:

~/NetBeansProjects/NativeApplication/build/classes$ java -cp /Users/name/Downloads/jna-4.1.0.jar:. -Djava.library.path=/Users/name/NetBeansProjects/NativeApplication/native/ nativeapplication.NativeApplication 

~/NetBeansProjects/NativeApplication/native$ gcc -dynamiclib -I/System/Library/Frameworks/JavaVM.framework/Headers mynativelib.c -framework JavaVM -o libmynativelib.jnilib
Comment 2 Jaroslav Havlin 2015-09-14 13:52:53 UTC
To prevent the problem, calling fflush seems to work fine:

printf("Textual output.")
fflush(stdout);

Alternatively, you can call this before the first printf:

setbuf(stdout, NULL);

(The workarounds are taken from [1]).

As this bug relates to default output buffering in native code rather than to NetBeans IDE, closing the issue as WORKSFORME.


[1] http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin