简体   繁体   English

Java:准备资源(.png,.txt,.arff)以使用Eclipse创建可运行的Jar

[英]Java:Prepare resources(.png,.txt,.arff) to create a runnable Jar wtih Eclipse

I have a java project in Eclipse and I have a couple of .arff files+1 png file. 我在Eclipse中有一个Java项目,并且有几个.arff文件+1 png文件。

I want to compile my project and create a runnable jar. 我想编译我的项目并创建一个可运行的jar。 The thing is I can't find the best solution so that the jar can run using arff files. 问题是我找不到最佳解决方案,因此jar可以使用arff文件运行。 Right now I have a structure like that: 现在我有一个这样的结构:

Nameofproject
|
|
-------src
|        |
|        testpackage
|                      |
|                      |
|                      test1.java
|
 -------resources
|              |
|              | 
|              abc.arff
|              logo.png
|              ...

Inside my code I call the required arff files using inputReader=new BufferedReader(new FileReader( getClass().getResource("/resources/abc.arff").getFile() )); 在我的代码内部,我使用inputReader = new BufferedReader(new FileReader( getClass()。getResource(“ / resources / abc.arff”)。getFile() ))调用所需的arff文件。

and for the png file: logo= new ImageIcon(getClass().getResource("/resources/logo.png")); 对于png文件:logo = new ImageIcon(getClass()。getResource(“ / resources / logo.png”)));

Can you tell me what is the procedure to create a runnable jar which will be able to correctly read both arff and png files? 您能告诉我创建一个可运行的jar以便能够正确读取arff和png文件的程序吗?

try 尝试

getClass().getClassLoader().getResource("resources/abc.arff");

Or if resources is marked as a source folder in your IDE, then 或者,如果资源在您的IDE中被标记为源文件夹,则

getClass().getClassLoader().getResource("abc.arff");

More info: 更多信息:

Check that you've designated resource as a source folder. 检查您是否已将资源指定为源文件夹。 When you build a jar, check where the resources are packaged within it. 生成jar时,请检查其中打包了哪些资源。 If they are in the root of the jar, it should be 如果它们在罐子的根部,应该是

getClass().getClassLoader().getResource("abc.arff");

otherwise, it will be something like: 否则,它将类似于:

getClass().getClassLoader().getResource("path/to/abc.arff");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM