我想在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
发布评论