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

两盒软妹~` 提交于 2019-12-24 03:43:07

问题


I am working on a Java project using Netbeans for the first time. I would like to use Netbeans 6.5 to create a jar including about 50 text files. Is there a simple way to do this?


回答1:


You need to locate the project directory on your drive. And then put all your 50 text files into src folder. Go back to 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.

EDIT: Here is example source code which reads from text file in "default" package. The file name is "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));
    }

}



回答2:


If you're familiar with ant, ant can do this, and Netbeans can run an ant script.



来源:https://stackoverflow.com/questions/435971/how-do-you-add-files-to-a-jar-using-netbeans-6-5

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!