如何解决java调dll报Unable to load library的错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何解决java调dll报Unable to load library的错误相关的知识,希望对你有一定的参考价值。

  载入库文件有两个函数,
  System.load和System.loadLibrary。
  当使用System.load
  时,需要输入文件的全路径,例如:
  System.load(“/tmp/test.so”);当使用System.loadlibrary
  时是在系统的library的目录中需找符合条件的库文件,可以使用
  -Djava.library.path=[path]参数,来指定Java程序加载库文件的路径,或者将库文件(
  *.so,*.dll)复制到include的默认路径,
  Linux一般是/usr/lib目录下,当然可以编辑/etc/ld.so.conf.d/下的文件,设定自己的库查找路径ldconfig -v|grep xxx看看有没有动态库。
  Linux还要注意,使用
  System.loadLibrary(name)方法其中参数内容与Windows有区别,
  Linux中的的库文件名为libname.so.dll。在Windows中尽量使用Windows的C/C++编译器生成dll文件,否则会出现问题。
参考技术A 载入库文件有两个函数System.load和System.loadLibrary。

当使用System.load时,需要输入文件的全路径,例如:System.load(“/tmp/test.so”);

当使用System.loadlibrary时是在系统的library的目录中需找复合条件的库文件,可以使用
-Djava.library.path=[path]参数,来指定Java程序加载库文件的路径,或者将库文件(
*.so,*.dll)复制到include的默认路径,Linux一般是/usr/lib目录下,当然可以编辑/etc/ld.so.conf.d/下的文件,设定库查找路径ldconfig -v|grep xxx看看有没有动态库。

Linux还要注意,使用System.loadLibrary(name)方法其中参数内容与Windows有区别,
Linux中的的库文件名为libname.so,.dll。
在Windows中尽量使用Windows的C/C++编译器生成dll文件,否则会出现问题。
参考技术B 错误已很明显的告诉人了,没有加载到library,是不是加载的路径不对啊,java调用dll,是用什么语言写的,建议用JNI 或者JNA 去调用。

Java调用https服务报错unable to find valid certification path to requested target的解决方法

我们网站要进行https改造,配置上购买的SSL证书后,浏览器访问正常,但是写了个java代码用httpcomponents调用https rest接口时报错:


Exception in thread "main" javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)

at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1904)

at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:279)

at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:273)

at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1446)

at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:209)

at sun.security.ssl.Handshaker.processLoop(Handshaker.java:901)

at sun.security.ssl.Handshaker.process_record(Handshaker.java:837)

at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1023)

at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1332)

at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1359)

at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1343)

at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:394)

at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:353)

at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:141)

at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)

at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)

at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)

at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)

at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)

at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)

at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)

at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)

at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)

at com.duiba.activity.cmsweb.controller.DappConfigCtrl.main(DappConfigCtrl.java:1248)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:606)

at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:385)

at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)

at sun.security.validator.Validator.validate(Validator.java:260)

at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:326)

at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:231)

at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:126)

at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1428)

... 25 more

Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:196)

at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:268)

at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:380)

... 31 more



网上查了一堆资料,要么说要把网站证书放到某个目录下,要么要改代码,没有我想要的,因为不可能让其他开发者去做这些事情。

后来了解到证书链这回事,才知道如何解决这个问题。有关证书链可以读这里:http://blog.sina.com.cn/s/blog_53ed87c10102vn8b.html

此问题产生的原因是因为我们运维配置证书时只使用了签发的证书,java客户端无法找到可信任的上级证书,所以报错。解决方法也很简单,把中级证书、根证书附加到签发证书后面就可以了,具体方法参考这里:https://yq.aliyun.com/articles/26569

本文出自 “wolf” 博客,请务必保留此出处http://huangwq.blog.51cto.com/10299403/1859536

以上是关于如何解决java调dll报Unable to load library的错误的主要内容,如果未能解决你的问题,请参考以下文章

如何解决java调dll报Unable to load library的错误

如何解决java调dll报Unable to load library的错误

亲测解决dump导出JAVA堆内存文件报错:Unable to open socket file: target process not responding or HotSpot VM not lo

Unable to load dynamic library ext/php_ldap.dll

unable to inject dll into target怎么解决?

Java调用https服务报错unable to find valid certification path to requested target的解决方法