如何使用可执行 Jar 包含文本文件
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你想要它在可执行 jar 中吗?
然后要读取文件,您应该使用
读取文件。
将文本文件保存在您要从中访问它的包中。
类加载器会找到它。
另请记住,JAR 中的文件名区分大小写。
you want it IN the executable jar?
then to read the file you should use
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.
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