java 在eclipse java项目中加载dll文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14471150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading dll file inside eclipse java project
提问by user544079
I am trying to add the file sqljdbc_auth.dll
to the project library. I add the folder containing the dll as external class folder.
我正在尝试将文件添加sqljdbc_auth.dll
到项目库中。我将包含 dll 的文件夹添加为外部类文件夹。
Here I am basically trying to connect to my SQL SERVER 2008 database using SQL drivers given by Microsoft.
在这里,我基本上是在尝试使用 Microsoft 提供的 SQL 驱动程序连接到我的 SQL SERVER 2008 数据库。
My code is
我的代码是
private static void Connect(){
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:hostname:1433;databaseName=dbname;"
+ "user=username;password=password";
java.sql.Connection con = DriverManager.getConnection(connectionUrl);
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException e2)
{
e2.printStackTrace();
}
}`
I get the following error
我收到以下错误
WARNING: Failed to load the sqljdbc_auth.dll cause : no sqljdbc_auth in java.library.path
com.microsoft.sqlserver.jdbc.SQLServerException: This driver is not configured for integrated authentication. ClientConnectionId:b83147c7-b45a-4f35-b601-195a0aa9c32c
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1667)
at com.microsoft.sqlserver.jdbc.AuthenticationJNI.<init>(AuthenticationJNI.java:60)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:2229)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access-Djava.library.path="C:\Program Files\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\enu\auth\x64"
0(SQLServerConnection.java:41)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:2220)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1326)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.sagar.com.package1.T1.Connect(T1.java:21)
at com.sagar.com.package1.T1.main(T1.java:37)
Caused by: java.lang.UnsatisfiedLinkError: no sqljdbc_auth in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at com.microsoft.sqlserver.jdbc.AuthenticationJNI.<clinit>(AuthenticationJNI.java:35)
... 13 more
采纳答案by Mark Rotteveel
If you want to use a DLL from inside Eclipse, you either need to include the DLL in a location on the system PATH
, or you need to explicitly specify the java.library.path
property in the run configuration of Eclipse.
如果要从 Eclipse 内部使用 DLL,则需要在系统上的某个位置包含 DLL PATH
,或者需要java.library.path
在 Eclipse 的运行配置中明确指定该属性。
回答by Jose Tepedino
Another option is to add something like this to the VM parameters of the project's main class:
另一种选择是将这样的内容添加到项目主类的 VM 参数中:
##代码##(specifying the path to the sqljdbc_auth.dll file). There's no need to change the environment PATH or Eclipse IDE jvm parameters.
(指定 sqljdbc_auth.dll 文件的路径)。无需更改环境 PATH 或 Eclipse IDE jvm 参数。
For the project's main class, select menu option Run As >> Run Configurations...
对于项目的主类,选择菜单选项 Run As >> Run Configurations...
回答by quotidian_malfeasance
On windows platforms the Java.library.path defaults to the PATH environment variable. The simple solution is to copy the DLL into your path (e.g. windows/system32) and restart eclipse. Also the DLL type must match the Java version ie if you're using 32bit Java then you should use the 32bit DLL
在 Windows 平台上,Java.library.path 默认为 PATH 环境变量。简单的解决方案是将 DLL 复制到您的路径中(例如 windows/system32)并重新启动 eclipse。此外,DLL 类型必须与 Java 版本匹配,即如果您使用的是 32 位 Java,那么您应该使用 32 位 DLL