classLoader.getResource在jar文件中不起作用 [英] classLoader.getResource doesn't work in jar file
本文介绍了classLoader.getResource在jar文件中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
URL url = classLoader.getResource(com / x / y / z.cfg);
文件文件= new File(url.getPath());
当从Eclipse运行jar文件时运行,但在jar文件中运行时不起作用。 >
java.io.FileNotFoundException:file:\C:\Users\\\
ova\Desktop\Matcher.jar!\c
om\x\y\z.cfg
这不是重复的。我已经检查了所有其他问题,没有任何有用的信息。
解决方案
当文件被捆绑在jar内部时,它变成字节流而不是正常的File对象。
尝试
InputStream stram = 。的getClass()getClassLoader()的getResourceAsStream(relativePath);
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL url = classLoader.getResource("com/x/y/z.cfg");
File file = new File(url.getPath());
This works when running jar file from Eclipse but not works when running in a jar file.
java.io.FileNotFoundException: file:\C:\Users\nova\Desktop\Matcher.jar!\c om\x\y\z.cfg
This is not a duplicate. I've checked all other questions, no useful information.
解决方案
When file is bundled inside the jar then it become byte stream instead of a normal File object.
Try
InputStream stram=getClass().getClassLoader().getResourceAsStream(relativePath);
Read similar post here and here
这篇关于classLoader.getResource在jar文件中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文