Find your class in JAR files

Some time ago I’ve posted about using multiple -exec options for find command which can be used to find in what JARs particular class lies. Below you can find enriched and more intuitive/detailed solution for finding your class in a bunch of JARs: #!/bin/bash if [ $# -lt 1 ] then echo 'Usage: jarfind CLASS_NAME [DIR_WITH_JARS]'; exit 1; fi WHAT="$1" if [ $# -eq 2 ] then WHERE="$2" else WHERE="." fi FIND_RESULT=`find "$WHERE" -maxdepth 2 -name '*.jar' -type f` for LINE in $FIND_RESULT do grep -q "$WHAT" "$LINE" if [ $? -eq 0 ] then echo "$LINE" jar tvf "$LINE" | grep "$WHAT" echo fi done The first argument is the class name you want to search for and the second one is the directory in which the jar files should be located (relative to the current directory). If the second argument is not specified, it defaults to current directory. ...

February 27, 2012 · 2 min

Arquillian, ShrinkWrap and archive filename

When you create a deployment using Arquillian (great test runner for testing your Java EE code in the container of your choice: jBoss, Glassfish, OpenEJB, … either in embedded, managed or remote mode) remember that the name of the deployment archive file is the exact filename that will be executed in the container. It does make a difference if you use *.war or *.jar for your deployment, so beware the following construct if it’s not exactly what you intended to do: ...

June 13, 2011 · 1 min

ShrinkWrap -- adding classes to non-root location

If you want to add your classes (or any other resources) to your ShrinkWrap archive to a non-root location, you can use the following: @Deployment public static JavaArchive deploy() { // This is the classes and resources archive JavaArchive ar; ar = ShrinkWrap.create(JavaArchive.class, "resources.jar") .addPackage(TestClass.class.getPackage()); /* * The 'ar' archive will be added to the 'yourDir', so you'll * end with test.jar#/yourDir/TestClassPackage/TestClass.class */ JavaArchive endArch; endArch = ShrinkWrap.create(JavaArchive.class, "test.jar") .merge(ar, "yourDir"); endArch.merge(ar, "yourDir"); return endArch; }

June 13, 2011 · 1 min

More than one -exec in find

I was trying to execute several commands on a single record found by the GNU/Linux find command. The first attempt was to create something like: find . -name "*.pdf" -exec echo 'test' && echo 'test2' \; But it’s not the way the find command works. If you want to execute more than one command for each hit, you should use multiple -exec actions: find . -name "*.pdf" -exec echo 'test' \; -exec echo 'test2' \; I used this feature to list JAR files in a specified location along with classes they consist of: ...

April 24, 2011 · 1 min

Using Servlets 3.0 ServletContainerInitializer

Servlets 3.0 in Java EE 6, brings new interface called ServletContainerInitializer. The name is very self-explanatory, but the question is – how it’s different from the ServletContextListener? Well, technically, the ServletContainerInitializer is not a listener – it is an initializer and it’s executed before any servlet context will even be ready. You can use this initializer to do some programmatic servlet/filters/listeners addition (which is also possible in Servlets 3.0!). The ServletContainerInitializer.onStartup(Set<Class<?>> c, ServletContext ctx) can pass a set of classes which are a point of interest for the implementor (first argument). You can define such classes using @HandlesTypes annotation (also new for Servlets 3.0) which can be added to your ServletContainerInitializer implementation. For more information about the annotation, take a look at the linked javadocs. ...

March 20, 2011 · 2 min

JAR from command line -- the arguments order does matter

When you create a *.jar file using jar command-line tool, it is worth to remember that the order of arguments does matter, so: jar cmvf projects/myTags/etc/MANIFEST.MF myTags.war myTags means that you must firstly provide the manifest file (m), than the JAR filename (f). If you use the same order of arguments but change the switches like this: jar cvfm projects/myTags/etc/MANIFEST.MF myTags.war myTags the command will not be executed. According to the switches, your first argument should be the JAR filename (f) and then manifest file (m).

January 2, 2011 · 1 min