[英]How to resolve exception in thread main NoClassDefFoundError in Java?
For the first time I dealing with Java Annotations. 我第一次处理Java注释。 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 但是此类使用javac MyFirstAnnotation.java成功编译,但是当我尝试使用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/ 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. 我修复的三个问题。
public
class needs to be TestMyAnnotation
. public
类必须是TestMyAnnotation
。 This line should be MyAnnotation
, not what it was before 这行应该是MyAnnotation
,而不是之前的样子
Method myMethods[]=Class.forName("MyAnnotation").getDeclaredMethods();
The first class at the top should not be public
, because you cannot have two public classes inside one file. 顶部的第一个类不应是public
,因为在一个文件中不能有两个public类。
Take the following code and put it inside TestMyAnnotation.java
. 采取以下代码,并将其放入TestMyAnnotation.java
。 Then run javac TestMyAnnotation.java
, followed by java TestMyAnnotation
. 然后运行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());
}
}
}
}
}
try run your Main Java class with adding -cp
(classpath) like below commands: 尝试通过添加-cp
(classpath)来运行您的Main Java类,如下所示:
java -cp . TestMyAnnotation
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.