0

I have a Java Application which is distributed to a few users. The application connets to a SQLServer Database using windows authentification. From several SO-Posts (SO6938717, SO17277001) I learned that it is common practice to set a path to the needed library in the VM arguments.

java -Djava.library.path=/path/to/my/dll -jar /my/classpath/goes/here MainClass

My Application runs on 32- and 64bit environments, but for every environment there is a specific library with the same name: sqljdbc_auth.dll.

I can set two paths as VM arguments: java -Djava.library.path=/auth/x86;/auth/x64 -jar /my/classpath/goes/here MainClass

But this doesn't work. How can I ensure, that windows authentification works in 32- and 64bit environments?

Community
  • 1
  • 1
MarkusN
  • 3,051
  • 1
  • 18
  • 26
  • I think you should be able to detect if your OS is 32 or 64 bit and set the java.library.path to one of two directories where you store the dll from inside your program. Just make sure that you set the value before first contacting jdbc. – Marged Nov 10 '15 at 21:57

1 Answers1

1

Have a look here:

possible-values-of-processor-architecture

With this in mind you can distribute your app this way:

/libs/yourjarfiles.jar
/AMD64/sqljdbc_auth.dll (the 64bit version)
/x86/sqljdbc_auth.dll (the 32 bit version)

And call it using

java -Djava.library.path=.\%PROCESSOR_ARCHITECTURE%\ -jar /my/classpath/goes/here MainClass

Absolute Path to your install might be a good idea in library-path setting.

EDIT: Addressing the mentioned concerns for 32bit Java on 64bit host

Helper-Class: Architecture.java

public class Architecture {    
    public static void main(String[] args) {
        System.out.print(System.getProperty("os.arch"));
    }   
}

CMD to start your program

@ECHO OFF
for /f %%i in ('java -classpath /my/classpath/goes/here/ Architecture') do set JAVA_ARCH=%%i
java -Djava.library.path=path/to/dlls/%JAVA_ARCH%/ -cp /my/classpath/goes/here MainClass

You'd have to name your directories like this to match possible os.arch values:

/x86  - 32 bit DLL goes here
/amd64  - 64 bit DLL goes here

I guess if you'd know the path for the libraries you could even append that path based on os.arch System property at runtime of MainClass.

Community
  • 1
  • 1
Jan
  • 13,738
  • 3
  • 30
  • 55
  • Thanks for this solution. Maybe this works for me. But I'm not sure if the processor_architecture is relevant, or the jre that is used. I suppose on a 64bit computer with a 32bit jre, this command would fail? – MarkusN Nov 19 '15 at 17:51
  • Processor architecture is relevant in this case. Please see this [documentation](https://msdn.microsoft.com/en-us/library/ms378428.aspx) for more information. – andrea-lam-MSFT Nov 20 '15 at 02:25