I have a clock program as AbstractClock.java in C:\Users\Neeraj\Prg folder.
CLASSPATH is specified as C:\Users\Neeraj\Prg
COMPILED javac AbstractClock.java with no errors.
RUN AS java -cp . net.sf.fmj.ejmf.toolkit.media.AbstractClock
Getting errors as
Exception in thread "main" java.lang.NoClassDefFoundError: net/sf/fmj/ejmf/toolkit/media/AbstractClock
Caused by: java.lang.ClassNotFoundException: net/sf/fmj/ejmf/toolkit/media/AbstractClock
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:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:315)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: com.hib.TranslateSQL. Program will exit.
PROGRAM:-
package net.sf.fmj.ejmf.toolkit.media;
import java.lang.*;
import javax.media.Clock;
import javax.media.Time;
import javax.media.TimeBase;
public class AbstractClock implements Clock {
private TimeBase systemtimebase = Manager.getSystemTimeBase();
public AbstractClock() { super(); }
///REST ALL METHODS
}
I am running this in commandprompt in Windows. I have seen other posts and changed classpath and run command, but still error occurs.Please provide steps suitable for running in windows in command prompt to rectify this mistake.
Is you java file in C:\Users\Neeraj\Prg or is it in C:\Users\Neeraj\Prg\net\sf\fmj\ejmf\toolkit\media\ ?
Packages in java are related to directories. So your classpath is the top level directory in which packages live. A class with the package name net.sf.fmj.ejmf.toolkit should be in that directory.
Related
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?
So I have such class:
public class Main {
public static void main(String[] args) {}
}
I work on Windows 7 and I've changed current locale to English (USA).
When I run this class on path such C:\Москва I have error message:
Exception in thread "main" java.lang.NoClassDefFoundError: Main
Caused by: java.lang.ClassNotFoundException: Main
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: Main. Program will exit.
When I run this class on path such C:\Moscow I have no errors and it works.
If my locale Russian everything is OK.
Please help me to understand. Why this happens.
C:\Москва is this folder in CLASSPATH? I suspect it is not. NoClassDefFoundError happens if a class was not available in java classpath during runtime. Add that folder to classpath. Here is good link on why this happens and what you need to do. NoclassDefFoundError
I'm a new JCuda user and I started to try some samples in my node.
I'm running a simple:
import jcuda.*;
import jcuda.runtime.*;
public class JCudaRuntimeTest{
public static void main(String args[]){
Pointer pointer = new Pointer();
JCuda.cudaMalloc(pointer, 4);
System.out.println("Pointer: "+pointer);
JCuda.cudaFree(pointer);
}
}
I put every library in the same folder and a can easily compile the code, but when I run java JCudaRuntimeTest, I got this exception.
Exception in thread "main" java.lang.NoClassDefFoundError: jcuda/Pointer
at JCudaRuntimeTest.main(JCudaRuntimeTest.java:7)
Caused by: java.lang.ClassNotFoundException: jcuda.Pointer
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:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
... 1 more
suggestions?
NoClassDefFoundError almost always means that something is missing from your classpath.
Make sure the jcuda-<version>.jar file (as well as possibly other necessary JAR files) are on the classpath when you run your program.
You can specify the classpath when you run your program with the -cp switch, for example:
java -cp C:\Project\jcuda\jcuda-0.3.2a.jar;C:\Project\mystuff\classes org.mystuff.MyProgram
or by setting the CLASSPATH environment variable (not recommended).
import java.io.*;
public class ArrayApp{
public static void main(String[] args){
System.out.println("lllll");
} // end main()
} // end class ArrayApp
i get this error when i run my application after compiling it.
Exception in thread "main" java.lang.NoClassDefFoundError: ArrayApp
Caused by: java.lang.ClassNotFoundException: ArrayApp
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:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: ArrayApp. Program will exit.
You need to make sure your class file is in the classpath. Assuming you are using the default package (i.e. no package declaration), you need to tell Java where to find your class when you run it. So let's assume your ArrayApp.class file is in the same directory. You will need to run it like this:
java -cp . ArrayApp
The option -cp and the following . tell Java that the classes will be in the current directory. The longer name for -cp is -classpath, so you can use that as well.
Also note the space between the classpath and the class name. The path is the base directory of where your class files are located. If you compiled them into a directory named "bin" then you would change the way you call Java like this:
java -cp bin/ ArrayApp
The "ArrayApp" is the fully qualified class name.
Your classpath is incorrect. Try...java -classpath . ArrayApp
I'm getting the following error when I try to run a simple Java JDBC program at the command line:
Exception in thread "main" java.lang.NoClassDefFoundError: LoadDriver/java
Caused by: java.lang.ClassNotFoundException: LoadDriver.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)
Here's the simple Java program, copied right out of the JDBC docs:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
// Notice, do not import com.mysql.jdbc.*
// or you will have problems!
public class LoadDriver {
public static void main(String[] args) {
try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
throw ex;
// handle the error
}
}
}
Problem is, I'm bloody sure my bash shell $ClASSPATH variable is pointed at the correct .jar file. To be sure, I copied the JDBC .jar to the same directory as my program and ran it as follows:
java -classpath ./mysql-connector-java-5.1.12-bin.jar LoadDriver.java
I still get the same error.
Edit:
I followed Powerlord's suggestion below, and now I am still getting virtually the same exception.
I entered:
javac -classpath ./mysql-connector-java-5.1.12-bin.jar LoadDriver.java
java LoadDriver
Whether or not I leave the classpath flag on the second command seems not to matter. I am still getting:
Exception in thread "main" java.lang.NoClassDefFoundError: LoadDriver
Caused by: java.lang.ClassNotFoundException: LoadDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)
I think your syntax is just wrong here. Have you already compiled LoadDriver.java using:
javac -classpath ./mysql-connector-java-5.1.12-bin.jar LoadDriver.java ?
If so, then you should be able to do :
java -classpath ./mysql-connector-java-5.1.12-bin.jar LoadDriver
(Note that I removed the '.java' from the end)
Short version:
javac requires you to put the .java at the end, java requires you to not put the .java at the end.
As Jim Garrison noted before he deleted his answer, this command-line to run the program is wrong.
java -classpath ./mysql-connector-java-5.1.12-bin.jar LoadDriver.java
This tells Java to load LoadDriver/java.class
What you actually want is
java -classpath ./mysql-connector-java-5.1.12-bin.jar LoadDriver
Provided of course that you compile it first with
javac -classpath ./mysql-connector-java-5.1.12-bin.jar LoadDriver.java
The problem is not the missing driver, it's the missing LoadDriver class. You need to compile the .java source file to a .class file first:
javac LoadDriver.java