简体   繁体   中英

How to resolve exception in thread main NoClassDefFoundError in Java?

For the first time I dealing with Java Annotations. So please pardon me if I m doing anything wrong ! But this class compiled successfully using javac MyFirstAnnotation.java but when I try to run this source code using java TestMyAnnotation

it throws an error like this

在此处输入图片说明

package Annotations;

import java.lang.annotation.*;
import java.util.*;
import java.lang.reflect.*;

@Documented
@Target(ElementType.METHOD)
@Inherited
@Retention(RetentionPolicy.RUNTIME)

public @interface MyFirstAnnotation
{   
String author() default "Chiranjib Nandy";
int revisionNumber() default 1;
String date();
}

class MySuperClass 
{   
public String showMe()
{
    return "Do Something";
}
}

class MyAnnotation extends MySuperClass
{
@Override
@MyFirstAnnotation(author="Recmach",revisionNumber=2,date="1st June,2014")
public String showMe()
{
    return "Display Something";
}

@Deprecated
@MyFirstAnnotation(revisionNumber=2,date="2nd June,2014")
public void oldMethod() 
{
    System.out.println("It is a deprecated method");
}

@SuppressWarnings({"unused","deprecation"})
@MyFirstAnnotation(author="Papai",date="1st June,2014")
public void myMethod()
{
    int j;
    oldMethod();
    System.out.println("It is defined in my way");
}
}

class TestMyAnnotation
{
public static void main(String[] args) throws ClassNotFoundException
{
    Method myMethods[]=Class.forName("Annotations.MyAnnotation").getDeclaredMethods();
    for(Method m : myMethods)
    {
        Annotation[] annotations=m.getDeclaredAnnotations();
        for(Annotation anno : annotations)
        {
            if(anno  instanceof MyFirstAnnotation)
            {
                MyFirstAnnotation myFirstAnnotation = (MyFirstAnnotation) anno;
                System.out.println("name : "+myFirstAnnotation.author());
                System.out.println("name : "+myFirstAnnotation.revisionNumber());
                System.out.println("name : "+myFirstAnnotation.date());
            }
        }
    }
}
}

Hope this link helps.

http://www.shivasoft.in/blog/java/compile-and-run-java-program-in-package-from-command-line/

This is already in stack overflow. You have to compile your class with package like in this post.

Three issues that I fixed.

  1. The public class needs to be TestMyAnnotation .
  2. This line should be MyAnnotation , not what it was before

     Method myMethods[]=Class.forName("MyAnnotation").getDeclaredMethods(); 
  3. The first class at the top should not be public , because you cannot have two public classes inside one file.

Take the following code and put it inside TestMyAnnotation.java . Then run javac TestMyAnnotation.java , followed by java TestMyAnnotation .

import java.lang.annotation.*;
import java.util.*;
import java.lang.reflect.*;

@Documented
@Target(ElementType.METHOD)
@Inherited
@Retention(RetentionPolicy.RUNTIME)

@interface MyFirstAnnotation
{   
String author() default "Chiranjib Nandy";
int revisionNumber() default 1;
String date();
}

class MySuperClass 
{   
public String showMe()
{
    return "Do Something";
}
}

class MyAnnotation extends MySuperClass
{
@Override
@MyFirstAnnotation(author="Recmach",revisionNumber=2,date="1st June,2014")
public String showMe()
{
    return "Display Something";
}

@Deprecated
@MyFirstAnnotation(revisionNumber=2,date="2nd June,2014")
public void oldMethod() 
{
    System.out.println("It is a deprecated method");
}

@SuppressWarnings({"unused","deprecation"})
@MyFirstAnnotation(author="Papai",date="1st June,2014")
public void myMethod()
{
    int j;
    oldMethod();
    System.out.println("It is defined in my way");
}
}

public class TestMyAnnotation
{
public static void main(String[] args) throws ClassNotFoundException
{
    Method myMethods[]=Class.forName("MyAnnotation").getDeclaredMethods();
    for(Method m : myMethods)
    {
        Annotation[] annotations=m.getDeclaredAnnotations();
        for(Annotation anno : annotations)
        {
            if(anno  instanceof MyFirstAnnotation)
            {
                MyFirstAnnotation myFirstAnnotation = (MyFirstAnnotation) anno;
                System.out.println("name : "+myFirstAnnotation.author());
                System.out.println("name : "+myFirstAnnotation.revisionNumber());
                System.out.println("name : "+myFirstAnnotation.date());
            }
        }
    }
}
}

try run your Main Java class with adding -cp (classpath) like below commands:

java -cp . TestMyAnnotation

Hope it helps.

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