2/24/2009

Solve classpath format problems when launch java programs via shell scripts in cygwin

At work our java programs runs in unix machines, so we write shell scripts to launch them. Unfortunately our development environment is windows, so test the shell scripts is painfully because you have to install the entire application on the test server.
To avoid the installation of the application, i installed cygwin on my computer to easily test shell scripts, but there is a problem with the classpath format (explained here and here). As you can see in referenced posts cygwin provide an utility to solve this kind of problems (cygpath), so i came with a solution to execute my shell scripts without modify them.

First you have to write a shell script named java and placed it at the ~/bin directory:


#!/usr/bin/ksh

arguments=""
cpmod=0

for arg in $*
do

if [ $cpmod -eq 1 ] ; then
cp=`cygpath -wp ${arg}`
arguments="${arguments} ${cp}"
cpmod=0
else

if [[ $arg == "-cp" ]] || [[ $arg == "-classpath" ]] ; then
cpmod=1
fi
arguments="${arguments} ${arg}"

fi
done

java.exe ${arguments}

Finally you must set PATH variable enviroment to search first in your user bin directory. This can be easily done editing .bash_profile.



3 comments:

Laura said...

¿En inglés? :-(
Intentaré seguirlo....

flix said...

No se si contare muchas cosas, pero la idea surge de problemas que me encuentro en informatica y solucionarlos, aunque esta abierto a cualquier cosa.

Laura said...

Muy buena idea, seguro que nos sirve a muchos :-)