如何解决Java中线程主NoClassDefFoundError中的异常?

时间:2014-06-02 04:24:20

标签: java annotations

我第一次处理Java Annotations。如果我做错了什么,请原谅我!但是这个类使用 javac MyFirstAnnotation.java 成功编译 但是当我尝试使用 java TestMyAnnotation

运行此源代码时

它会抛出这样的错误

enter image description here

包注释;

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());
            }
        }
    }
}
}

3 个答案:

答案 0 :(得分:1)

希望此链接有所帮助。

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

这已经在堆栈溢出。您必须使用此文章中的包编译您的课程。

答案 1 :(得分:1)

我修复了三个问题。

  1. public课程必须为TestMyAnnotation
  2. 这一行应该是MyAnnotation,而不是之前的

    Method myMethods[]=Class.forName("MyAnnotation").getDeclaredMethods();
    
  3. 顶部的第一个类不应该是public,因为一个文件中不能有两个公共类。

  4. 使用以下代码并将其放在TestMyAnnotation.java中。然后运行javac TestMyAnnotation.java,然后运行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());
                }
            }
        }
    }
    }
    

答案 2 :(得分:0)

尝试运行您的Main Java类,添加-cp(类路径),如下面的命令:

java -cp . TestMyAnnotation

希望它有所帮助。