这是我第一次处理 Java 注解。所以如果我做错了什么请原谅我!但是这个类使用javac MyFirstAnnotation.java编译成功 但是当我尝试使用 java TestMyAnnotation
运行此源代码时它会抛出这样的错误
包注释;
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());
}
}
}
}
}
最佳答案
我修复了三个问题。
public
类需要是TestMyAnnotation
。此行应该是
MyAnnotation
,而不是之前的内容Method myMethods[]=Class.forName("MyAnnotation").getDeclaredMethods();
顶部的第一个类不应该是
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/