'Running JAR from BAT file cannot find natives path

I am running my JAR from a BAT file on Windows 7 x64 machine. Lately I added native .dll files to project, so now I need to add the path to the BAT. My project is exported to "C:/dist" and folder with native libs called "natives" placed alongside, that is "C:/dist/natives".

The problem is that for some reason the path is ignored, or cannot be found, or it seems something else is wrong in the BAT. This is my BAT code:

@ECHO OFF
java -XX:+UseG1GC -Xmx1G -server -Djava.library.path="C:/dist/natives" -jar data.jar
@if %errorlevel% neq 0 pause

When I run my project from the NetBeansIDE all works fine as expected. It is only when I try to run the compiled file from the BAT file it seems it cannot find the native libs when they are needed ending up with error.

I also tried add the path to the native libs programaticaly like this:

//PROGRAMATICALLY SET THE PATH TO NATIVE LIBRARIES
System.setProperty("java.library.path", "natives");
Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
fieldSysPath.setAccessible(true);
fieldSysPath.set(null, null);

It works as it should in NetBeansIDE but not once the jar is compiled and run via BAT file.



Solution 1:[1]

This might have the same problem I mentioned at the beginning of this post of mine, namely one of these:

  • unnecessary jar files in your project (they might cause problems similar to yours)
  • mixing different versions of LWJGL jar files (might cause problems similar to yours)
  • mixing different versions of LWJGL native .dll files (might cause problems similar to yours)
  • using unneeded native .dll files (sometime unnecessary ones may cause errors like yours)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1