简体   繁体   English

如何使用Netbeans 6.5将文件添加到jar中?

[英]How do you add files to a jar using Netbeans 6.5?

I am working on a Java project using Netbeans for the first time. 我是第一次使用Netbeans进行Java项目。 I would like to use Netbeans 6.5 to create a jar including about 50 text files. 我想使用Netbeans 6.5创建一个包含约50个文本文件的jar。 Is there a simple way to do this? 有一个简单的方法吗?

You need to locate the project directory on your drive. 您需要在驱动器上找到项目目录。 And then put all your 50 text files into src folder. 然后将所有50个文本文件放入src文件夹。 Go back to Netbeans. 返回到Netbeans。 You should see your text files under your source package by now. 现在,您应该已经在源程序包下看到了文本文件。 Then Build the project. 然后构建项目。 Your newly created JAR should be in your project directory's dist folder. 新创建的JAR应该位于项目目录的dist文件夹中。

EDIT: Here is example source code which reads from text file in "default" package. 编辑:这是从“默认”包中的文本文件读取的示例源代码。 The file name is "hello.txt". 文件名为“ hello.txt”。

package testtextfile;

import java.io.InputStream;
import java.util.Scanner;
import javax.swing.JLabel;
import javax.swing.JOptionPane;


public class Main {

    public static void main(String[] args) {
        InputStream s = Main.class.getClassLoader().getResourceAsStream("hello.txt");
        Scanner sc = new Scanner(s);
        sc.useDelimiter("\\Z"); // read to the end of file. all at one.
        String contents = sc.next();
        JOptionPane.showMessageDialog(null, new JLabel(contents));
    }

}

如果您熟悉ant,则ant可以做到这一点,而Netbeans可以运行ant脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM