I have a folder /usr/local/lib/mytoolfolder/
that contains (1) a shell script, myscript
; (2) another shell script, cp.sh
; and (3) two folders containing java code (mytool/
and utils/
) that myscript
needs. myscript
contains the following lines of code:
class_path=$(./cp.sh "")
java -cp "$class_path" mytool.MyTool
cp.sh
calculates the class_path
given to java: it reads a text file, cp.txt
, and prepends the argument given (in this case an empty string) to each line in cp.txt
. It then combines the resulting strings, separating them with a semicolon. This is what cp.txt
contains:
.
mytool/bin
mytool/lib/*
utils/bin
utils/lib/*
This the code in cp.sh
:
lib_path=$1
classpath=""
while IFS= read -r line; do
classpath+="$lib_path$line;"
done < "$lib_path""cp.txt"
classpath=$(echo "$classpath" | tr -d 'r')
classpath=${classpath%;}
echo "$classpath"
In this case, the class path created contains paths relative to the current folder /usr/local/lib/mytoolfolder/
:
.;mytool/bin;mytool/lib/*;utils/bin;utils/lib/*
If I call myscript
from inside /usr/local/lib/mytoolfolder/
as follows, everything works:
bash myscript
Now, I want to move the script to /usr/local/bin/
, so that it can be called anywhere from my computer, without the preceding bash
. I understand that this means that I will have to include the absolute paths in class_path
, rather than the relative paths. So I adapt myscript
as follows:
lib_path="/usr/local/lib/mytoolfolder/"
class_path=$("$lib_path""cp.sh" "$lib_path")
java -cp "$class_path" mytool.MyTool
This indeed gives the absolute paths on class_path
:
/usr/local/lib/mytoolfolder/.;/usr/local/lib/mytoolfolder/mytool/bin;/usr/local/lib/mytoolfolder/mytool/lib/*;/usr/local/lib/mytoolfolder/utils/bin;/usr/local/lib/mytoolfolder/utils/lib/*
However — if I now run myscript
(from wherever on my computer), I suddenly get this error:
Error: Could not find or load main class mytool.MyTool
Caused by: java.lang.ClassNotFoundException: mytool.MyTool
As proven by running the script from /usr/local/lib/mytoolfolder/
, where it works, there is nothing wrong with the code, and everything is there.
Where does this ClassNotFoundException
come from? Is there still something wrong with the class path? What can cause this? Googling around and using ChatGPT, I’ve been trying many things (i.a. checking java versions, changing permissions), but I just can’t get it right and I am really lost. Where is the flaw in my logic?
I am on Windows, and am using Cygwin as my CLI.
Any help greatly appreciated!