Showing posts with label JBoss Tools. Show all posts
Showing posts with label JBoss Tools. Show all posts

Saturday, October 23, 2010

How to find which .jar a class is in (easily)

Along with the many blessings a high level language brings you are a few curses. In Java's case, one of the biggest downfalls is the dependency graph your code becomes part of as you develop applications. This is especially noticeable when you're using frameworks like JEE or Spring.

A Java developer feels this pain in a couple of ways:

- Compile time, manifested with errors such as "(ClassName) cannot be resolved to a Type" or "The import some.class.SoughtFor cannot be resolved".

- Runtime, where the errors will show themselves with messages like the all-time favorite "java.lang.ClassNotFoundException".

But what if you had a report like this? Wouldn't that be nice?












So what do you do then? Many coders start by trying to guess which .jars hold the missing classes, then add them to the classpath one by one. This can be an exercise in frustration, as often times .jar names give you little clue as to which classes are inside.

A second methodology developers sometimes use is the 'throw everything but the kitchen sink at it' approach, where they build huge classpaths loaded with every .jar they can find. This inexact coping mechanism obviously leads to bloat.

But wait! There is an exact way to cure the problem, and it's easy and open sourced! Here's how to do it:

1) Download and unzip JBoss Tattletale. It can be found here.
2) Make a run script for Tattletale.
3) Run the run script.
4) Read the report Tattletale makes for you. It's that easy!

The script is easy to write. It takes the form 'java -jar tattletale.jar DIRECTORY_TO_RECURSIVELY_SEARCH DIRECTORY_FOR_REPORTS.

Here's an example:
java -jar tattletale.jar /home/rick/Tools/JBoss/jboss-5.1.0.GA /home/rick/rpts

Then you just go to the reports directory and view the index. To find your classes in .jars, have a look at the 'Class Location' report. It'll tell you which jar holds the missing classes!















Tattletale has many more capabilities, which are well documented in the packaged docs. I hope you'll find this tool useful in your day-to-day development work.

Happy Coding!

Saturday, October 9, 2010

Solve your Java runtime mysteries easily with Byteman

Update February 2012

Use the following to easily use Byteman on AS7, using the HelloWorld quickstart:

-- Script to attach, check, and submit rule -----
#!/bin/bash
export JBOSS_HOME=/home/rick/Tools/JBoss_Org/jboss-as-7.0.2.Final
echo $JBOSS_HOME
export BYTEMAN_HOME=/home/rick/Tools/MISC_TOOLS/byteman-2.0/byteman-download-2.0.0
export BYTEMAN_BIN=$BYTEMAN_HOME/bin
# Only install once.....
# $BYTEMAN_BIN/bminstall.sh -b -Dorg.jboss.byteman.transform.all $JBOSS_HOME/jboss-modules.jar

export QS_HELLOWORLD_TGT=/home/rick/Tools/JBoss_Org/jboss-as-quickstarts-7.0.2.CR1/helloworld/target/classes
# check it
$BYTEMAN_BIN/bmcheck.sh -cp $QS_HELLOWORLD_TGT RicksScript.btm

# add the rule
$BYTEMAN_BIN/bmsubmit.sh RicksScript.btm

-------The Rule. Check the arbitrary Java code used! --------------

RULE trace main entry
CLASS org.jboss.as.quickstarts.helloworld.HelloService
METHOD createHelloMessage
AT ENTRY
IF true
#DO traceln("entering createHelloMessage")
#DO traceStack("found the caller!\n", 100)
DO System.out.println("Hey, I'm random java code!");
ENDRULE

-------------

Have you heard of Byteman? It's sort of like AOP-lite. With a small script and a lightly doctored startup command, you can have x-ray vision into your Java applications. You don't even have to alter your source code, and it's easy to use.

Here's a quick example. Let's say we have a Java application where some method is called at some point, and you want to know when that method is called and what's being passed to it.

So let's say we have an application that provides this output. (I'll show you the source code in a bit.)
-----------------------------------------------------------------
Suspects about to do stuff!
Thump! A murder happens!
Suspects done moving around!
-----------------------------------------------------------------

Well, we don't know much about who committed the crime, do we? (In fairness, we don't even know who the suspects are yet.) But how about if you had the source code? Then could you tell? I doubt it. Here's the source:

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Clue {
private void makeMystery(){
List suspects = new ArrayList();
suspects.add("Colonel Mustard");
suspects.add("Mr. Green");
suspects.add("Miss Scarlet");
Random gen = new Random();
// determine the killer!
int theKiller = gen.nextInt(3);
System.out.println("Suspects about to do stuff!");
for (String suspect : suspects){
walkThroughKitchen(suspect);
// maybe commit the crime!
if (suspect.equals(suspects.get(theKiller))){
commitCrime(suspect);
}
walkThroughLibrary(suspect);
}
System.out.println("Suspects done moving around!");
}

private void walkThroughLibrary(String suspect) {
// walks through...
}

private void commitCrime(String suspect) {
System.out.println("Thump! A murder happens!");
}

private void walkThroughKitchen(String suspect) {
// walks through
}

public static void main(String[] args) {
Clue clue = new Clue();
clue.makeMystery();
}

}

I defy you to tell me who committed the crime! But with Byteman you can tell!

So let's set things up. Aside from downloading Byteman (found here), you need to do a few things.
  1. Make your application into a jar (using jar -cvf someJar.jar *.class, perhaps). This is done as a convenience to make it easier to put on a classpath.
  2. Make a Byteman script, telling it what you want to see.
  3. Make a command-line script to invoke your application.
Step 1 should be familiar to most Java coders.

Step 2. For the above class, let's make a script like this:

 #

# clue_script.txt - A simple script to intercept calls to a method
#
RULE Simple byteman example - watch a method
CLASS Clue
METHOD commitCrime(String)
AT EXIT
BIND THE_MURDERER = $1
IF TRUE
DO traceln("Caught the murderer! It was " + $1)
ENDRULE

Step 3. Add the necessary agent to your startup shell script.
java -javaagent:/home/rick/Tools/Examples/Byteman/byteman-1.3.0/lib/byteman.jar=script:clue_script.txt -classpath clue.jar Clue


Now when we run the script, we see this:
---------------------------------------------------------------
Suspects about to do stuff!
Thump! A murder happens!
Caught the murderer! It was Colonel Mustard
Suspects done moving around!
---------------------------------------------------------------

There we have it! Byteman has let us find out which arguments were being passed to that method, and we did it without altering the source for the application, without attaching a remote debugger, and without much trouble.

Byteman can do much more, though. It can provide stack traces on demand, it can inject faults for testing and other neat tricks. You can add an arbitrary Java helper .jar to help you do things-- just bind it with a "-b" option.  Best of all, it doesn't cost anything. Check it out today, here.

Happy Inspecting!

Wednesday, January 6, 2010

GlassFish vs. JBoss -- current best dev kit?


As I previously posted, lately I've been working with the Netbeans/GlassFish combination in an effort to keep my Enterprise Java skills current. I've been very pleased with the ease of application development and have to say that JEE has never been easier.

I have a concern, though. The shop I work at is a JBoss shop, which makes me wonder about the current state of the development world for JBoss developers.

I've worked with JBoss for a number of years, but never did find it especially easy to keep current on toolkit usage, especially compared to the aforementioned Sun toolkit. I really like the latest NetBeans all-in-0ne toolkit that runs the databases, App server, dev tools, etc. all within the IDE. To counter that, the last time I checked JBoss had a free offering that was comprised of a set of Eclipse plug-ins that offered similar functionality. It's been a while since I used that kit, but I don't remember it being nearly as slick as this latest NetBeans offering. Maybe it's time I have a fresh look at what the JBoss folks have in store for us.

I know I'm going to get results fastest if I seek expert counsel, so I'm going to refer to "JBoss AS 5 Development" from Packt Publishing. The book promises to cover plenty of ground, going from app server install, through the development process, security, and deployment all the way through app server clustering.

I've already had a read through the FREE sample chapter, which can be found here. The sample chapter covers use of Hibernate and Hibernate tools under JBoss, written in a very easy to understand step-by-step manner. In my next code practice session I'll get JBoss installed and try to establish my own basic Hibernate web app, based on the contents of this chapter.

I'll blog more about my luck with the JBoss kit after I've had a chance to read the book and write a few sample applications. Here's hoping we find good things there!

'Till then, Happy Coding!

Rick