创建包含外部文件的Runnable Jar

编程入门 行业动态 更新时间:2024-10-12 05:44:55
本文介绍了创建包含外部文件的Runnable Jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在java中构建一个可运行的jar。我需要在jar中包含一些文件,这样当我执行jar时,文件会自动从java类中读取。因此,我在项目中创建了一个文件夹,并从项目中引用了这些文件。我在一些教程后创建了jar文件,但我无法将这些外部文件包含在我的jar文件中。请告诉我有关使用外部文件创建可运行jar的信息。

I want to build a runnable jar in java. I need to include some of the files in the jar so that when I execute jar the files are automatically read from the java class. Hence I created a folder in the project and referred these files from the project. I created jar file following some tutorial but I could not able to include these external files in my jar file. Please let me about creating runnable jar with external files.

我的文件结构是

Test | | -------src | | | default package | | | | | test1.java | -------FileFOlder | | | | | abc.txt

我在 test1中访问 abc.txt .java 类。 我的代码是,

I am accessing abc.txt in test1.java class. My code is,

public class test1 { public static void main(String[] args) throws IOException { char [] read = new char[20]; String path = new File(".").getCanonicalPath(); path = path+"\\Newfolder\\abc.txt"; System.out.println(path); File nF = new File(path); FileReader fR = new FileReader(nF); fR.read(read); for(char c : read){ System.out.print(c); } fR.close(); System.out.println(" Hi..This is test program "); } }

当我使用eclipse创建可执行jar时导出选项,我无法在jar中看到FileFolder目录。请给我一些有关此事的信息。

When I create executable jar using eclipse export option, I am unable to see FileFolder directory inside the jar. Please give me some information regarding this.

推荐答案

以下是您应该做的事情:

Here's what you should do instead:

放置该文件在你的jar文件中 in 。使用 class.getResourceAsStream()来读取它而不是文件和 FileReader 。以下是如何执行此操作的说明:如何真的从Java中的classpath读取文本文件

Put that file back in your jar file. Use class.getResourceAsStream() to read it instead of File and FileReader. Here is an explanation of how to do that: How to really read text file from classpath in Java

更多推荐

创建包含外部文件的Runnable Jar

本文发布于:2023-11-25 10:57:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629478.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   Runnable   Jar

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!