views:

1521

answers:

2

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  A: 

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

frankodwyer
+4  A: 

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));
    }

}
m3rLinEz
Why are you reading the file, BTW?
Adeel Ansari
@AdeelAnsari Because he will probably want to read the text files from within JAR :)
m3rLinEz
quite a foresight. :)
Adeel Ansari