Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts

Saturday, February 04, 2012

Detect multiple occurrences of Java classes in jar files

A developer came to me for help and said that a recent upgrade to a newer Java library broke the code. The developer was getting was a class not found exception. The developer said that the old jar file was replaced with a newer jar file. I know this same library upgrade in other applications did not have any problems, so this sounded like an issue with duplicate occurrence of the same class in the classpath. Since my Ruby Programming Language book was in handy access when this developer came to me, I ended up writing a small ruby script that went through all the jar files packed in the web archive and dumped an output of the fully qualified class along with the jar file that it can be found in and a count of the number of occurrences. Here's the ruby script running on a Windows platform:

java_home="C:\\sdk\\jdk1.6.0_24\\bin"

classmaps = Hash.new(0)

Dir["*.jar"].each do |file|
  cmd = sprintf("%s\\jar  -tf %s",java_home,file)
  lines = `#{cmd}`
  lines.each do |line|
    if line =~ /.class/
      key = line.chomp
      if classmaps.key?(key) then
        old = classmaps[key]
        data = sprintf("%s,%s",old,file)
        classmaps[key] = data 
      else
        classmaps[key] = file
      end
    end
  end
end


classmaps.each do |k,v|
    tokens = v.split(",")
    printf("%s,%i,%s\n",k,tokens.size,v)
end

I then loaded the output file in csv format into Excel and sorted the occurrences in descending order and was flabbergasted to find numerous entries that look something like the following entry:

Class NameOccurrenceFound in Jar files:
kodo/jdo/KodoExtent.class6mylib.jarmylib.jarmylib.jarkodo-api.jarkodo-runtime.jarkodo.jar

Somehow, for this application team, they were able to add the same class to the same jar file multiple times. I never thought that it was possible to add the same class to the same jar file multiple times nor would I ever want to do that. When I finally confronted the application team about this, they recognize that their build process is broken and need to fix their build process. But as a quick test, the developer removed some of the duplicate classes related to the library upgrade and the problems went away. Until Java 8 is introduced with Modularity capabilities, this tool has will be a handy way for me to check duplicate classes given a list of jar files.

Monday, May 21, 2007

Checking duplicate lines in functional programming

Recently at work, I found the need to remove duplicate lines from a file and sort the result. I've must have rewritten this hundreds of times and in a variety of programming languages: java,c#,perl,ruby,vb.

I know that you can do this easily if you have unix shell and the unix tools. Then this can be done with a simple

sort dups.txt | uniq

However, I want to code and my latest fancy is with functional programming language. So my latest iteration of the code is done in Haskell. Haskell took me a while to grok. It really warped my way of thinking about how to program. Reminds me of the first time that I had to develop proofs in math in high school. I'm slowly getting the hang of programming simple stuffs in Haskell.

Code written in Haskell

import List

main = readFile "dups.txt" >>=
(\fileContents -> putStr (unlines (sort (nub (lines fileContents)))))

lines
breaks the file content into lines
nub
removes the duplicate
unlines
combines the lines into one giant string

Here's how I would have implemented it in other languages:

Code written in Perl

open(FIN,"< dups.txt");
@lines=<FIN>;
close(FIN);
chomp (@lines);
my %uniques;
foreach $line (@lines)
{
    $uniques{$line}++;
}
foreach $key (sort keys %uniques) {
    print $key,"\n";
}

Code written in Ruby

File.open("dups.txt") { |file|
    lines = file.readlines
    lines.uniq!
    lines.sort.each { |line|
        puts line
    }
}

Code written in F#


Okay, for self enlightment, I tried to implement this in F#,which is mostly OCaml with interop to the .Net Framework. Maybe it's me but OCaml's mixed imperative and functional style confuses me or maybe I just don't have a really good grasp of OCaml syntax. In any case, it took me a while to figure out how to do this in OCaml.
#light
open System.IO;;

let main() =
    let lines = File.ReadAllLines("dups.txt")
    let s = List.fold_right Set.add (Array.to_list lines) Set.empty
    Set.iter print_endline s

do main()

Modified on 11/23/2007...after working in F# for a while, I can rewrite the above F# code as

#light
open System.IO;;

File.ReadAllLines("dups.txt")
|> Set.of_array
|> Set.iter print_endline

As a developer, I love the code density of Haskell. It's also readable so you are not sacrificing code readability for code density.