我想从我的罐子里像这样读一个资源:
File file;
file = new File(getClass().getResource("/file.txt").toURI());
BufferedReader reader = new BufferedReader(new FileReader(file));
//Read the file
当在Eclipse中运行它时,它工作得很好,但如果我将它导出到一个jar,然后运行它,会有一个IllegalArgumentException:
Exception in thread "Thread-2"
java.lang.IllegalArgumentException: URI is not hierarchical
我真的不知道为什么,但通过一些测试,我发现如果我改变了
file = new File(getClass().getResource("/file.txt").toURI());
to
file = new File(getClass().getResource("/folder/file.txt").toURI());
然后它反过来工作(它在jar中工作,但在eclipse中不工作)。
我正在使用Eclipse,文件所在的文件夹位于类文件夹中。
由于某些原因,当我将web应用程序部署到WildFly 14时,classLoader.getResource()总是返回null。从getClass(). getclassloader()或Thread.currentThread(). getcontextclassloader()获取classLoader返回null。
getclassloader () API文档说,
"返回该类的类装入器。一些实现可能使用null来表示引导类装入器。如果该类是由引导类装入器装入的,则此方法在此类实现中将返回null。”
可能是如果你正在使用WildFly和你的web应用程序试试这个
request.getServletContext(). getresource()返回资源url。这里request是ServletRequest的一个对象。
由于某些原因,当我将web应用程序部署到WildFly 14时,classLoader.getResource()总是返回null。从getClass(). getclassloader()或Thread.currentThread(). getcontextclassloader()获取classLoader返回null。
getclassloader () API文档说,
"返回该类的类装入器。一些实现可能使用null来表示引导类装入器。如果该类是由引导类装入器装入的,则此方法在此类实现中将返回null。”
可能是如果你正在使用WildFly和你的web应用程序试试这个
request.getServletContext(). getresource()返回资源url。这里request是ServletRequest的一个对象。