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

标签 java annotations

这是我第一次处理 Java 注解。所以如果我做错了什么请原谅我!但是这个类使用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());
            }
        }
    }
}
}

最佳答案

我修复了三个问题。

  1. public 类需要是 TestMyAnnotation
  2. 此行应该是 MyAnnotation,而不是之前的内容

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

将以下代码放入 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());
            }
        }
    }
}
}

关于java - 如何解决Java中线程主NoClassDefFoundError异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23987323/

相关文章:

java - 结果集存储在 hashMap 中,行数为零

java - Firebase:修改/更新字符串编号

asp.net-mvc - DataAnnotations.DisplayAttribute.Order 属性是否不适用于 ASP.NET MVC 2?

java - Hibernate 5 ID AUTO Generation Type for Oracle 作为 Sequence 和 MySQL 作为 Identity

Java - 如何在类中使用现有的枚举

java - 在线程上设置中断标志

PHPStorm 正在为 Symfony 项目中注释中使用的导入显示警告

annotations - 关于是否使用基于 Annotation 的 spring boot graphql server 的指导

用于性能跟踪的 Java 自定义注解

Spring 批 : Assemble a job rather than configuring it (Extensible job configuration)