title: How to get Apache Windows service daemons (procrun prunsrv) to use a java library path so Microsoft SQL Integrated Security Authentication can work
body: <p>I have a Java application that needs to run as a Windows service. The application needs to connect to Microsoft SQL server using Integrated Security. This means that Java needs to have the location of the driver dll in the java library path.</p>
  
  <p>According to the <a href="http://commons.apache.org/proper/commons-daemon/procrun.html" rel="nofollow">prunsrv documentation</a>, it is possible to pass in JVM options using the following parameter:</p>
  
  <pre><code>++JvmOptions="-Djava.library.path=drivers/MS SQL/x64"
  </code></pre>
  
  <p>where the path above is specific to my case.</p>
  
  <p>When running the application using java.exe from a command line, and passing in the same library path as above (with the appropriate java syntax), it works exactly as expected. However, when running the application as a service (using jvm mode in prunsrv) it gives the following exception and does not connect:</p>
  
  <pre><code>com.microsoft.sqlserver.jdbc.SQLServerException: 
  This driver is not configured for integrated authentication. com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1667)
  ...
  Caused by: java.lang.UnsatisfiedLinkError: no sqljdbc_auth in java.library.path
          at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1865)
  ...
  </code></pre>
  
  <p>The exception is the same as when the "-Djava.library.path=drivers/MS SQL/x64" options is left out from the console application. It seems like the service is not setting the library path as expected.</p>
  
  <p>Upon reviewing the documentation for <a href="http://commons.apache.org/proper/commons-daemon/procrun.html" rel="nofollow">prunsrv</a> there is an option that looks promising:</p>
  
  <pre><code>--LibraryPath : Directory added to the search path used to locate the DLLs for the JVM. This directory is added both in front of the PATH environment variable and as a parameter to the SetDLLDirectory function.
  </code></pre>
  
  <p>This is exactly what I want.</p>
  
  <p>Sadly, when setting this option along with the java.library.path jvm option, it still throws the exact same exception as before. It's as if it hasn't applied the setting.</p>