21 October 2008

Javadoc is not enough: java2dot

I just wrote a tiny tool used to draw a graph for a java hierarchy. The input of the program is a set of jar files and the name of the classes to be displayed.

The source code is available here:

. The information about each class is obtained using the java.lang.reflect API and the classes are dynamically loaded using an URLClassLoader. The output is a DOT file which is then piped into graphiz dot

As an example, the command line below was used to create the hierarchy of the com.hp.hpl.jena.rdf.model.Model.
It was generated using the following command line:
java -jar ./java2dot.jar
Pierre Lindenbaum PhD. pindenbaum@yahoo.fr
Java2Dot : Compiled by lindenb on 2008-10-21 at 17:40:52 in /home/lindenb/src/lindenb/proj/tinytools
-h this screen
-jar <dir0:jar1:jar2:dir1:...> add a jar in the jar list. If directory, will add all the *ar files
-r add a pattern of classes to be ignored.
-i ignore interfaces
-m ignore classes iMplementing interfaces
-d ignore declared-classes (classes with $ in the name)
-o output file

class-1 class-2 ... class-n




java -jar ./java2dot.jar -jar ${JENADIR}/Jena-2.5.6/lib -d com.hp.hpl.jena.rdf.model.Model |\
dot -Tjpeg -ojenamodel.jpeg



Update: A jar is available here http://lindenb.googlecode.com/files/java2dot.jar.

Pierre

13 comments:

Anonymous said...

very interesting... how can i use it? (were is the jar?)

Anonymous said...

i have a prob javac-ing your refference code... is there available the whole jar?
regards

Pierre Lindenbaum said...

just added an executable jar: see http://code.google.com/p/lindenb/downloads/list

Anonymous said...

I get:
Exception in thread "main" java.lang.NoClassDefFoundError: //java2dot/jar
Caused by: java.lang.ClassNotFoundException: ..java2dot.jar

from this command:

java ./java2dot.jar -jar d:/java/projects/jolene/jolene-core-0.9.j
ar dot -Tjpeg -ojolene.jpeg

Pierre Lindenbaum said...

@Dan

the correct command would be :

java -jar java2dot.jar -jar d:/java/projects/jolene/jolene-core-0.9.jar NAME OF YOUR CLASS>| dot -Tjpeg -ojolene.jpeg

DOT must be installed on your machine.

Unknown said...
This comment has been removed by the author.
Pierre Lindenbaum said...

@wytten you can define multiple jar by using the option -jar at mulitple times or use the path of the directory of your jars

Hope it helps

Unknown said...

Pierre, when I try to use drive letters in the 2nd -jar parameter I get errors like this:
c doesn't exists
\my.jar
java.util.zip.ZipException: error in opening zip file

Pierre Lindenbaum said...

http://aovestdipaperino.com/blogfiles/InstallazionediUbuntu8.04suVirtualPC_14C4E/worksonmymachine.png

I'm sorry it doesn't work for you. Last chance: can you send me your full command please ?

Unknown said...

java -jar t:/java2dot.jar -jar c:/subgroups.jar stuff

I've tried the above with both cmd.exe and Cygwin bash.

Thanks

Pierre Lindenbaum said...

Sorry, no idea.

Are you sure 'c:' contains a readable non-encrypted file called 'subgroups.jar' ?

Richard said...

I get "Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file" when I run "java -jar java2dot.jar".

Pierre Lindenbaum said...

Java 1.6 is required. Running java -version should give "Java(TM) SE Runtime Environment (build 1.6.xxxx)