title: How to compile a java program with imported classes?
body: <p>I am trying to get started with the API of the java software weka. I wrote the following code for testing:</p>
  
  <pre><code>import weka.core.Instances;
  import java.io.BufferedReader;
  import java.io.FileReader;
  
  public class hello_weka {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new FileReader("/home/aljoscha/Masterarbeit/weka_examples/iris.arff"));
        Instances data = new Instances(reader);
        reader.close();
        // setting class attribute
        data.setClassIndex(data.numAttributes() -1);
        System.out.println(data);
        System.exit(0);
    }
  } 
  </code></pre>
  
  <p>It works fine when I execute it in Eclipse.
  However I can't get it to run in the Terminal. </p>
  
  <p>I tried to provide the .jar path during compilation and then execute the program from the directory of the compiled class.</p>
  
  <pre><code>javac -cp /usr/share/java/weka.jar hello_weka.java
  java hello_weka
  </code></pre>
  
  <p>This approach does not work, I get the following error: </p>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.NoClassDefFoundError: weka/core/Instances
        at hello_weka.main(hello_weka.java:8)
    Caused by: java.lang.ClassNotFoundException: weka.core.Instances
        at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
        ... 1 more</p>
  </blockquote>
  
  <p>What am I doing wrong? </p>
  
  <p>I guess I am doing just some completely stupid stuff since I just start to code in Java. If so, please excuse me and try to tell me how I can do better.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>when I try the thing proposed in the answers I get the following Error:</p>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.NoClassDefFoundError: hello_weka
    Caused by: java.lang.ClassNotFoundException: hello_weka
        at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
    Could not find the main class: hello_weka. Program will exit.</p>
  </blockquote>