0

I have downloaded sql jdbc driver that I would like to use in my Eclipse Maven Java project. Where I should place sqljdbc_auth.dll file in order I could run my project in Eclipse IDE. What I should tell maven then?

UPD

I have placed sqljdbc_auth.dll to src/main/resources, but this not helped :

2019-02-01T15:06:02.138+0200 INFO user.dir=C:\projects\eclipse_workspace\RFIDGates
Feb 01, 2019 3:06:02 PM com.microsoft.sqlserver.jdbc.AuthenticationJNI <clinit>
WARNING: Failed to load the sqljdbc_auth.dll cause : no sqljdbc_auth in java.library.path
2019-02-01T15:06:16.927+0200 ERROR Get conn exception
com.microsoft.sqlserver.jdbc.SQLServerException: This driver is not configured for integrated authentication. ClientConnectionId:68da04a3-d764-4c5d-a7ad-addd324db006
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:2670) ~[mssql-jdbc-6.4.0.jre8.jar:?]
    at com.microsoft.sqlserver.jdbc.AuthenticationJNI.<init>(AuthenticationJNI.java:79) ~[mssql-jdbc-6.4.0.jre8.jar:?]
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:3381) ~[mssql-jdbc-6.4.0.jre8.jar:?]
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$100(SQLServerConnection.java:85) ~[mssql-jdbc-6.4.0.jre8.jar:?]
    at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:3373) ~[mssql-jdbc-6.4.0.jre8.jar:?]
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7344) ~[mssql-jdbc-6.4.0.jre8.jar:?]
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2713) ~[mssql-jdbc-6.4.0.jre8.jar:?]
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:2261) ~[mssql-jdbc-6.4.0.jre8.jar:?]
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1921) ~[mssql-jdbc-6.4.0.jre8.jar:?]
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1762) ~[mssql-jdbc-6.4.0.jre8.jar:?]
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1077) ~[mssql-jdbc-6.4.0.jre8.jar:?]
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:623) ~[mssql-jdbc-6.4.0.jre8.jar:?]
    at java.sql.DriverManager.getConnection(DriverManager.java:664) ~[?:1.8.0_171]
    at java.sql.DriverManager.getConnection(DriverManager.java:247) ~[?:1.8.0_171]
    at com.kpv.rfid.db.ConnectMSSQLServer.main(ConnectMSSQLServer.java:270) [classes/:?]
Caused by: java.lang.UnsatisfiedLinkError: no sqljdbc_auth in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867) ~[?:1.8.0_171]
    at java.lang.Runtime.loadLibrary0(Runtime.java:870) ~[?:1.8.0_171]
    at java.lang.System.loadLibrary(System.java:1122) ~[?:1.8.0_171]
    at com.microsoft.sqlserver.jdbc.AuthenticationJNI.<clinit>(AuthenticationJNI.java:54) ~[mssql-jdbc-6.4.0.jre8.jar:?]
    ... 13 more
vico
  • 17,051
  • 45
  • 159
  • 315

2 Answers2

0

If you already have your .dll file just store it in your project's src/main/resources directory and Maven will put it in the target jar's root directory.

Ramesh Subramanian
  • 944
  • 1
  • 12
  • 28
  • This is a copy of the first answer of [this question](https://stackoverflow.com/questions/12157783/maven-put-dll-in-the-root-of-jar) – nortontgueno Feb 01 '19 at 13:13
  • Placing dll to src/main/resources not helped. More details in question body. – vico Feb 01 '19 at 13:19
  • 1
    Unfortunately, Java can only load a DLL from the file system, not from a Jar file. You need to write your own code to extract the .dll file from your .jar to a location in the file system. The thing itself is simple: get a stream to resource, dump it into a file. Somewhat non-trivial: where do you create the .dll file? How do you ensure alll the dependencies of the .dll are available? What do you do with the temporary .dll file when your app is not running? What if multiple instances of your app are running? –  Feb 01 '19 at 13:34
  • Here is some code in github that purports to help with the whole process: https://github.com/scijava/native-lib-loader . Have a look, it should illustrate the challenges. Feel free to write your own well-thought-through library to do this :). –  Feb 01 '19 at 13:51
0

You can place your dll files anywhere on the file system. You just have to make sure the JVM knows the path to them.

For JVM to know how to load your dll, you must update the java.library.path variable with the path to your dll on disk.

In Eclipse you can do that by modifying the eclipse.ini file in the root folder of your eclipse installation, or you can do this for each run configuration, in the "Arguments" tab -> "VM arguments".

This is the line that needs to be added:

-Djava.library.path=<path-to-your-dll>

The same applies if you package your application as a runnable jar. In this case, the command would be:

java -Djava.library.path=<path-to-your-dll> -jar myjar.jar

However, you need to be aware of the fact that your dll may also have dependencies and they are resolved by the operating system. Essentially, having the path to your dlls also in your system PATH variable would be a good idea.

Update

In eclipse.ini, this property needs to be added after the -vmargs line:

-vmargs
-Djava.library.path=<path-to-your-dll>
.....
Alex
  • 81
  • 1
  • 5