How do I appropriately put and use project resources with a built jar?

笑着哭i 提交于 2019-12-13 01:09:39

问题


I have a project having the following folder:

resources/xml-schemata/someXMLschema.xsd

A class inside my project uses this xml schema to validate xml files before importing them.

This class has a local variable pointing out the schema path:

schemaFilePath = "resources" + File.separator + "xml-schemata" + File.separator + "someXMLschema.xsd"

Of course this works locally.

Now I want to put my tool in a jar to give to other people.

But the project using the jar can't find the schema, because of maybe two things:

  • The jar build process has removed the resources folder so that the xml-schemata folder is in the jar root folder.
  • The project doesn't even look inside the used jar, but assumes a local resources/xml-schemata folder.

Which of these could be the problem and how could I solve it?


回答1:


 File f1 = new File("..\\..\\..\\config.properties");  

this path trying to access file is in Project directory then just access file like this.

File f=new File("filename.txt");

if your file is in OtherSources/Resources

this.getClass().getClassLoader().getResource("relative path");//-> relative path from resources folder

also, most impotently, read and print the current directory:

System.getProperty("user.dir");

that way you'll know what's the relative path you need and it will be easier for you to understand if you are trying to read from a bad path or the resources you need aren't there



来源:https://stackoverflow.com/questions/18440559/how-do-i-appropriately-put-and-use-project-resources-with-a-built-jar

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