This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
I am trying to run a class, but I get the following error:
java.lang.NoClassDefFoundError: MyClass
Caused by: java.lang.ClassNotFoundException: MyClass
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)
Exception in thread "main"
It happens, although the main method is in MyClass and I run directly this class.
Why is this class not found, although I launch the program from it?
Here is some code:
public class MyClass extends A implements B{
public MyClass() throws Exception {
//make some initializations
}
public static void main(final String[] args) throws Exception {
MyClass myClass = new MyClass();
//do stuff with myClass
}
}
PS: I am using Eclipse Indigo.
EDIT
I have run the class in the Command Line twice:
D:\Eclipse JEE\Workspace2\Example\target\classes\com\example\main>java com.example.main.MyClass
Exception in thread "main" java.lang.NoClassDefFoundError: com/example/main/MyClass
Caused by: java.lang.ClassNotFoundException: com.example.main.MyClass
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: com.example.main.MyClass. Program will exit.
D:\Eclipse JEE\Workspace2\Example\target\classes\com\example\main>java GeoDAOImpl
Exception in thread "main" java.lang.NoClassDefFoundError: MyClass(wrong name: com/example/main/MyClass)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
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: MyClass. Program will exit.
You haven't really given us enough information to say for sure, but my guess is that either:
You're not giving the right fully-qualified class name
Your class isn't on the classpath
For an example of the first case, if your code looks like this:
package foo;
public class MyClass {
public static void main(String[] args) {
}
}
Then you should be running:
java foo.MyClass
EDIT: With the extra information provided, I suspect I know what's happened. I suspect the class was created not in a package, and a Run Configuration was created which just runs MyClass. It's now been moved into com.example.main, but without the Run Configuration being updated.
Go into the Run Configuration editor (click on the dropdown next to the "run" button, and select Run Configurations...) and find "MyClass", then check which class is going to be run, and edit it to put the right package name in.
You first need to compile your source file if you haven't already done so. Assuming that your class MyClass is not in a package, and you are in the directory that contains the source file MyClass.java, compile it with:
javac MyClass.java
You should now have a file named MyClass.class.
Then, to run it, you need to make sure that the directory that contains the MyClass.class file is in the classpath. If you do not have the CLASSPATH environment variable set, Java will by default look in the current directory, so you should be able to run it with:
java MyClass
If this doesn't work, you can explicitly put the current directory . in the classpath using the -cp option:
java -cp . MyClass
Related
In first case, For explicit loading of test.ClassLoaderTest using below code,
public ClassLoaderTest{
public static void main(String[] args){
.....
Class.forName("test.ClassLoaderTest", true,
ClassLoaderTest.class.getClassLoader().getParent());
....
}
findClass() method of Launcher$ExtClassLoader instance gets invoked to load test.ClassLoaderTest with below error due to visibility principle,
java.lang.ClassNotFoundException: test.ClassLoaderTest
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 sun.misc.Launcher$ExtClassLoader.findClass(Launcher.java:229)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at test.ClassLoaderTest.main(ClassLoaderTest.java:29)
In second case, On explicit loading of test.ClassLoaderTest1, using
public ClassLoaderTest{
public static void main(String[] args){
.....
Class.forName("test.ClassLoaderTest1");
....
}
loadClass() method of Launcher$AppClassLoader instance is ultimately used to load test.ClassLoaderTest1 class,
where test.ClassLoaderTest1 is a wrong class file that lead to below error,
java.lang.ClassNotFoundException: test.ClassLoaderTest1
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at test.ClassLoaderTest1.main(ClassLoaderTest.java:16)
In both cases, class loading job is done by java.net.URLClassLoader.findClass()
Class.forName() internally invokes getClassLoader() to know the class loader that already loaded the class.
In second case, When Class gets a class loader instance(of type Launcher$AppClassLoader) by calling
ClassLoader cl = getClassLoader0(); to invoke the class loader instance again.
Is java.lang.ClassLoader mainly used for sub-classing custom class loader? that load classes not available in CLASSPATH but from network source etc...
From the Javadoc,
public **abstract** class ClassLoader
extends Object
There are different ClassLoader implementations that use different strategies for locating and reading the byte streams that compose a class.
I am trying to load class "SampleTest.class" from folder(C:\com\scheduler\jobs) in my current application, I could able to load class when it doesn't have package declaration.
The problem with when "SampleTest.class" has package declaration then I couldn't able to load and giving error No class found.
My current application when I am trying to load class :
**ReadInterfacesCount.java**
URL url = new URL("file:/C:/com/scheduler/jobs/");
ClassLoader cl = new URLClassLoader(new URL[]{url});
Class<?> aClass = cl.loadClass("SampleTest");
System.out.println("Interfaces count ==> "+aClass.getInterfaces().length);
SampleTest.class
package com.scheduler.jobs;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
public class SampleTest implements Serializable{
public static void main(String[] args) throws IOException{
System.out.println("From Sample Test");
}
}
When I run ReadInterfacesCount it is giving below error:
Exception in thread "main" java.lang.NoClassDefFoundError: SampleTest (wrong name: com/scheduler/jobs/SampleTest)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at com.test.ReadAllJavaClasses.getListeners(ReadAllJavaClasses.java:119)
at com.test.ReadAllJavaClasses.main(ReadAllJavaClasses.java:101)
When I remove package declaration in SampleTest file now I could able to load class.
Can anyone help me out?
Thanks,
Naresh Kallamadi.
As someone upvoted my comment, I'm converting that quick assumption to an answer
When Class.forName("SampleTest", ...) fails to instantiate a class named com.scheduler.jobs.SampleTest, but succeeds instantiating a class SampleTest, you might want to try Class.forName("com.scheduler.jobs.SampleTest", ...)
Edit: And as I wrote in the comment, it seems completely weird for me to digest code this way - I've never tried it. The next thing that comes to mind after your comment: When you want to load the class stored in C:/com/scheduler/jobs/SampleTest.class as com.scheduler.jobs.SampleTest, you might need to give C:/ as the path to the classloader, because that's the root of where it should search for a class of this package/name.
Coming back to the mentioned weirdness of the code: I don't trust any business level code that explicitly deals with classloading. Classloading is an infrastructure problem, and while you might solve the problem at hand, I believe that you're solving a completely wrong problem - you rather have severe problems with your underlying architecture (which can't be solved in this question)
Below is the code taken from http://doc.akka.io/docs/akka/2.2.3/AkkaScala.pdf
import akka.actor.Actor
object Greeter {
case object Greet
case object Done
}
class Greeter extends Actor {
def receive = {
case Greeter.Greet =>
println("Hello World!")
sender ! Greeter.Done
}
}
In it, it says :
How can I run this as a standalone within Eclipse ?
I've tried creating a new Run configuration, setting com.example.HelloWorld as the main class with the program argument being akka.Main but I receive "main class not found" exception.
Update :
Based on answer by TheTerribleSwiftTomato I have
input akka.Main into the Main class field, and
added com.example.HelloWorld as the sole argument in the Arguments tab.
but I receive below error :
Exception in thread "main" java.lang.ClassNotFoundException: com.example.HelloWorld
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)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at akka.actor.ReflectiveDynamicAccess$$anonfun$getClassFor$1.apply(DynamicAccess.scala:67)
at akka.actor.ReflectiveDynamicAccess$$anonfun$getClassFor$1.apply(DynamicAccess.scala:66)
at scala.util.Try$.apply(Try.scala:161)
at akka.actor.ReflectiveDynamicAccess.getClassFor(DynamicAccess.scala:66)
at akka.Main$.main(Main.scala:32)
at akka.Main.main(Main.scala)
I have added the akka Maven dependency, is there something else I'm missing ?
akka.Main is not an argument, it is the launcher class (i.e. the one that contains the main method). In this case, as described in the documentation, it will set up the ActorSystem instance and other necessary infrastructure.
So, in Eclipse, you would:
input akka.Main into the Main class field, and
add com.example.HelloWorld as the sole argument in the Arguments tab.
Re edit: I see two problems:
in the snippet you posted in the question, you don't actually appear to have the HelloWorld class from the example. Did you remember to include in your project?
even if you did remember to include it, there's a slight error in the HelloWorld class. It's missing the package declaration:
package com.example
Correct that (move it to the right package) and you should be on your way to writing Actor systems.
I compile a class successfully like this:
zookeeper#zookeeper-virtual-machine:~/zookeeper-3.4.5$ javac -cp "zookeeper-3.4.5.jar" org/zookeeper/Worker.java
But when I then try to run it Java's class loader can't find the class:
zookeeper#zookeeper-virtual-machine:~/zookeeper-3.4.5$ java -cp "zookeeper-3.4.5.jar" org.zookeeper.Worker
Exception in thread "main" java.lang.NoClassDefFoundError: org/zookeeper/Worker
Caused by: java.lang.ClassNotFoundException: org.zookeeper.Worker
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: org.zookeeper.Worker. Program will exit.
Here are the relevant parts of Worker.java
package org.zookeeper;
...
class Worker implements Watcher {
...
public static void main(String[] args) throws Exception {
Worker worker = new Worker(args[0], args[1],args[2]);
worker.processRequest();
}
Why can't the class loader load the class?
When the -cp flag is specified, the current directory is not automatically used in the classpath so needs to be added explicitly:
java -cp zookeeper-3.4.5.jar:. org.zookeeper.Worker
Why quote the jar anyway?
In any case, you still need your classes on the classpath, e.g.,
java -cp .:zookeeper-3.4.5.jar org.zookeeper.Worker
I wrote a java program to test RESTful web services by using Netbeans7.0.1 and it works fine there. Now I wrote the build.xml file to compile the code and when I try to run the generated .class file I always got this exception:
Exception in thread "main" java.lang.NoClassDefFoundError: ClientREST (wrong name: clientrest/ClientREST)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
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: ClientREST. Program will exit.
The name and path are correct, so any thoughts why I'm getting this exception?
Exception in thread "main" java.lang.NoClassDefFoundError: ClientREST
So, you ran it as java ClientREST. It's expecting a ClientREST.class without any package.
(wrong name: clientrest/ClientREST)
Hey, the class is trying to tell you that it has a package clientrest;. You need to run it from the package root on. Go one folder up so that you're in the folder which in turn contains the clientrest folder representing the package and then execute java clientrest.ClientREST.
You should not go inside the clientrest package folder and execute java ClientREST.
I encountered this error using command line java:
java -cp stuff/src/mypackage Test
where Test.java resides in the package mypackage.
Instead, you need to set the classpath -cp to the base folder, in this case, src, then prepend the package to the file name.
So it will end up looking like this:
java -cp stuff/src mypackage.Test
To further note on Garry's reply: The class path is the base directory where the class itself resides. So if the class file is here -
/home/person/javastuff/classes/package1/subpackage/javaThing.class
You would need to reference the class path as follows:
/home/person/javastuff/classes
So to run from the command line, the full command would be -
java -cp /home/person/javastuff/classes package1/subpackage/javaThing
i.e. the template for the above is
java_executable -cp classpath the_class_itself_within_the_class_path
That's how I finally got mine to work without having the class path in the environment
Probably the location you are generating your classes in doesnt exists on the class path. While running use the jvm arg -verbose while running and check the log whether the class is being loaded or not.
The output will also give you clue as to where the clasess are being loaded from, make sure that your class files are present in that location.
Try the below syntax:
Suppose java File resides here: fm/src/com/gsd/FileName.java
So you can run using the below syntax:
(Make current directory to 'fm')
java src.com.gsd.FileName
Suppose you have class A
and a class B
public class A{
public static void main(String[] args){
....
.....
//creating classB object
new classB();
}
}
class B{
}
this issue can be resolved by moving class B inside of class A and using static keyword
public class A{
public static void main(String[] args){
....
.....
//creating class B
new classB();
static class B{
}
}
Here is my class structure
package org.handson.basics;
public class WithoutMain {
public static void main() {
System.out.println("With main()...");
}
}
To compile this program, I had to use absolute path. So from src/main/java I ran:
javac org/handson/basics/WithoutMain.java
Initially I tried with the below command from basics folder and it didn't work
basics % java WithoutMain
Error: Could not find or load main class WithoutMain
Caused by: java.lang.NoClassDefFoundError: org/handson/basics/WithoutMain (wrong name: WithoutMain)
Later I went back to src\main\java folder and ran the class with relevant package structure, which worked as expected.
java % java org.handson.basics.WithoutMain
With main()...
I also have encountered this error on Windows when using Class.forName() where the class name I use is correct except for case.
My guess is that Java is able to find the file at the path (because Windows paths are case-insensitive) but the parsed class's name does not match the name given to Class.forName().
Fixing the case in the class name argument fixed the error.