0

I am trying to follow this tutorial to build a JNI project from a Windows 10 machine with CYGWIN and MinGW installed:

https://www.baeldung.com/jni

I have managed to create the Java file HelloWorldJNI.java and generated the header file HelloWorldJNI.h.

The 'HelloWorldJNI.java' file:

public class HelloWorldJNI {

    static {
        System.loadLibrary("native");
    }
    
    public static void main(String[] args) {
        new HelloWorldJNI().sayHello();
    }

    // Declare a native method sayHello() that receives no arguments and returns void
    private native void sayHello();
}

The generated header file 'HelloWorldJNI.h':

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloWorldJNI */

#ifndef _Included_HelloWorldJNI
#define _Included_HelloWorldJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     HelloWorldJNI
 * Method:    sayHello
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_HelloWorldJNI_sayHello
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

The 'HelloWorldJNI.cpp' C++ file:

#include "HelloWorldJNI.h"
#include <stdio.h>

JNIEXPORT void JNICALL Java_HelloWorldJNI_sayHello(JNIEnv* env, jobject thisObject) {
    printf("Hello World");
}

I attempted to use G++ compiler to compile the 'HelloWorldJNI.cpp' and received a bunch of errors:

C:\Users\Administrator\Desktop>g++ -c -I"C:\Program Files\Java\jdk-16.0.1\include" -I"C:\Program Files\Java\jdk-16.0.1\include\win32" HelloWorldJNI.cpp -o HelloWorldJNI.o
In file included from C:\Program Files\Java\jdk-16.0.1\include/jni.h:44,
                 from HelloWorldJNI.h:2,
                 from HelloWorldJNI.cpp:1:
C:\Program Files\Java\jdk-16.0.1\include\win32/jni_md.h:36:9: error: '__int64' does not name a type; did you mean '__int64_t'?
   36 | typedef __int64 jlong;
      |         ^~~~~~~
      |         __int64_t
In file included from HelloWorldJNI.h:2,
                 from HelloWorldJNI.cpp:1:
C:\Program Files\Java\jdk-16.0.1\include/jni.h:125:5: error: 'jlong' does not name a type; did you mean 'ulong'?
  125 |     jlong    j;
      |     ^~~~~
      |     ulong
C:\Program Files\Java\jdk-16.0.1\include/jni.h:333:20: error: expected identifier before '*' token
  333 |     jlong (JNICALL *CallLongMethod)
      |                    ^
C:\Program Files\Java\jdk-16.0.1\include/jni.h:333:21: warning: 'stdcall' attribute only applies to function types [-Wattributes]
  333 |     jlong (JNICALL *CallLongMethod)
      |                     ^~~~~~~~~~~~~~
C:\Program Files\Java\jdk-16.0.1\include/jni.h:334:57: error: 'jlong' declared as function returning a function
  334 |       (JNIEnv *env, jobject obj, jmethodID methodID, ...);
      |                                                         ^
C:\Program Files\Java\jdk-16.0.1\include/jni.h:335:20: error: expected identifier before '*' token
  335 |     jlong (JNICALL *CallLongMethodV)
      |                    ^
C:\Program Files\Java\jdk-16.0.1\include/jni.h:335:21: warning: 'stdcall' attribute only applies to function types [-Wattributes]
  335 |     jlong (JNICALL *CallLongMethodV)
      |                     ^~~~~~~~~~~~~~~
C:\Program Files\Java\jdk-16.0.1\include/jni.h:336:66: error: 'jlong' declared as function returning a function
  336 |       (JNIEnv *env, jobject obj, jmethodID methodID, va_list args);
      |                                                                  ^
C:\Program Files\Java\jdk-16.0.1\include/jni.h:337:20: error: expected identifier before '*' token
  337 |     jlong (JNICALL *CallLongMethodA)
      |                    ^
C:\Program Files\Java\jdk-16.0.1\include/jni.h:337:21: warning: 'stdcall' attribute only applies to function types [-Wattributes]
  337 |     jlong (JNICALL *CallLongMethodA)
      |                     ^~~~~~~~~~~~~~~
C:\Program Files\Java\jdk-16.0.1\include/jni.h:338:72: error: 'jlong' declared as function returning a function
  338 |       (JNIEnv *env, jobject obj, jmethodID methodID, const jvalue *args);
      |                                                                        ^
C:\Program Files\Java\jdk-16.0.1\include/jni.h:415:20: error: expected identifier before '*' token
  415 |     jlong (JNICALL *CallNonvirtualLongMethod)
      |                    ^
C:\Program Files\Java\jdk-16.0.1\include/jni.h:415:21: warning: 'stdcall' attribute only applies to function types [-Wattributes]
  415 |     jlong (JNICALL *CallNonvirtualLongMethod)
      |                     ^~~~~~~~~~~~~~~~~~~~~~~~
C:\Program Files\Java\jdk-16.0.1\include/jni.h:416:71: error: 'jlong' declared as function returning a function
  416 |       (JNIEnv *env, jobject obj, jclass clazz, jmethodID methodID, ...);
      |                                                                       ^
C:\Program Files\Java\jdk-16.0.1\include/jni.h:417:20: error: expected identifier before '*' token
  417 |     jlong (JNICALL *CallNonvirtualLongMethodV)
      |                    ^
C:\Program Files\Java\jdk-16.0.1\include/jni.h:417:21: warning: 'stdcall' attribute only applies to function types [-Wattributes]
  417 |     jlong (JNICALL *CallNonvirtualLongMethodV)
      |                     ^~~~~~~~~~~~~~~~~~~~~~~~~
C:\Program Files\Java\jdk-16.0.1\include/jni.h:419:20: error: 'jlong' declared as function returning a function
  419 |        va_list args);
      |                    ^
C:\Program Files\Java\jdk-16.0.1\include/jni.h:420:20: error: expected identifier before '*' token
  420 |     jlong (JNICALL *CallNonvirtualLongMethodA)
      |                    ^
C:\Program Files\Java\jdk-16.0.1\include/jni.h:420:21: warning: 'stdcall' attribute only applies to function types [-Wattributes]
  420 |     jlong (JNICALL *CallNonvirtualLongMethodA)
      |                     ^~~~~~~~~~~~~~~~~~~~~~~~~
C:\Program Files\Java\jdk-16.0.1\include/jni.h:422:26: error: 'jlong' declared as function returning a function
  422 |        const jvalue *args);
      |                          ^
C:\Program Files\Java\jdk-16.0.1\include/jni.h:466:20: error: expected identifier before '*' token
  466 |     jlong (JNICALL *GetLongField)
      |                    ^
C:\Program Files\Java\jdk-16.0.1\include/jni.h:466:21: warning: 'stdcall' attribute only applies to function types [-Wattributes]
  466 |     jlong (JNICALL *GetLongField)
      |                     ^~~~~~~~~~~~
C:\Program Files\Java\jdk-16.0.1\include/jni.h:467:50: error: 'jlong' declared as function returning a function
  467 |       (JNIEnv *env, jobject obj, jfieldID fieldID);
      |                                                  ^
C:\Program Files\Java\jdk-16.0.1\include/jni.h:486:52: error: 'jlong' has not been declared
  486 |       (JNIEnv *env, jobject obj, jfieldID fieldID, jlong val);
      |                                                    ^~~~~
C:\Program Files\Java\jdk-16.0.1\include/jni.h:537:20: error: expected identifier before '*' token
  537 |     jlong (JNICALL *CallStaticLongMethod)
      |                    ^
C:\Program Files\Java\jdk-16.0.1\include/jni.h:537:21: warning: 'stdcall' attribute only applies to function types [-Wattributes]
  537 |     jlong (JNICALL *CallStaticLongMethod)
      |                     ^~~~~~~~~~~~~~~~~~~~
C:\Program Files\Java\jdk-16.0.1\include/jni.h:538:58: error: 'jlong' declared as function returning a function
  538 |       (JNIEnv *env, jclass clazz, jmethodID methodID, ...);
      |                                                          ^
C:\Program Files\Java\jdk-16.0.1\include/jni.h:539:20: error: expected identifier before '*' token
  539 |     jlong (JNICALL *CallStaticLongMethodV)
      |                    ^
C:\Program Files\Java\jdk-16.0.1\include/jni.h:539:21: warning: 'stdcall' attribute only applies to function types [-Wattributes]
  539 |     jlong (JNICALL *CallStaticLongMethodV)
      |                     ^~~~~~~~~~~~~~~~~~~~~
C:\Program Files\Java\jdk-16.0.1\include/jni.h:540:67: error: 'jlong' declared as function returning a function
  540 |       (JNIEnv *env, jclass clazz, jmethodID methodID, va_list args);
      |                                                                   ^
C:\Program Files\Java\jdk-16.0.1\include/jni.h:541:20: error: expected identifier before '*' token
  541 |     jlong (JNICALL *CallStaticLongMethodA)
      |                    ^
C:\Program Files\Java\jdk-16.0.1\include/jni.h:541:21: warning: 'stdcall' attribute only applies to function types [-Wattributes]
  541 |     jlong (JNICALL *CallStaticLongMethodA)
      |                     ^~~~~~~~~~~~~~~~~~~~~
C:\Program Files\Java\jdk-16.0.1\include/jni.h:542:73: error: 'jlong' declared as function returning a function
  542 |       (JNIEnv *env, jclass clazz, jmethodID methodID, const jvalue *args);
      |                                                                         ^
C:\Program Files\Java\jdk-16.0.1\include/jni.h:579:20: error: expected identifier before '*' token
  579 |     jlong (JNICALL *GetStaticLongField)
      |                    ^
C:\Program Files\Java\jdk-16.0.1\include/jni.h:579:21: warning: 'stdcall' attribute only applies to function types [-Wattributes]
  579 |     jlong (JNICALL *GetStaticLongField)
      |                     ^~~~~~~~~~~~~~~~~~
C:\Program Files\Java\jdk-16.0.1\include/jni.h:580:51: error: 'jlong' declared as function returning a function
  580 |       (JNIEnv *env, jclass clazz, jfieldID fieldID);
      |                                                   ^
C:\Program Files\Java\jdk-16.0.1\include/jni.h:599:53: error: 'jlong' has not been declared
  599 |       (JNIEnv *env, jclass clazz, jfieldID fieldID, jlong value);
      |                                                     ^~~~~
C:\Program Files\Java\jdk-16.0.1\include/jni.h:661:5: error: 'jlong' does not name a type; did you mean 'ulong'?
  661 |     jlong * (JNICALL *GetLongArrayElements)
      |     ^~~~~
      |     ulong
C:\Program Files\Java\jdk-16.0.1\include/jni.h:679:39: error: 'jlong' has not been declared
  679 |       (JNIEnv *env, jlongArray array, jlong *elems, jint mode);
      |                                       ^~~~~
C:\Program Files\Java\jdk-16.0.1\include/jni.h:696:63: error: 'jlong' has not been declared
  696 |       (JNIEnv *env, jlongArray array, jsize start, jsize len, jlong *buf);
      |                                                               ^~~~~
C:\Program Files\Java\jdk-16.0.1\include/jni.h:713:69: error: 'jlong' does not name a type; did you mean 'ulong'?
  713 |       (JNIEnv *env, jlongArray array, jsize start, jsize len, const jlong *buf);
      |                                                                     ^~~~~
      |                                                                     ulong
C:\Program Files\Java\jdk-16.0.1\include/jni.h:757:37: error: 'jlong' has not been declared
  757 |        (JNIEnv* env, void* address, jlong capacity);
      |                                     ^~~~~
C:\Program Files\Java\jdk-16.0.1\include/jni.h:760:20: error: expected identifier before '*' token
  760 |     jlong (JNICALL *GetDirectBufferCapacity)
      |                    ^
C:\Program Files\Java\jdk-16.0.1\include/jni.h:760:21: warning: 'stdcall' attribute only applies to function types [-Wattributes]
  760 |     jlong (JNICALL *GetDirectBufferCapacity)
      |                     ^~~~~~~~~~~~~~~~~~~~~~~
C:\Program Files\Java\jdk-16.0.1\include/jni.h:761:33: error: 'jlong' declared as function returning a function
  761 |        (JNIEnv* env, jobject buf);
      |                                 ^
C:\Program Files\Java\jdk-16.0.1\include/jni.h:1004:5: error: 'jlong' does not name a type; did you mean 'ulong'?
 1004 |     jlong CallLongMethod(jobject obj, jmethodID methodID, ...) {
      |     ^~~~~
      |     ulong
C:\Program Files\Java\jdk-16.0.1\include/jni.h:1012:5: error: 'jlong' does not name a type; did you mean 'ulong'?
 1012 |     jlong CallLongMethodV(jobject obj, jmethodID methodID,
      |     ^~~~~
      |     ulong
C:\Program Files\Java\jdk-16.0.1\include/jni.h:1016:5: error: 'jlong' does not name a type; did you mean 'ulong'?
 1016 |     jlong CallLongMethodA(jobject obj, jmethodID methodID,
      |     ^~~~~
      |     ulong
C:\Program Files\Java\jdk-16.0.1\include/jni.h:1196:5: error: 'jlong' does not name a type; did you mean 'ulong'?
 1196 |     jlong CallNonvirtualLongMethod(jobject obj, jclass clazz,
      |     ^~~~~
      |     ulong
C:\Program Files\Java\jdk-16.0.1\include/jni.h:1206:5: error: 'jlong' does not name a type; did you mean 'ulong'?
 1206 |     jlong CallNonvirtualLongMethodV(jobject obj, jclass clazz,
      |     ^~~~~
      |     ulong
C:\Program Files\Java\jdk-16.0.1\include/jni.h:1211:5: error: 'jlong' does not name a type; did you mean 'ulong'?
 1211 |     jlong CallNonvirtualLongMethodA(jobject obj, jclass clazz,
      |     ^~~~~
      |     ulong
C:\Program Files\Java\jdk-16.0.1\include/jni.h:1304:5: error: 'jlong' does not name a type; did you mean 'ulong'?
 1304 |     jlong GetLongField(jobject obj, jfieldID fieldID) {
      |     ^~~~~
      |     ulong
C:\Program Files\Java\jdk-16.0.1\include/jni.h:1338:23: error: 'jlong' has not been declared
 1338 |                       jlong val) {
      |                       ^~~~~
C:\Program Files\Java\jdk-16.0.1\include/jni.h:1463:5: error: 'jlong' does not name a type; did you mean 'ulong'?
 1463 |     jlong CallStaticLongMethod(jclass clazz,
      |     ^~~~~
      |     ulong
C:\Program Files\Java\jdk-16.0.1\include/jni.h:1472:5: error: 'jlong' does not name a type; did you mean 'ulong'?
 1472 |     jlong CallStaticLongMethodV(jclass clazz,
      |     ^~~~~
      |     ulong
C:\Program Files\Java\jdk-16.0.1\include/jni.h:1476:5: error: 'jlong' does not name a type; did you mean 'ulong'?
 1476 |     jlong CallStaticLongMethodA(jclass clazz,
      |     ^~~~~
      |     ulong
C:\Program Files\Java\jdk-16.0.1\include/jni.h:1554:5: error: 'jlong' does not name a type; did you mean 'ulong'?
 1554 |     jlong GetStaticLongField(jclass clazz, jfieldID fieldID) {
      |     ^~~~~
      |     ulong
C:\Program Files\Java\jdk-16.0.1\include/jni.h:1589:25: error: 'jlong' has not been declared
 1589 |                         jlong value) {
      |                         ^~~~~
C:\Program Files\Java\jdk-16.0.1\include/jni.h:1683:5: error: 'jlong' does not name a type; did you mean 'ulong'?
 1683 |     jlong * GetLongArrayElements(jlongArray array, jboolean *isCopy) {
      |     ^~~~~
      |     ulong
C:\Program Files\Java\jdk-16.0.1\include/jni.h:1719:35: error: 'jlong' has not been declared
 1719 |                                   jlong *elems,
      |                                   ^~~~~
C:\Program Files\Java\jdk-16.0.1\include/jni.h:1755:53: error: 'jlong' has not been declared
 1755 |                             jsize start, jsize len, jlong *buf) {
      |                                                     ^~~~~
C:\Program Files\Java\jdk-16.0.1\include/jni.h:1788:35: error: 'jlong' does not name a type; did you mean 'ulong'?
 1788 |                             const jlong *buf) {
      |                                   ^~~~~
      |                                   ulong
C:\Program Files\Java\jdk-16.0.1\include/jni.h:1851:48: error: 'jlong' has not been declared
 1851 |     jobject NewDirectByteBuffer(void* address, jlong capacity) {
      |                                                ^~~~~
C:\Program Files\Java\jdk-16.0.1\include/jni.h:1857:5: error: 'jlong' does not name a type; did you mean 'ulong'?
 1857 |     jlong GetDirectBufferCapacity(jobject buf) {
      |     ^~~~~
      |     ulong

How do I fix the issue with the compilation ?

thotheolh
  • 7,040
  • 7
  • 33
  • 49
  • 2
    Looks like it's trying to use `__int64`, which is windows-specific. You can include `_mingw.h` to fix this (usually), but then it's no longer cross-platform code. – lionkor Jul 05 '21 at 12:40
  • 2
    I just saw you might not be in mingw... Maybe just `#include `, then? – lionkor Jul 05 '21 at 12:42
  • I have included both #include and #include <_mingw.h> in the cpp file and its still the same error. – thotheolh Jul 05 '21 at 12:51
  • It seems you are trying to use header files designed for Visual Studio, with a G++ compiler instead – user253751 Jul 05 '21 at 12:52
  • @user253751 I am using a Java JNI generated header file that the javac -h automatically creates from the above Java class file with native JNI call. – thotheolh Jul 05 '21 at 12:53
  • Maybe I need to bring in some Microsoft libraries on g++ with Mingw-w64 running on Windows 10 ? I believe g++ is trying to guess __int64 which is a Microsoft type from the error message ? – thotheolh Jul 05 '21 at 12:57
  • @thotheolh you are also using all the header files that are part of Java itself. The ones in `C:\Program Files\Java` – user253751 Jul 05 '21 at 13:00
  • 1
    Have you try to simply define/typedef `__int64` as `int64_t` before jni.h include ? – Zilog80 Jul 05 '21 at 13:22
  • 1
    I managed to get the JNI DLL to work by doing this in the header: #include #define __int64 int64_t. Also, this seems to be a duplicate issue of: https://stackoverflow.com/questions/36846746/jni-cannot-detect-int64-on-netbeans – thotheolh Jul 06 '21 at 03:44
  • Oracle seems to also mention it in #10 of their website: https://www.oracle.com/java/technologies/jni-j2sdk-faq.html – thotheolh Jul 06 '21 at 03:45

0 Answers0