~经言/歌词~

I find that the harder I work, the more luck I seem to have.
~ Thomas Jefferson (1743-1826)

Monday, October 18, 2010

Include external libraries using build.xml in Java

Sometimes, we are required to use some extend libraries in our Java application. For my case, I am using jwnl.jar and ritaWN.jar, in order to connect my application to a dictionary called, Wordnet. Well, wordnet is not a complete dictionary, it more likely to be a semantic lexical database. It links the words according to some specific relationships such as synonym, hyponym, and etc.

A detail explanation about wordnet can be obtained from http://wordnet.princeton.edu/, and get yourself a dictionary to play around. 

Back to the topic, even though I manage to include the external libraries to my application, by using JCreator, a java IDE, but I found out I couldnt able to compile my source codes using normal command prompt commands. It always prompted me the errors saying the syntaxes from the libraries are undefined. It was very frustrated when you found out what you had been done was not executable. 

Therefore I been searching for the solutions from the internet, dated Ms Google for days. Finally I got a solution, to use build.xml.

In order to use build.xml, first thing first, a command needs to be installed to the pc, ANT, which can get it from http://ant.apache.org/bindownload.cgi, and FOC. After download the ANT zip/tar, extract it. Well, to make life easier, extract the zip/tar file to the local disk, here I extracted to my C drive. After extracting, we need to set the environment variable, just like what we did when we first installed Java to our pc.

Right clicks MyComputer,  
Selects Properties,
Clicks on  Advance tab,
Clicks the Environment Variables button,
At System Variables box, clicks on New button, 
Creates ANT_HOME,with value C:\ant (where your ant zip file extracted).
At User Variables box, clicks on New button too,
Create Path (if Path is not created), with the value C:\ant\bin.

After set the ANT_HOME, open the command prompt and type, 'ant'. If it prompts the error saying 'ant is not recognized as internal or external command, operating program or batch file', it means something wrong with the ANT_HOME setting. Check it again, see if anything wrongly typed. If it prompts the 'Buildfile: build.xml does not exist!', this means that ant had been successfully installed to the pc. 

After that, all you need is a build.xml.

Check this link, a simple and easy tutorial about how to create a build.xml file.   http://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html?PHPSESSID=7292c8206cecdae00f82ea5ac3507e21 .

After creating the jar file, like what shown in the link above, another jar file is required. We need to include all the external libraries, merge it with the jar file created. Therefore a comment like below should be included in the build.xml.


When the build.xml file was done, using comment prompt and access to the directory where build.xml file stored. Type 'ant', it will automatically compile the source codes, and create jar file. Type 'ant exe', to create another jar file, which consists of all the libraries required.

Double clicks on the newer jar file, everything should work fine.

No comments: