Search code examples
javacjava-native-interfacemingw

How to %JAVA_HOME% environment variable in MinGw shell


I installed MinGw on Windows 10, and followed some online instructions to create a JNI project. Although, I followed the instructions verbatim, gcc can not find the jni.h file. This file however is in the /include directory of my JAVA_HOME. Here are the commands I issued:

sansari@DESKTOP/c/Users/sansari/Documents/source/java
$ set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_91

sansari@DESKTOP/c/Users/sansari/Documents/source/java
$ echo %JAVA_HOME%
%JAVA_HOME%

sansari@DESKTOP/c/Users/sansari/Documents/source/java
$ gcc -Wl, --add-stdcall-alias -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" -shared -o hello.dll HelloJNI.c
gcc.exe: error: unrecognized command line option '--add-stdcall-alias'

sansari@DESKTOP/c/Users/sansari/Documents/source/java
$ gcc -Wl,--add-stdcall-alias -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" -shared -o hello.dll HelloJNI.c
HelloJNI.c:1:17: fatal error: jni.h: No such file or directory
 #include <jni.h>
                 ^
compilation terminated.

sansari@DESKTOP/c/Users/sansari/Documents/source/java

I am trying to make sure I know the file structure that needs to be in place for a JNI project, and also dig in a bit more on how to exchange different structures from JAVA classes to the native code and vise versa.

I also tried it this way:

$ gcc -Wl,--add-stdcall-alias -I"\include" -I"\include\win32" -shared -o hello.dll HelloJNI.c
HelloJNI.c:1:17: fatal error: jni.h: No such file or directory
 #include <jni.h>
                 ^
compilation terminate. 

@Chrono Kitsune- Thanks; Here is the result:

sansari@DESKTOP-/c/Users/sansari/Documents/source/java
$ export JAVA_HOME="C:\Program Files\Java\jdk1.8.0_91"

sansari@DESKTOP-/c/Users/sansari/Documents/source/java
$ gcc -Wl,--add-stdcall-alias -I"\include" -I"\include\win32" -shared -o hello.dll HelloJNI.c
HelloJNI.c:1:17: fatal error: jni.h: No such file or directory
 #include <jni.h>
                 ^
compilation terminated.

The strange thing is that I set the path to java directory also and reboot the machine. When I issue java, the shell finds the executable.

Environment Variables I added JAVA_HOME to my variables, and to my path variable. I still get the same message. I also rebooted the machine.

sansari@DESKTOP-/c/users/sansari/Documents/source/java
$ gcc -Wl,--add-stdcall-alias -I"\include" -I"\include\win32" -shared -o hello.dll HelloJNI.c
HelloJNI.c:1:17: fatal error: jni.h: No such file or directory
 #include <jni.h>
                 ^
compilation terminated.

sansari@DESKTOP-/c/users/sansari/Documents/source/java
$ gcc -Wl,--add-stdcall-alias -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" -shared -o hello.dll HelloJNI.c
HelloJNI.c:1:17: fatal error: jni.h: No such file or directory
 #include <jni.h>
                 ^
compilation terminated.

It worked!

sansari@DESKTOP-/c/users/sansari/Documents/source/java
$ gcc -Wl,--add-stdcall-alias -I"$JAVA_HOME\include" -I"$JAVA_HOME\include\win32" -shared -o hello.dll HelloJNI.c

sansari@DESKTOP-/c/users/sansari/Documents/source/java
$ ls
HelloJNI.c  HelloJNI.class  HelloJNI.h  HelloJNI.java  hello.dll

Solution

  • The MinGW shell uses a different syntax for variables -- the syntax specified for a POSIX shell. For example echo %PATH% doesn't work as it does in a Windows/DOS command shell. However, echo $PATH does.

    You'd need to use the POSIX shell syntax export VAR="value" instead of the Windows/DOS syntax set VAR="value".

    An alternative to avoid this step in future sessions is to add a new Windows user environment variable:

    1. Type Windows+R.
    2. Type control sysdm.cpl,3 to open "Advanced system settings".
    3. Click the Environment Variables button.
    4. Click the New button in the top section (it is labeled "User variables for <username>" in English releases.)
    5. Add your JAVA_HOME variable and its value.
    6. Click OK on all of the dialog boxes to save the changes.
    7. Open a new MinGW shell and test using echo $JAVA_HOME.
    8. You might need to close all MinGW shells or even log out and log in to effect the changes.