简体   繁体   English

如何通过内置的jar适当放置和使用项目资源?

[英]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的项目可能找不到架构,因为可能有两件事:

  • The jar build process has removed the resources folder so that the xml-schemata folder is in the jar root folder. jar构建过程已删除资源文件夹,因此xml-schemata文件夹位于jar根文件夹中。
  • The project doesn't even look inside the used jar, but assumes a local resources/xml-schemata folder. 该项目甚至都不会在使用过的jar中查看,而是假设有一个本地resources / xml-schemata文件夹。

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.

相关问题 如何在衍生项目中将jar资源加载到META-INF\resources中? - How do I load jar resources into META-INF\resources in a derivative project? 用gradle构建jar并放入构建到另一个jar中的资源 - Build jar and put in resources that are built into another jar with gradle 如何正确地将资源添加到我的java项目中,以便它们可用于构建的实例? - How do I properly add resources to my java project such that they are available to the built instance? 如何在类路径的.jar中找到资源? - How do I find resources in a .jar on the classpath? 如何在TeamCity构建的Java项目中引用第三方jar库? - How do I reference third-party jar libraries into a Java project being built by TeamCity? 如何将jar依赖项目中的资源包含在war文件中 - How do i include resources from a jar dependency project in a war file 将以IntelliJ Maven项目构建的JavaFX应用打包为JAR时,如何加载资源? - How to load resources when JavaFX app built as IntelliJ Maven project is packaged as JAR? 在项目中使用构建的 jar 而不是模块 - Use built jar instead of module in project 如何创建.JAR 来执行特定任务,然后在另一个项目中使用这个 JAR? - How can I create .JAR to Do a specific task , then use this JAR in another project? 我在哪里放置.tld文件,以便正确打包使用maven2构建的生成的JAR文件? - Where do I put the .tld file so that the resulting JAR file built with maven2 is properly packaged?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM