如何使用可执行 Jar 包含文本文件

发布于 2024-08-29 03:49:03 字数 232 浏览 1 评论 0原文

我有一个 Java 项目,我想包含一个带有可执行 jar 的文本文件。现在文本文件位于默认包中。

InputFlatFile currentFile = new InputFlatFile("src/theFile.txt");

我使用该行获取文件,正如您使用 src 看到的那样。但是,这不适用于可执行 jar。

如何将此文件与可执行 jar 一起保存,以便使用该程序的人只需单击一个图标即可运行该程序?

I have a Java project and I want to include a text file with the executable jar. Right now the text file is in the default package.

InputFlatFile currentFile = new InputFlatFile("src/theFile.txt");

I grab the file with that line as you can see using src. However this doesn't work with the executable jar.

How to keep this file with the executable jar, so someone using the program can just click a single icon and run the program?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅唱々樱花落 2024-09-05 03:49:03

你想要它在可执行 jar 中吗?
然后要读取文件,您应该使用

getClass().getResourceAsStream()

读取文件。

将文本文件保存在您要从中访问它的包中。
类加载器会找到它。

另请记住,JAR 中的文件名区分大小写。

you want it IN the executable jar?
then to read the file you should use

getClass().getResourceAsStream()

to read the file.

Keep the text file in the package you want to access it from.
The classloader will find it.

Remember also that filenames in JARs are case sensitive.

涙—继续流 2024-09-05 03:49:03

jar 文件中的基目录位于类路径中。

尝试 InputFlatFile currentFile = new InputFlatFile("theFile.txt");

您可能正在使用 IDE,并且其中有一个 src 文件夹,IDE 将其用作包的基础。当您从 IDE 创建 jar 文件时,它会删除 src 文件夹,根文件夹中包含包。

即在 eclipse src/com.blah.blah 中,一旦创建 jar 文件,结构就会变成 com.blah.blah

当然,我假设 InputFlatFile 正在正确读取该值。

http://www.devdaily.com/blog /post/java/从 jar 文件读取文本文件

The base directory in the jar file is in the classpath.

Try InputFlatFile currentFile = new InputFlatFile("theFile.txt");

You are probably using an IDE and it has a src folder in it that the IDE uses for the base of the packages. When you create the jar file from the IDE it then removes the src folder and the root folder has the packages in it.

i.e. in eclipse src/com.blah.blah once jar file is created the structure becomes com.blah.blah

Of course I assume that InputFlatFile is properly reading the value.

http://www.devdaily.com/blog/post/java/read-text-file-from-jar-file

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文