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
You need to provide the classpath to your JAR also when executing the program:
java -cp /usr/share/java/weka.jar hello_weka
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
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?
Related
I am trying to run a code with multiple Java class files and a jar file which is from a library I downloaded. I compiled them with the following:
javac -cp "quickfixj-all-.jar" BTCCMarketDataRequest.java Bot.java
The Bot class has the main method and the BTCCMarketDataRequest file has a bunch of other methods in the class. I am not creating any packages.
How should I run it though?
If I do:
java Bot
I get the following output:
Exception in thread "main" java.lang.NoClassDefFoundError: quickfix/Group
at Bot.main(Bot.java:4)
Caused by: java.lang.ClassNotFoundException: quickfix.Group
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:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
... 1 more
The compiled class (the one in the jar file could not be found, but why? I compiled it.
I am new to Java so I have no idea what's going on.
Thank you!
You need to add the classpath while executing the program as well.
java -cp .:quickfixj-all-.jar Bot
This assumes that the Bot class is in the default package and all the jar and .class dependencies are in the same directory.
I am trying to run this command on linux... compilation is successfull but while running it gives error.
Executing this command:
java -cp .:/smash/same/hope/ant-launcher-1.6.1.jar src.vp
output
Setting the value for property-debug
Fusion Repository/asd/file/repo
Logs Directory
Running validations
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/tools/ant/Project
at src.vp.call(vp.java:114)
at src.vp.main(vp.java:172)
Caused by: java.lang.ClassNotFoundException: org.apache.tools.ant.Project
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:319)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:264)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:332)
... 2 more
Can some one help me to solve it...
You've included the ant-launcher jar file, but that doesn't contain the whole of ant, I'm sure. Look for the ant jar file that includes the Project class. (I'd expect it to be ant-1.6.1.jar based on the other file you've specified.)
Using Ant to compile simple Hello World program in Java, and something isn't working right. I know the Java code is correct, as it runs using javac. Something is wrong with my Any config file, and I can't quite figure out what it is. http://pastebin.com/q50L5b0D
and the command line errors I'm getting:
**Exception in thread "main" java.lang.NoClassDefFoundError: Proj0/class
Caused by: java.lang.ClassNotFoundException: Proj0.class
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: Proj0.class. Program will exit.
**
It just tell you the class Proj0 could not be found when trying to run your application using the ANT target you defined. Are you sure this line is correct ?
<property name="Proj0.class" value="package.names.Proj0"/>
The package.names.Proj0 is a strange name for a package, maybe you wanted something like using the actual value of a property called package.names?
For your javac task need to set the classpath , srcdir properly. As Vincent wrote you are missing the Proj0 class. Please look here for how to set the javac task.
my program is in package pl.edu.uj.tcs.crazySocket; due to some requirements. I work in the crazySocket directory. In order to compile the program I use command
CLASSPATH=~/prog/colosseum/data javac tictactoe.java
and that succeeds. I want to run the program. I change javac to java and get rid of the '.java'. I get
Exception in thread "main" java.lang.NoClassDefFoundError: tictactoe
Caused by: java.lang.ClassNotFoundException: tictactoe
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)
when I get up in the tree directory to the ~/prog/colosseum/data and run java pl/edu/uj/tcs/crazySocket/tictactoe all works fine.
What's wrong with the CLASSPATH? Shouldn't it work both with javac and java?
There's nothing wrong with your classpath, it's your call to java that's wrong.
You need to specify the full qualified class name pl.edu.uj.tcs.crazySocket.tictactoe.
When I run the java program it gives following error:
Exception in thread "main" java.lang.NoClassDefFoundError: check
Caused by: java.lang.ClassNotFoundException: check
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: check. Program will exit.
The source code is:
import java.io.*;
class check {
public static void main (String [] args)
{
System.out.println("Hello");
}
}
~
~
You've got the CLASSPATH environment variable set, and it doesn't include . (dot), the current directory. Try this
java -cp . check
(That's java space dash cp space dot space check).
Please try by set the class path first then compile and execute the class Then your problem will be resolved.
For example at command prompt:
C:\> setclasspath=%classpath%;.;
C:\> javac check.java
C:\> java check
Now, you will get the output as Hello.