如何使用构建的jar适当地放置和使用项目资源? [英] How do I appropriately put and use project resources with a built jar?

查看:135
本文介绍了如何使用构建的jar适当地放置和使用项目资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下文件夹的项目:

I have a project having the following folder:

resources / xml-schemata / someXMLschema.xsd

resources/xml-schemata/someXMLschema.xsd

我的项目中的一个类使用此xml架构在导入之前验证xml文件。

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"

当然这适用于本地。

现在我想放我的jar中的工具可以提供给其他人。

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

但是使用jar的项目无法找到模式,因为可能有两件事:

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


  • jar构建过程已删除resources文件夹,因此xml-schemata文件夹位于jar根文件夹中。

  • 该项目甚至没有查看使用过的jar内部,而是假定本地资源/ xml-schemata文件夹。

其中哪一项可以是一个问题我怎么能解决它?

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

推荐答案

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

尝试访问文件的路径位于Project目录中,然后就像这样访问文件。

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

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

如果您的文件位于 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

这篇关于如何使用构建的jar适当地放置和使用项目资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆