StrugglingCoder
StrugglingCoder

Reputation: 5021

How to resolve exception in thread main NoClassDefFoundError in Java?

For the first time I dealing with Java Annotations. 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

it throws an error like this

enter image description here

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

Upvotes: 0

Views: 105

Answers (3)

merlin2011
merlin2011

Reputation: 75555

Three issues that I fixed.

  1. The public class needs to be TestMyAnnotation.
  2. This line should be MyAnnotation, not what it was before

    Method myMethods[]=Class.forName("MyAnnotation").getDeclaredMethods();
    
  3. The first class at the top should not be public, because you cannot have two public classes inside one file.

Take the following code and put it inside TestMyAnnotation.java. Then run javac TestMyAnnotation.java, followed by 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());
            }
        }
    }
}
}

Upvotes: 1

Sunny
Sunny

Reputation: 308

Hope this link helps.

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.

Upvotes: 1

Sal Prima
Sal Prima

Reputation: 1422

try run your Main Java class with adding -cp (classpath) like below commands:

java -cp . TestMyAnnotation

Hope it helps.

Upvotes: 0

Related Questions