get Class By Name

Discussion in 'Plugin Development' started by JeroenV, Feb 13, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    JeroenV

    I've got a pretty straightforward question,

    How can I get a class using it's name as a string? I saw this being used by a method in JavaPluginLoader (getClassByName(String)). However I can't find a way to actually get the JavaPluginLoader.


    Hope somone can help me out.
    Greets,
    Jeroen V.
     
  2. JeroenV
    Code:java
    1.  
    2. Class<?> clazz = Class.forName("class-name");


    Please try and use google before posting for help here.
     
  3. Offline

    JeroenV

    I did, for an hour actually. I guess I just got stuck too much with trying to use the JavaPluginLoader and focusses everything on that. I didn't know I could simply get it like that. Please excuse me for my ignorance.

    So something like this would work, right?
    Code:
       
     
        Class var = "FireScheduler".getClass();  OR  Class<?> var = Class.forName("class-name");
        Method meth = foo.getMethod("doSchedulerCode");
        meth.invoke(meth, null);
     
  4. JeroenV
    Don't use the first method, my guess would be that it would return the String class, which you don't want. Try the one I suggested, remember to specify the package. Eg.

    Code:java
    1.  
    2. Class clazz = Class.forName("com.adamki11s.myclass");
    3.  
     
  5. Offline

    JeroenV

    Alright, thank you very much for helping me out with this :)
     
Thread Status:
Not open for further replies.

Share This Page