[英]getResourceAsStream not loading resource
The project that I am currently working on utilizes an old application contained within a .jar file. 我目前正在处理的项目使用.jar文件中包含的旧应用程序。 One of the responsibilities of this application is that it updates the database when changes to the configuration files are made.
此应用程序的职责之一是在更改配置文件时更新数据库。 Every time I try to run this file (which is a simple Ant Task extension), I get an exception thrown early in the process.
每次我尝试运行此文件(这是一个简单的Ant任务扩展)时,我都会在过程的早期抛出异常。 I decompiled the Java file responsible, and found that the exception being thrown happens here.
我反编译了负责的Java文件,发现抛出的异常发生在这里。 I do not know what the issue is, as "hibernate.cfg.xml" is contained within the same .jar file as the .class throwing the exception.
我不知道是什么问题,因为“hibernate.cfg.xml”包含在与抛出异常的.class相同的.jar文件中。
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream in = loader.getResourceAsStream("hibernate.cfg.xml");
if (in == null) {
throw new RuntimeException("Couldn't find built in hibernate config");
}
If anyone has any ideas, even pointing me in the right direction, I would be grateful. 如果有人有任何想法,甚至指出我正确的方向,我将不胜感激。
Of course, any solution will have to be external, as the client already has this build of the program in production use. 当然,任何解决方案都必须是外部的,因为客户已经在生产中使用了这个程序的构建。
你是从root目录加载它,你需要“/hibernate.cfg.xml”而不只是hibernate.cfg.xml?
getResourceAsStream() expects the file to be in the same package as the Class that was the origin of the ClassLoader. getResourceAsStream()期望该文件与作为ClassLoader的原点的Class位于同一个包中。 Is your file in the right package?
您的文件是否在正确的包中?
Doh. 卫生署。 Didn't read the question fully.
没有完全阅读这个问题。 Please ignore this.
请忽略这一点。
try 尝试
InputStream in = YourClass.class..getResourceAsStream("hibernate.cfg.xml");
this will work if the class is in the same jar as the cfg file. 如果类与cfg文件位于同一个jar中,这将起作用。
edit: if you can't rebuild the application, you will need to add the jar to the bootstrap classloader. 编辑:如果无法重建应用程序,则需要将jar添加到bootstrap类加载器。 this is very ugly.
这非常难看。 you can do it by running the jvm with (play with the exact arguments, you may need to add rt.jar from your jre to it as well).
你可以通过运行jvm来实现它(使用确切的参数,你可能需要从你的jre添加rt.jar)。
-Xbootclasspath your.jar
your problem is that the code is using the classloader that loaded the Thread class, which is most likely the bootstrap classloader. 你的问题是代码是使用加载Thread类的类加载器,这很可能是bootstrap类加载器。 and that you are now in a more complex environment (app server?) that loads your jar using a different classloader.
并且您现在处于一个更复杂的环境(应用服务器?),它使用不同的类加载器加载您的jar。 so the bootstrap classloader can't find your resource.
所以bootstrap类加载器找不到你的资源。
It is possible that the classloader cannot open files contained in the jar file. 类加载器可能无法打开jar文件中包含的文件。 Try one of two things 1) try extracting the jar file and running it from the file system, or 2) if the jar manifest has a ClassPath entry, try extracting the hibernate.cfg.xml into a directory in the classpath and see if it runs.
尝试以下两种方法之一:1)尝试解压缩jar文件并从文件系统运行它,或者2)如果jar清单有一个ClassPath条目,请尝试将hibernate.cfg.xml解压缩到类路径中的目录中,看看是否有运行。
Apparently the hibernate.cfg.xml
file isn't located in the source root of the JAR file, but it is instead placed inside a package. 显然,
hibernate.cfg.xml
文件不在JAR文件的源根目录中,而是放在包中。 You'll need to specify the package path in the resource name as well. 您还需要在资源名称中指定包路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.