【问题标题】:java refer a file inside project from runnable jarjava从可运行jar引用项目内的文件
【发布时间】:2016-09-08 17:40:21
【问题描述】:

我正在编写一个 java swings 应用程序。在应用程序中我们需要保留一个证书文件来创建一个自定义的 sslContext

SSLContext sslcontext = SSLContexts.custom()
 .loadTrustMaterial(new File("certs\\cacerts"),
    "password".toCharArray(), new TrustSelfSignedStrategy())
.build();

我在项目中创建了文件夹“certs”,并将文件“cacerts” 存储在该文件夹中。当我从 Eclipse 运行应用程序时,这工作正常。但是当我将应用程序打包为可运行的 jar 文件时,java 没有获取文件“certs\cacerts”。它正在抛出 filenotfound 异常。

但是当我将“\\” 更改为“//” 时,它工作正常,如果我保留jar 文件所在的文件夹“certs” 并将“cacerts”文件存储在其中。代码如下所示,

SSLContext sslcontext = SSLContexts.custom()
 .loadTrustMaterial(new File("certs//cacerts"),
    "password".toCharArray(), new TrustSelfSignedStrategy())
.build();

【问题讨论】:

标签: java file jar runnable filenotfoundexception


【解决方案1】:

我假设您使用的是 Apache 的 SSLContextBuilder。您可以load it from a URL,而不是从File 加载信任材料。 Oracle 教程Access to Resources 中介绍了获取URL 的in-jar 文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-07
    • 1970-01-01
    • 1970-01-01
    • 2014-01-10
    • 2016-12-24
    • 1970-01-01
    相关资源
    最近更新 更多