[英]How do I appropriately put and use project resources with a built jar?
I have a project having the following folder: 我有一个具有以下文件夹的项目:
resources/xml-schemata/someXMLschema.xsd 资源/xml-schemata/someXMLschema.xsd
A class inside my project uses this xml schema to validate xml files before importing them. 我的项目中的类使用此xml架构在导入xml文件之前对其进行验证。
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: 但是使用jar的项目可能找不到架构,因为可能有两件事:
Which of these could be the problem and how could I solve it? 其中哪些可能是问题,我该如何解决?
File f1 = new File("..\\..\\..\\config.properties");
this path trying to access file is in Project directory then just access file like this. 此尝试访问文件的路径位于Project目录中,然后仅访问此文件。
File f=new File("filename.txt");
if your file is in OtherSources/Resources
如果您的文件位于
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 这样,您将知道所需的相对路径是什么,并且如果您试图从错误的路径中读取内容或所需的资源不存在,将更容易理解
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.