how to make a JAR file

It is easy when you do not refer any other jar files in your project. You may directly use eclipse's Export and ask it to generate the manifest.mf for you. You can get a JAR file directly.

 

But if you refer other jar files, you must write your own manifest.mf file. Inside this file, you must put all the locations of the referenced jar files into the Class-Path part, and be careful, this file only accepts SPACE, no TAB! Then you can still use Export in Eclipse to make the JAR file, but must choose to use the manifest file you just create. Here is what I do:

 

1, Put all the referenced jar files in one separate directory and add them in the project property dialog "Java Build Path"'s "add JARs". Then build the project and ensure it can run well in Eclipse;

2, Open the ".classpath" file at the project root directory, because it contains all the external jar files that you need to reference and that you need to specify in the manifest.mf. I make a copy of this file and rename it as manifest.mf, becuase I do not want to type manualy so many jar names and directory names in the Class-Path. Just use find/replace to remove the useless part or characters;

3, Finish writing the manifest.mf by adding the Manifest-Version and Main-Class parts, be careful about the format, and put this file at some place (I put it in the project directory, but this does not matter as long as you can find it);

4, Go to menu to Export this project. I only export the "src" folder. All other files, including those jar files are useless, since I will put them together with the generated JAR file in the same directory later. It make no sense to include them in the JAR file. Also I UN-CHECK "Export java source files and resources" option, since I do not want to include the source code in the JAR file.

5, Generate the JAR successfully (but still has some warning, but does not matter). Now I put it to any folder and add the external jar files also into this folder with the same directory names and structure as described in the manifest.mf. I also put the other possible configure files which are used in the code into this folder -- this folder looks quite like the projecct root folder, except for those source code files which have been replaced by the generated JAR.

 

tired of reading this? here is a good guide with picture (http://www.java2000.net/viewthread.jsp?tid=477).

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章