使用JDK 9 + NetBeans Dev进行JNI教程时遇编译错误求助
Hey there, sorry to hear you’ve been stuck on this JNI compilation error for two days—let’s dig into this together, since JNI can be pretty finicky when mixing older NetBeans builds with JDK 9.
First, let’s recap your setup to make sure we’re aligned:
- NetBeans Dev Build 201803200002
- Java JDK 9.0.4
- Windows 10 64-bit
- Working through the NetBeans beginner JNI tutorial, hitting a compile error near the section below Figure 13
Since you didn’t share the exact error message, here are the most common issues that pop up at this stage of the tutorial, along with actionable fixes:
1. Mismatched 32/64-bit Architecture
JDK 9.0.4 is 64-bit, so your C/C++ toolchain must match this architecture to avoid linker or compilation failures:
- Right-click your C/C++ project → Properties → Build → Tool Collection
- Ensure you’ve selected a 64-bit compiler (like MSVC 64-bit or MinGW-w64) instead of a 32-bit variant. A mismatch here will prevent the native library from linking correctly with your Java code.
2. Incorrect JNI Header Paths
The tutorial depends on access to JNI headers from your JDK. Double-check your project’s include directories:
- For the C/C++ project, go to Properties → Build → C Compiler → Include Directories
- Verify that
${JAVA_HOME}/includeand${JAVA_HOME}/include/win32are added (replace${JAVA_HOME}with your actual JDK 9.0.4 path, e.g.,C:\Program Files\Java\jdk-9.0.4) - Missing or incorrect paths will trigger errors like
jni.h: No such file or directoryor undefined references to JNI functions.
3. JDK 9 Module System Conflicts
JDK 9 introduced the module system, which can clash with older NetBeans builds that weren’t fully updated to support it:
- When running your Java application, add the VM option
--add-modules java.base(right-click Java project → Properties → Run → VM Options). This ensures core JNI-related modules are accessible. - Also, specify the native library path with
-Djava.library.path=C:\path\to\your\native\project\dist(replace with your actual build output directory) so the JVM can locate your.dllfile.
4. Stale Build Artifacts or NetBeans Dev Quirks
The 2018 NetBeans Dev build is quite old and may have subtle bugs with JDK 9 integration:
- Clean both your Java and C/C++ projects (Build → Clean Project) then rebuild from scratch. Stale files often cause unexpected compilation errors.
- Double-check that the native method signatures in your Java code exactly match those in the generated header file. Even a tiny typo (case sensitivity, parameter types) will lead to linker errors when the JVM can’t find the native implementation.
If you can share the exact error output from the NetBeans console, that would help us narrow this down even faster!
内容的提问来源于stack exchange,提问作者el platin

