I have a script placed in /opt/MyDir/myscript.sh which calls java, setting its directory as the classpath:

scriptdir=`dirname $0`
java -mx3g -cp "$scriptdir/*" MyApp.jar

I have created a link to this script in /usr/local/bin/myscript but if I run it, the classpath is messed up. Could anyone suggest me a workaround? How to deal in such situations? Thanks.


When you intend to use symbolic links for a script, the right way to get the real path of that script is:

# Absolute path to this script
scriptpath=$(readlink -e "$0")
# Absolute path this script is in
scriptdir=$(dirname "$scriptpath")
java -mx3g -cp "$scriptdir/*" MyApp.jar

More about: Reliable way for a bash script to get the full path to itself?

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.