aldorado
aldorado

Reputation: 4844

How to compile a java program with imported classes?

I am trying to get started with the API of the java software weka. I wrote the following code for testing:

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);
  }
} 

It works fine when I execute it in Eclipse. However I can't get it to run in the Terminal.

I tried to provide the .jar path during compilation and then execute the program from the directory of the compiled class.

javac -cp /usr/share/java/weka.jar hello_weka.java
java hello_weka

This approach does not work, I get the following error:

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

What am I doing wrong?

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.

Edit:

when I try the thing proposed in the answers I get the following Error:

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.

Upvotes: 2

Views: 6817

Answers (4)

aldorado
aldorado

Reputation: 4844

I finally found the answer: The answer of dunni is nearly correct. For me it works if I provide the classpath of both, the jar file and the classfile, but separated by a :.

java -cp /usr/share/java/weka.jar:/home/aldorado/myjavascripts/ hello_weka

Is it possible that this differs depending on the OS / JDK you are using?

Upvotes: 0

dunni
dunni

Reputation: 44535

You need to add also the current directory (where your own classes are stored) to the classpath:

java -cp .;/usr/share/java/weka.jar hello_weka

Upvotes: 1

Peter Walser
Peter Walser

Reputation: 15706

You need to provide the classpath to your JAR also when executing the program:

java -cp /usr/share/java/weka.jar hello_weka

Upvotes: 1

Ankur Shanbhag
Ankur Shanbhag

Reputation: 7804

Try this out:

java -cp /usr/share/java/weka.jar hello_weka

Upvotes: 2

Related Questions