Getting “Error: Could not find or load main class

2019-09-02 07:54发布

问题:

I built and ran a JAR on machineA as follows:

java -cp /foo/lib/dep1.jar:/foo/lib/dep2.jar:/foo/export/myjar.jar foo.bar.baz.MyMainClass ... 

So myjar.jar is the JAR I built and the other two its dependencies. Ran without any issues.

I then wanted to do the same on machineB so I copied the 3 JAR files there, then tried:

/non/default/install/java -cp /bar/dep1.jar:/bar/dep2.jar:/bar/myjar.jar foo.bar.baz.MyMainClass ...

and got Error: Could not find or load main class. (NOTE: /non/default/install/java is a separate java install on machineB that I have to use instead of /usr/bin/java. Reason is "historical", i.e. I have no control over this)

On machineA, java -version gives

java version "1.7.0_91"
OpenJDK Runtime Environment (rhel-2.6.2.2.el6_7-x86_64 u91-b00)
OpenJDK 64-Bit Server VM (build 24.91-b01, mixed mode)

On machineB, /non/default/install/java -version gives

java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

Both machines are running RedHat 6.

Why am I getting the error and how do I fix it?

Thx!

IMPORTANT UPDATE: I ran the commands below from command line on machineA. On machineB I originally ran them using a bash script. This means that I put them into run.bash and then ran bash run.bash from the command line. That's when I got the error. When I reran the same command from command line without using any scripts, it worked. So it seems that the problem had nothing to do with me copying the JAR form one machine to another but rather with the fact that I was invoking them form a script. And so the question really is now: how do I modify run.bash and/or the way I execute it to get rid of the error? (cuz I do need to use a script eventually)

标签: java class jar