4

I'm trying to recreate this tutorial on Windows: Java Native Interface (JNI)

When I try to compile it I get the following error:

fatal error: jni.h: No such file or directory

 #include <jni.h>

compilation terminated.

My command line is:

gcc -Wl,--add-stdcall-alias -IC:\Program_Files\Java\jdk1.7.0_45\include -IC:\Program_Files\Java\jdk1.7.0_45\include\win32 -shared -o hello.dll HelloJNI.c

I compile in the folder where all the files are.

I am sure that the file "jni.h" in this folder is located:

C:\Program Files\Java\jdk1.7.0_45\include

Does anyone know why the import statement does not work?

Thanks!

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
anmi
  • 123
  • 1
  • 1
  • 14
  • possible duplicate of [jni.h: no such file or directory](http://stackoverflow.com/questions/13466777/jni-h-no-such-file-or-directory) – Eel Lee Nov 07 '13 at 13:12
  • 2
    Your statement is that `jni.h` is in `C:\Program Files\Java\jdk1.7.0_45\include` however your compile line specifies `-IC:\Program_Files\Java\jdk1.7.0_45\include`. Does your gcc convert the underscore to a space for you? – mah Nov 07 '13 at 13:17
  • x86_64-w64-mingw32-gcc -I"C:\Program Files\Java\jdk1.8.0_151\include" -I"C:\Program Files\Java\jdk1.8.0_151\include\win32" -shared -o hello.dll HelloJNI.c - worked for me – Anand Kadhi May 02 '18 at 11:43

2 Answers2

8

Underscore is not the same as space. If jni.h really is in the "program files" directory you should use this command:

gcc -Wl,--add-stdcall-alias -I "C:\Program Files\Java\jdk1.7.0_45\include" -I "C:\Program Files\Java\jdk1.7.0_45\include\win32" -shared -o hello.dll HelloJNI.c

Joni
  • 108,737
  • 14
  • 143
  • 193
  • That is why I never, ever install anything into "C:/Program Files". – Ingo Nov 07 '13 at 13:37
  • 1
    I don't even *have* a "C:/Program Files" directory :D – Joni Nov 07 '13 at 13:39
  • I bet you have .... just because the explorer displays this as "C:\Programme" or something doesn't mean it isnt' there. Windows does a lot of black "magic" there. I whish they would either fire the cretin who designed all the crap with "Program Files" "Own Documents" etc., come up with sane symbolic links in NTFS and forget about any and all "Magic" pathnames. Better yet, adopt a real filesystem, and let the NTFS crap die. (Hell, they are still not case sensitive!) – Ingo Nov 07 '13 at 13:45
  • You seem to be assuming that I use Windows to begin with. – Joni Nov 07 '13 at 13:59
  • In that case you're clean, of course. To be sure, everybody who is doing more involved development, like OP, should not do it on Windows. – Ingo Nov 07 '13 at 14:36
  • So you are saying that the Windows platform is not suitable for serious developers? I'm pretty sure many people would disagree with you. – Joni Nov 07 '13 at 18:04
  • depends on what we understand by "serious". Yet, as soon as you need to leave your cozy IDE, you're lost on Windows, IMHO. – Ingo Nov 07 '13 at 18:09
1

I got the answer:

I just need to use quotation marks, replace the backslash with slash and replace the underscores with spaces.

The command looks like this:

gcc -Wl,--add-stdcall-alias -I"C:/Program Files/Java/jdk1.7.0_45/include" -I"C:/Program Files/Java/jdk1.7.0_45/include/win32" -shared -o hello.dll HelloJNI.c

Thanks to all!!

anmi
  • 123
  • 1
  • 1
  • 14
  • what it will be for linux environment ? – CoDe Jul 20 '15 at 10:41
  • In Linux it is different to Windows. I have never done this but maybe try this tutorial. http://stackoverflow.com/questions/3950635/how-to-compile-dynamic-library-for-a-jni-application-on-linux I hope this is helpful – anmi Jul 29 '15 at 18:57