问题
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