ClassnotFound exception using java reflection

2019-01-29 13:49发布

问题:

i am trying to gt a list of method on a particular java class object i have created and trying to unit test it.

It fails saying it cannot find the class with a "java.lang.ClassNotFoundException: class com.jr.freedom.user.User"

here is the testcode:

    @Test
    public void testRegisterNewUser() throws InvalidDataException {
        userService = new UserService();
        try {
            Class classUser = Class.forName(User.class.toString());
            Method m[] =classUser.getDeclaredMethods();
            for(int i = 0; i < m.length; i++){
                System.out.println(m[i].toString());
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            fail(e.getMessage());
        }


        assertNotNull(createTestUserJson());
}

My project package looks like this:

Inside the project folder: src test

the User class is inside src>com>jr?freedom>user (representing com.jr.freedom.user.User) and the test class is in test>test>com>jr>freedom>user (representing test.com.jr.freedom.user.UserServiceTest)

at first the package name inside /test director was same as the package label in the /src directory for User object but that diddnt work as i thought that the java relfection was trying to retrieve the User object inside the /test folder so i renamed the package so that it is unique and it still did not work. any advice? thanks

Full error details:

java.lang.ClassNotFoundException: class com.jr.freedom.user.User
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at test.com.jr.freedom.user.UserServiceTest.testRegisterNewUser(UserServiceTest.java:49)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197

)

回答1:

The result of User.class.toString() is:

class com.jr.freedom.user.User

See the class before the real classname starts? That makes the string an illegal class name.

You need to use getName().



回答2:

The toString of the Class class returns: "class com.foo.NameOfClass" So it is trying to load: class com.jr.freedom.user.User (as your error messages states)

But more importantly, you already have the class: User.class, why try to load again? Just take that line out and have:

User.class.getDeclaredMethods...