Saturday, April 09, 2011

Jar Up and Run – How to run a class in jar file

Ok not so fast – a brief background first. Have you ever used hudson, is it cool to just run it by typing java -jar hudson.war, well you can do it too, but need to take care of some steps before that, here you go!

1. Cerate your application with all the classes and libraries included, and most importantly ONE MAIN class ( which has a MAIN method ) , lets call it MyDriver.java

2. Now create a test file, input.txt with the following info in it (only one line ), Main-Class: classname In this case it will be Main-Class: MyDriver.java (don’t forget the space after Collen “:” – it wont work otherwise ) - click here for more info and search for “jar”

3. now run this command – jar cfm jar-file manifest-addition input-file(s). In this case it will be jar cfm my.jar input.txt *.* (or *.class what every you want to add ) – click here for details on this command

Finally you have your jar ready to run or rather FLY. you can use this command to do exactly that!

java -jar my.jar    ( this should run what ever you have in your main method in your main class ) - clieck here for details

No comments: