title: getRuntime().exec(): cannot run the class from the jar-file in the new process: NoClassDefFoundError
body: <p>I've spent a lot of time trying to find out what is wrong, but still I've got no answer.
  So here's the problem:</p>
  
  <p>I have 2 classes in my project:</p>
  
  <pre><code>ProcessTest
  |
  |-src
    |
    |-testpackage
    | |
    | |-TestClass
    |
    |-AnotherClass
  </code></pre>
  
  <p>I've created a jar-file (an artifact) from this project. And I want my program to work using this jar-file only.</p>
  
  <p>Here's the code of this 2 classes:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class TestClass {
  
      public static void main(String[] args) {
  
          String myPath = System.getProperty("user.dir");
          String stringToExecute = String.format("java -classpath \"%s/test.jar\" AnotherClass 0 10", myPath);
  
          System.out.println("Trying to execute: "+stringToExecute);
  
          Process process = null;
          try {
              process = Runtime.getRuntime().exec(stringToExecute);
          } catch (IOException e) {
              e.printStackTrace();
              System.exit(1);
          }
  
          BufferedReader errReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
          StringBuilder text = new StringBuilder();
          String tmp;
          try {
              while ((tmp = errReader.readLine()) != null)
                  text.append(tmp + "\n");
          } catch (IOException e) {
              e.printStackTrace();  
          }
          System.out.println(process);
          System.out.println(text);
          System.out.println(process.exitValue());
  
      }
  }
  </code></pre>
  
  <p>2nd class:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class AnotherClass {
  
      public static void main(String[] args) {
          if (args.length &gt;= 2) {
              int a = Integer.parseInt(args[0]);
              int b = Integer.parseInt(args[1]);
              if (a &gt; b) {
                  System.out.println("The first argument should be less or equal than the second argument.");
              } else {
                  for (int i=a; i&lt;b; i++) {
                      System.out.println("Number "+i);
                  }
              }
          } else {
              System.out.println("Not enough arguments.");
          }
      }
  
  }
  </code></pre>
  
  <p>Interesting thing, when I just copy/paste stringToExecute to the terminal - everything works fine, but when I'm trying to run the code in the TestClass I got:</p>
  
  <pre class="lang-bash prettyprint-override"><code>$ java -classpath "/tmp/test.jar" testpackage.TestClass
  Trying to execute: java -classpath "/tmp/test.jar" AnotherClass 0 10
  java.lang.UNIXProcess@7d67d940
  Exception in thread "main" java.lang.NoClassDefFoundError: AnotherClass
  Caused by: java.lang.ClassNotFoundException: AnotherClass
      at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
  Could not find the main class: AnotherClass.  Program will exit.
  
  1
  $
  </code></pre>
  
  <p>test.jar contains:</p>
  
  <pre class="lang-bash prettyprint-override"><code>$ jar tvf /tmp/test.jar 
    0 Mon Aug 27 05:19:16 EEST 2012 testpackage/
    2082 Mon Aug 27 05:19:16 EEST 2012 testpackage/TestClass.class
    1087 Mon Aug 27 05:19:16 EEST 2012 AnotherClass.class
  $
  </code></pre>
  
  <p>Thanks for your help!</p>
  
  <p>P.S. I'm using IntelliJ IDEA (if it's matter). For example for creating an artifact (test.jar).</p>