Thursday, November 8, 2012

Quickies for Quick Results : Jar and JUnit

1.Updating a Jar File

The Jar tool provides a u option which you can use to update the contents of an existing JAR file by modifying its manifest or by adding files.

The basic command for adding files has this format:
jar uf jar-file input-file(s)
In this command:
  • The u option indicates that you want to update an existing JAR file.
  • The f option indicates that the JAR file to update is specified on the command line.
  • jar-file is the existing JAR file that's to be updated.
  • input-file(s) is a space-deliminated list of one or more files that you want to add to the Jar file.
Also, please remember the following before executing the command :
  • Any files already in the archive having the same pathname as a file being added will be overwritten.
  • When creating a new JAR file, you can optionally use the -C option to indicate a change of directory
If using windows command line put the "class file" ( which you want to replace in the jar file ), in the same directory hierarchy as that of the file in the jar file. Otherwise the file would be either added to some other location or will not appear at all.

Also, Use Zip or 7-zip software to look into the jar file. This will help you to
  • Obtain the fully qualified name of the class file you want to replace. ( using it you will create the folder structure (eg org/apache/hadoop/mapred/abc.class) that you want to create for the file).
  • After jar command execution you can recheck the timestamp of the file in the new jar file. If it is an old one you need to replace it with a new one.
  • For more info
2. Running tests using junit (Testcase) :

If you have your test case file in a jar file use the option from following depending on your JUnit's version.
test class name is the fully qualified name of your test class file.
For JUnit 4.X it's really:
java -cp /usr/share/java/junit.jar:{any other jar files/ your jar file where your test case resides} org.junit.runner.JUnitCore [test class name]
But if you are using JUnit 3.X please note the class name is different:
java -cp /usr/share/java/junit.jar:{any other jar files/ your jar file where your test case resides} junit.textui.TestRunner [test class name]

No comments:

Post a Comment