Issue
I have a jar file of my application.
When i run it using java -cp "jarfile.jar" com.my.MainClass
it works just fine but if i give it to other devs, there is a clear case where i get this error:
Error: Could not find or load main class com.my.MainClass
Caused by: java.lang.ClassNotFoundException: com.my.MainClass
It is as such, only not working on Mac's. (Windows is fine)
Mac java version:
openjdk version “11.0.2” 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)
Windows java version:
openjdk version "11" 2018-09-25
OpenJDK Runtime Environment 18.9 (build 11+28)
OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)
I do not have any sun.* dependencies. Only finding dependencies under java.util, java.net, java.io, java.nioorg.junit (which is bundled in jar).
Will gladly give more info if necessary, just have no idea what is missing as such.
Solution
can you try this and ensure that you must be in parent folder of com directory from your question MainClass is in following package hierarchy com->my->MainClass.java and your class file MainClass.class also will present in same package hierarchy.
while running this MainClass you must be in parent directory of your com folder and try the following command for compile or run
in windows:- for compile (ensure that you executing this command in parent folder for com directory)
javac -cp "jarfile.jar;." com\my\MainClass.java
java -cp "jarfile.jar;." com.my.MainClass
for Mac or linux (ensure that you executing this command in parent folder for com directory)
javac -cp "jarfile.jar:." com/my/MainClass.java
java -cp "jarfile.jar:." com.my.MainClass
Answered By - S Manikandan
Answer Checked By - Marilyn (JavaFixing Volunteer)