2015-11-10 48 views
0

我有一个分发给少数用户的Java应用程序。应用程序使用Windows验证连接到SQLServer数据库。 从几个SO帖子(SO6938717,SO17277001)我了解到,通常的做法是在VM参数中设置所需库的路径。在32位和64位环境中使用Windows验证的SQLServer连接

的java -Djava.library.path = /路径/要/我的/ DLL的罐子/我的/ classpath中/去/这里MainClass

我的应用程序在32位和64位环境中运行,但对于每一个环境有是一个具有相同名称的特定库:sqljdbc_auth.dll。

我可以设置两个路径作为VM参数: 的java -Djava.library.path =/AUTH/86;/AUTH/64罐子/我的/ classpath中/去/这里MainClass

但是这并未”工作。我怎样才能确保Windows验证可以在32位和64位环境下运行?

+0

我认为你应该能够检测出你的操作系统是32位还是64位,并将java.library.path设置为你存储程序中dll的两个目录之一。只要确保在第一次联系jdbc之前设置了该值。 – Marged

回答

1

看一看这里:使用

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

绝对路径

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

,并称之为:

possible-values-of-processor-architecture

考虑到这一点,你可以发布你的应用程序这种方式到你的安装可能是库路径设置的一个好主意。

编辑:寻址64位主机上的32位Java中提到的担忧

助手级:Architecture.java

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

CMD启动程序

@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 

你”你必须命名你的目录这样匹配可能的os.arch值:

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

我想如果你知道库的路径,你甚至可以追加基于os.arch系统属性在运行时的MainClass该路径。

+0

感谢您的解决方案。也许这对我有用。但我不确定processor_architecture是否相关,或者使用的是jre。我想在一台32位的64位计算机上,这个命令会失败吗? – MarkusN

+0

处理器体系结构在这种情况下是相关的。请参阅此[文档](https://msdn.microsoft.com/en-us/library/ms378428.aspx)以获取更多信息。 –

+0

看到我的编辑解决关注。 – Jan