Java ClassNotFoundException when running shell script from different directories

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class_path=$(./cp.sh "")
java -cp "$class_path" mytool.MyTool
</code>
<code>class_path=$(./cp.sh "") java -cp "$class_path" mytool.MyTool </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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>.
mytool/bin
mytool/lib/*
utils/bin
utils/lib/*
</code>
<code>. mytool/bin mytool/lib/* utils/bin utils/lib/* </code>
.
mytool/bin
mytool/lib/*
utils/bin
utils/lib/*

This the code in cp.sh:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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"
</code>
<code>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" </code>
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/:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>.;mytool/bin;mytool/lib/*;utils/bin;utils/lib/*
</code>
<code>.;mytool/bin;mytool/lib/*;utils/bin;utils/lib/* </code>
.;mytool/bin;mytool/lib/*;utils/bin;utils/lib/*

If I call myscript from inside /usr/local/lib/mytoolfolder/ as follows, everything works:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>bash myscript
</code>
<code>bash myscript </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>lib_path="/usr/local/lib/mytoolfolder/"
class_path=$("$lib_path""cp.sh" "$lib_path")
java -cp "$class_path" mytool.MyTool
</code>
<code>lib_path="/usr/local/lib/mytoolfolder/" class_path=$("$lib_path""cp.sh" "$lib_path") java -cp "$class_path" mytool.MyTool </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>/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/*
</code>
<code>/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/* </code>
/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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Error: Could not find or load main class mytool.MyTool
Caused by: java.lang.ClassNotFoundException: mytool.MyTool
</code>
<code>Error: Could not find or load main class mytool.MyTool Caused by: java.lang.ClassNotFoundException: mytool.MyTool </code>
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!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật