在JAR我的程序中,Java放置资源文件在哪里? [英] Where does Java put resource files when I JAR my program?

查看:575
本文介绍了在JAR我的程序中,Java放置资源文件在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一直在寻找这个过去2个小时,找不到任何东西(我找到解决方案同样的问题,但图像,而不是文本文件)。

Been looking for this for the past 2 hours and can't find anything (I've found solutions to the same problem but with images, not text files).

很多,我做了一个读取文本文件的程序。该文件是名称和ID的列表。使用Eclipse,我将文件放在我的src文件夹中,并在程序中放置路径文件。像这样:

Pretty much, I made a program that reads a text file. The file is a list of names and IDs. Using Eclipse, I put the file in my src folder and in the program put the path file to it. Like this:

in = new BufferedReader(new FileReader(curDir+"\\bin\\items.txt"));

其中curDir是用户的当前目录(用System.getProperty(user.dir)发现) 。

Where curDir is the user's current directory (found with System.getProperty("user.dir")).

现在,问题是,当我从Eclipse运行时,程序运行正常,但是当我尝试使其成为可运行的JAR,然后运行它时,程序运行,但是文本文件中的信息不会加载。看起来Eclipse不是把文本文件放在JAR上。

Now, problem is, the program runs fine when I run it from Eclipse, but when I try to make it a runnable JAR and then run it, the program runs, but the info from the text file does not load. It look like Eclipse is not putting the text file with the JAR.

编辑:解决问题?那么JAR文件需要在一个包含所有原始文件的文件夹中?我很困惑,那么JAR文件是什么?

Solved-ish the problem? So the JAR file needs to the in a folder with all the original files? I am so confused, what is a JAR file then?

推荐答案

一种更强大的获取文件的方法,无论你是从Eclipse或者一个JAR是要做

A more robust way to get a file whether you are running from Eclipse or a JAR is to do

MyClass.getResource("items.txt")

其中 MyClass 是与所需资源在同一个包(文件夹)中的一个类

where MyClass is a class in the same package (folder) as the resource you need.

如果Eclipse没有将文件放在JAR中,可以去

If Eclipse is not putting the file in your JAR you can go to

运行 - >运行配置 - > - >类路径选项卡 - >高级 - >添加文件夹

Run -> Run Configurations -> -> Classpath tab -> Advanced -> Add Folders

然后将包含文件的文件夹添加到类路径中。或者,导出Ant脚本并创建自定义构建脚本。

Then add the folder containing your file to the classpath. Alternatively, export the Ant script and create a custom build script.

这篇关于在JAR我的程序中,Java放置资源文件在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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