如何在可执行 Jar 中包含文本文件 [英] How to include text files with Executable Jar
问题描述
我有一个 Java 项目,我想包含一个带有可执行 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");
InputFlatFile currentFile = new InputFlatFile("src/theFile.txt");
我使用该行抓取文件,正如您使用 src 看到的那样.但是,这不适用于可执行 jar.
I grab the file with that line as you can see using src. However this doesn't work with the executable jar.
如何将此文件与可执行 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?
推荐答案
jar 文件中的基目录在 classpath 中.
The base directory in the jar file is in the classpath.
试试 InputFlatFile currentFile = new InputFlatFile("theFile.txt");
Try InputFlatFile currentFile = new InputFlatFile("theFile.txt");
您可能正在使用 IDE,其中有一个 src 文件夹,IDE 将其用作包的基础.当您从 IDE 创建 jar 文件时,它会删除 src 文件夹,根文件夹中包含包.
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.
即在 Eclipse src/com.blah.blah 中,一旦创建了 jar 文件,结构就变成了 com.blah.blah
i.e. in eclipse src/com.blah.blah once jar file is created the structure becomes com.blah.blah
当然,我假设 InputFlatFile 正确读取值.
Of course I assume that InputFlatFile is properly reading the value.
http://www.devdaily.com/blog/post/java/read-text-file-from-jar-file
这篇关于如何在可执行 Jar 中包含文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!