Classpathchecker.rb

From EggeWiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Here's a handy script I often use in scripts which launch a Java process in order to verify if all the jars on the classpath exist and are readable.

<geshi lang="ruby">

  1. !/usr/bin/env ruby
  2. Checks to make sure all the entries on the classpath are valid
  1. We only want to emit color codes if we're outputting to a terminal. If this is getting piped into grep or less, we'll ignore these

red=STDOUT.isatty ? "\e[1;31m" :"" normal=STDOUT.isatty ? "\e[0;0m" : ""

if ARGV.size == 0 then

 cp = ENV['CLASSPATH']

else

 cp = ARGV[0]

end


if cp == nil then

 STDERR.puts "No CLASSPATH set"
 exit 1

else

 if cp =~ /jar:/ then
   splitchar = ':'
 else
   splitchar = ';'
 end
 ret = 0
 cp.split(splitchar).each do |file|
   if !File.readable?(file) then
     STDERR.puts "Warning: Classpath file #{red}#{file}#{normal} can't be read"
     ret = 1
   end
 end
 if ret != 0 then
   exit ret
 end

end </geshi>