Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reflection java.lang.ClassNotFoundException

Tags:

java

android

I am using the following:

protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode){
        case EDIT_ADJUSTMENT:
            if(resultCode == RESULT_OK){
                try{
                    String adjustment = data.getStringExtra("adjustment");
                    adjustment = adjustment.replace(" ", "");
                    ClassLoader myClassLoader = ClassLoader.getSystemClassLoader();
                    String classNameToBeLoaded = "com.picpic.adjustments." + adjustment;
                    Class adjust = myClassLoader.loadClass(classNameToBeLoaded);
                }catch(Exception e){
                    String msg = e.getMessage();
                }
            }
        break;
    }
}

When it gets to Class adjust = myClassLoader.loadClass(classNameToBeLoaded); I get the following Exception: java.lang.ClassNotFoundException: com.picpic.adjustments.ColorCorrect

The package of ColorCorrect is com.picpic.adjustments

So, Why is that error being thrown? Please let me know if you need more information. Thanks!

like image 502
Get Off My Lawn Avatar asked Nov 30 '25 18:11

Get Off My Lawn


1 Answers

I am guessing this is because your class in invisible to the Class Loader through which you are trying to load the class.

I would suggest that you try this.getClass().getClassLoader() instead of ClassLoader.getSystemClassLoader().

Java has a ClassLoader Hierarchy. Each Class Loader has different class path to load classes from. Since it seems the class you are trying to load is user defined one, it may be invisible to the Class Loader returned by ClassLoader.getSystemClassLoader() method. (You can try googling for Java ClassLoader Hierarchy. May be this link is useful if you did not know about this before.).

like image 108
user1700184 Avatar answered Dec 03 '25 08:12

user1700184



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!