How to read a file inside a jar's resources from ouside the jar

ⅰ亾dé卋堺 提交于 2019-12-02 07:24:16

问题


I have a file X.json inside abc.jar in its classpath. There is a method readFile in abc.jar which reads the file as

URL url = Abc.class.getClassLoader().getResource("X.json");  
File file = new File(url.toURI());*

which reads it fine if i run it from within the jar's context i.e main method in some file inside the abc.jar

But when readFile is called from from outside of abc.jar from some other code it fails with java.lang.IllegalArgumentException: URI is not hierarchical

How do I get the File object by calling the method readFile from outside the jar's context ?


回答1:


By the time of deployment, those resources will become an embedded-resource. That being the case, the resource must be accessed by URL instead of File. See the info page for the tag, for a way to form an URL.



来源:https://stackoverflow.com/questions/20167971/how-to-read-a-file-inside-a-jars-resources-from-ouside-the-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!