0

Quite lost here. So I have this Spring application that on the start loads the .dll of mine (custom one) in such manner in the main class:

static {
    File f = new File("src/lib/lib.dll");
    if(f.exists() && !f.isDirectory()) {
        String libDllPath = f.getParentFile().getAbsolutePath();
        log.info("libDllPath: {}", libDllPath);
        System.setProperty( "java.library.path", libDllPath);
        Field fieldSysPath = null;
        try {
            fieldSysPath = ClassLoader.class.getDeclaredField( "sys_paths" );
            fieldSysPath.setAccessible( true );
            fieldSysPath.set( null, null );
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
        log.info("\"java.library.path\" : {}", System.getProperty("java.library.path"));
        System.loadLibrary("lib");
    }
    else {
        log.error("Lib has not been loaded.");
        System.exit(-1);
    }
}

This chunk of code is working well if I start the application from the IntelliJ and .dll in fact is being loaded just like it was suggested here https://stackoverflow.com/a/6909689/3045845

The output for the loggers is:

11:14:39.287 [main] INFO  com.spring.application- libDllPath: D:\Workspace\whatever\spring-application\src\lib
11:14:39.289 [main] INFO  com.whatever - "java.library.path" : D:\Workspace\whatever\spring-application\src\lib

For the record, this is the project directory tree:

src
|----lib
      |----lib.dll
|----main
      |----java
             |----com.main.package
      |----resources
|----test

So I have built the jar file from this application using Gradle and tried to deploy the app on the Windows Server 2012 as this is the target OS for this app.

There, I am deploying the jar file in some default folder using java -jar spring-application-1.0-SNAPSHOT.jar (I tried deploying same jar under Windows10 where I am developing the app - works like a charm) and this is what I am getting:

C:\Users\user\Downloads>java -jar spring-application-1.0-SNAPSHOT.jar
11:27:55.841 [main] INFO  com.spring.application - libDllPath: C:\Users\user\Downloads\src\lib
11:27:55.853 [main] INFO  com.spring.application - "java.library.path" : C:\Users\user\Downloads\src\lib
Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\Users\user\Downloads\src\lib\lib.dll: Can't find dependent libraries
        at java.lang.ClassLoader$NativeLibrary.load(Native Method)
        at java.lang.ClassLoader.loadLibrary0(Unknown Source)
        at java.lang.ClassLoader.loadLibrary(Unknown Source)
        at java.lang.Runtime.load0(Unknown Source)
        at java.lang.System.load(Unknown Source)
        at com.spring.application.<clinit>(SpringApplication.java:51)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)

I am making sure that I do in fact have the .dll in the directory which I do.

I also tried running the jar with -Djava.library.path= set to the directory with the .dll but same story happens.

Could someone tell me what is going on here?

Lisek
  • 753
  • 2
  • 11
  • 31

1 Answers1

0

So apparently the reason for that it was not working was the dll itself was dependant on the both MSCVP120D.dll and MSCVR120D.dll. I used the Dependency Walker program to track down that on Windows Server 2012 both of these dlls were not available.

To overcome this the compilation using Visual Studio 2013 compiler (C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat) is supposed to be run along with /MTd flag as explained in this post: https://stackoverflow.com/a/32085978

Lisek
  • 753
  • 2
  • 11
  • 31