Add external files to jar

2019-02-15 17:05发布

问题:

I am relatively new to java programming. I want to know how we can add external files to our .jar. for e.g. i want to add a .txt file to my .jar so that when someone runs my program, it can ask user where it wants to store that .txt and then copy it at that location. Is it possible to do so? So far all i have been able to find is Open external file with an external program.

回答1:

JAR files can contain any kind of file. For example JEE uses war files and ear files, these are really just extended jar files and contain various XML files in addition to the java.

You can read files from a JAR by using methods of any class

 MyClass.class.getResource("/path/to/myfile.txt")


回答2:

yes you can do this if you want.. By running this command

**jar uf jar-file input-file(s)**

http://docs.oracle.com/javase/tutorial/deployment/jar/update.html