gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:46:44 25 4
gpt4 key购买 nike

这是我第一次处理 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com