创建包含外部文件的Runnable Jar [英] Creating Runnable Jar with external files included

查看:101
本文介绍了创建包含外部文件的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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆