Friday, June 17, 2011

Cygwin Java and Classpath Problem

" The cygpath program is a utility that converts Windows native filenames to Cygwin POSIX-style pathnames and vice versa. "
-- Cygwin Utilities

If you are trying to run a Java Class using a Shell Script running off Cygwin, you are sure to see a ClassNotFoundException.

The problem is due to these :-

  • java.exe is a Windows executable program
  • java.exe expects its paths & classpaths to be in the Windows Format - paths separated by semi-colon ( ; )
  • Cygwin wants its paths in the LINUX Format - paths separated by a colon ( : )
Ok, we know the problem now and here's a quick fix solution if you are running Shell Scripts off Cygwin :-

CLASSPATH=.:../lib/junit.jar:../lib/weblogic.jar:../lib/ojdbc6.jar

# Convert paths to help cygwin see Classpaths
CLASSPATH=`cygpath --path --windows "$CLASSPATH"`

java -classpath "$CLASSPATH" com.test.PreLoadTester

Please be sure not to check this into a Production environment - cygpath is only available as part of the Cygwin Utilities.

No comments: