Java ActiveMQ在创建连接时无法读取jar中的.jks文件
我正在尝试创建一个Java ActiveMQ在创建连接时无法读取jar中的.jks文件,java,activemq,Java,Activemq,我正在尝试创建一个ActiveMQSslConnection。我将信任库文件(.jks)作为分布式jar的一部分。但是,ActiveMQ无法读取文件并抛出FileNotFoundException。我验证了jar文件,并看到了相应的文件,如下面的结构所示 sample.jar: META-INF com.sample.jms——包 sample.jks--尝试加载的文件 示例1.properties 我在调试模式下运行该程序,发现ActiveMQ无法在jar中找到该文件。我将文件路径更改为o
ActiveMQSslConnection
。我将信任库文件(.jks)作为分布式jar的一部分。但是,ActiveMQ无法读取文件并抛出FileNotFoundException
。我验证了jar文件,并看到了相应的文件,如下面的结构所示
sample.jar
:
- META-INF
- com.sample.jms——包
- sample.jks--尝试加载的文件
- 示例1.properties
org.apache.activemq.ActiveMQSslConnectionFactory
中的以下位置
受保护的InputStream getInputStream(字符串urlOrResource)引发IOException{
试一试{
文件ifile=新文件(URLEORRESOURCE);
//仅当且仅当文件存在时才打开该文件
如果(ifile.exists()){/**此条件失败**
返回新的FileInputStream(ifile);
}
}捕获(例外e){
}
InputStream ins=null;
试一试{
URL=新URL(URL或资源);
ins=url.openStream();//**在此处引发异常。显然在jar中找不到该文件**
如果(ins!=null){
返回ins;
}
}捕获(错误的DurLexException忽略){
}
...
}
我想知道为什么ActiveMQ不使用getClass().getResourceAsStream()
。我认为这应该是可行的
如何通过从jar中读取.jk来修复此问题?由于您使用的ActiveMQ 5.x版本显然不支持您正在寻找的行为,因此我认为此时“修复此问题”的唯一方法是修改ActiveMQ源代码并重建代理 您还可以简单地升级到更高版本的ActiveMQ,如下所示:
您正在使用哪个版本的ActiveMQ?
protected InputStream getInputStream(String urlOrResource) throws IOException {
try {
File ifile = new File(urlOrResource);
// only open the file if and only if it exists
if (ifile.exists()) {
return new FileInputStream(ifile);
}
} catch (Exception e) {
}
InputStream ins = null;
try {
URL url = new URL(urlOrResource);
ins = url.openStream();
if (ins != null) {
return ins;
}
} catch (MalformedURLException ignore) {
}
// Alternatively, treat as classpath resource
if (ins == null) {
ins = Thread.currentThread().getContextClassLoader().getResourceAsStream(urlOrResource);
}
if (ins == null) {
throw new IOException("Could not load resource: " + urlOrResource);
}
return ins;
}