0

I'm running this code but I'm getting this error message where I couldn't figure it out. It is asked to design any Java code using the factory pattern with the help of reflection. Below I added the error message that appears when I run the code and btw my file name and the class name is TestReflectionFactoryDesign.

Error message:

Exception in thread "main" java.lang.ClassNotFoundException: com.test.TestReflectionFactoryDesign.Student
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at PersonFactory.getPersonWithFullQualifiedClassName(TestReflectionFactoryDesign.java:58)
    at TestReflectionFactoryDesign.main(TestReflectionFactoryDesign.java:6)

Code:

public class TestReflectionFactoryDesign {
    public static void main(String[] args) throws Exception {
        Person student = PersonFactory.getPersonWithFullQualifiedClassName("com.test.TestReflectionFactoryDesign.Student");    
        student.say();    
        Person teacher = PersonFactory.getPersonWithClass(Teacher.class);    
        teacher.say();   
        Person student2 = PersonFactory.getPersonWithName("student");    
        student2.say();   
    }    
}

class Student implements Person {    
    @Override    
    public void say() {    
        System.out.println("I am a student");    
    }    
}

class Teacher implements Person {    
    @Override    
    public void say() {   
        System.out.println("I am a teacher");    
    }    
}

interface Person {    
    void say();    
}

class PersonFactory {    
    // reflection, by full qualified class name    
    public static Person getPersonWithFullQualifiedClassName(String personType) throws Exception {    
        Class<?> personClass = Class.forName(personType);   
        return getPersonWithClass(personClass);    
    }

    // reflection, by passing class object   
    public static Person getPersonWithClass(Class personClass) throws Exception {  
        return (Person) personClass.newInstance();    
    }

    // no reflection, the ordinary way    
    public static Person getPersonWithName(String personType) {    
        if (personType.equalsIgnoreCase("STUDENT")) {    
            return new Student();    
        } else if (personType.equalsIgnoreCase("TEACHER")) {    
            return new Teacher();   
        }   
        return null;    
    }    
}
halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

0

As you (most probably) have not declared your class under a package, using the package xxx.yyy.zzz in the first line of your class, Java considers it to be part of the unnamed (default) package, as very well already explained here

You can fix that either by setting a package to your class file (most recommended), or changing the fully qualified class name to remove the invalid package (just keep Student).

JChrist
  • 1,734
  • 17
  • 23
0

If you only what to learn the factory pattern with the reflection, one simple solution is that just copy below code into a TestReflectionFactoryDesign.java file with your favorite text editor, and go to the path you saved it, and run javac TestReflectionFactoryDesign.java and then java TestReflectionFactoryDesign to see the result.

public class TestReflectionFactoryDesign {
    public static void main(String[] args) throws Exception {
        Person student = PersonFactory.getPersonWithFullQualifiedClassName("Student");    
        student.say();    
        Person teacher = PersonFactory.getPersonWithClass(Teacher.class);    
        teacher.say();   
        Person student2 = PersonFactory.getPersonWithName("student");    
        student2.say();   
    }    
}

class Student implements Person {    
    @Override    
    public void say() {    
        System.out.println("I am a student");    
    }    
}

class Teacher implements Person {    
    @Override    
    public void say() {   
        System.out.println("I am a teacher");    
    }    
}

interface Person {    
    void say();    
}

class PersonFactory {    
    // reflection, by full qualified class name    
    public static Person getPersonWithFullQualifiedClassName(String personType) throws Exception {    
        Class<?> personClass = Class.forName(personType);   
        return getPersonWithClass(personClass);    
    }

    // reflection, by passing class object   
    public static Person getPersonWithClass(Class personClass) throws Exception {  
        return (Person) personClass.newInstance();    
    }

    // no reflection, the ordinary way    
    public static Person getPersonWithName(String personType) {    
        if (personType.equalsIgnoreCase("STUDENT")) {    
            return new Student();    
        } else if (personType.equalsIgnoreCase("TEACHER")) {    
            return new Teacher();   
        }   
        return null;    
    }    
}

If you are in an IDE, and don't want to change your code, you should put package com.test.TestReflectionFactoryDesign in the very beginning of your code, which should be the path where you created your java file.

ZhaoGang
  • 4,491
  • 1
  • 27
  • 39