简体   繁体   中英

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.

Try this out:

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

您还需要将当前目录(存储您自己的类的位置)添加到类路径:

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

在执行程序时,您还需要为JAR提供类路径:

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM