title: Getting "Error: Could not find or load main class " when invoking a JAR from a bash script
body: <p>I built and ran a JAR on <code>machineA</code> as follows:</p>
  
  <pre><code>java -cp /foo/lib/dep1.jar:/foo/lib/dep2.jar:/foo/export/myjar.jar foo.bar.baz.MyMainClass ... 
  </code></pre>
  
  <p>So <code>myjar.jar</code> is the JAR I built and the other two its dependencies. Ran without any issues.</p>
  
  <p>I then wanted to do the same on <code>machineB</code> so I copied the 3 JAR files there, then tried:</p>
  
  <pre><code>/non/default/install/java -cp /bar/dep1.jar:/bar/dep2.jar:/bar/myjar.jar foo.bar.baz.MyMainClass ...
  </code></pre>
  
  <p>and got <code>Error: Could not find or load main class</code>. (NOTE: <code>/non/default/install/java</code> is a separate java install on <code>machineB</code> that I have to use instead of <code>/usr/bin/java</code>. Reason is "historical", i.e. I have no control over this)</p>
  
  <p>On <code>machineA</code>, <code>java -version</code> gives</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>On <code>machineB</code>, <code>/non/default/install/java -version</code> gives</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Both machines are running RedHat 6.</p>
  
  <p>Why am I getting the error and how do I fix it?</p>
  
  <p>Thx!</p>
  
  <p><strong>IMPORTANT UPDATE</strong>: I ran the commands below from command line on <code>machineA</code>. On <code>machineB</code> I originally ran them using a <code>bash</code> script. This means that I put them into <code>run.bash</code> and then ran <code>bash run.bash</code> 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: <strong>how do I modify <code>run.bash</code> and/or the way I execute it to get rid of the error?</strong> (cuz I do need to use a script eventually)</p>