NoClassDefFoundError

Discussion in 'Plugin Development' started by mine2012craft, Sep 20, 2017.

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

    mine2012craft

    Hello,

    So, I've imported a java jar called Reflections. You can find it here: https://code.google.com/archive/p/reflections/

    I've downloaded this to allow me to get all classes that extend a certain class. The problem is at runtime, I get an NCDFE error since the server cannot find this jar file.

    I get the error at this line:
    Code:
            Reflections reflections = new Reflections("testPackage.Dungeons.MobSpells");    
    I've checked my build path, classpath, refreshed, and cleaned my project. My code also has no errors, and I've checked to make sure that the import path is correct. Is there any way I could solve this problem, or is there any other method to get all classes that extend another class?

    Thanks,
    WarlordWeaponry
     
  2. Offline

    Zombie_Striker

    @mine2012craft
    What exactly are you trying to achieve? There is most likely a better way to do it that does not require reflection.
     
  3. Offline

    mine2012craft

    I'm trying to get all classes that extend an abstract class. I then get the abstract method from the class that I have chosen, and use it. I've downloaded the jar file above because it seemed like the simplest way to do so.
     
  4. Offline

    Caderape2

    @mine2012craft

    Here a method for list all the class of your jar. Just check if the super class is not null and if it's equal to what you want.

    Code:
        
        public void getClasses() throws URISyntaxException
        {
            File file = new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
           
            try
            {
                @SuppressWarnings("resource")
                JarFile jar = new JarFile(file);
               
                for (Enumeration<JarEntry> entry = jar.entries(); entry.hasMoreElements();)
                {
                    JarEntry e = entry.nextElement();
                    String name = e.getName().replace("/", ".");
                   
                    if (name.endsWith(".class"))
                    {
                        name = name.split(".class")[0];
                        Class<?> c = Class.forName(name);
                    }
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
     
  5. Offline

    mine2012craft

    Thanks for the response, but the problem is that the method needs to be static. I'll be using this in other classes, and not this one only.
     
  6. Offline

    Caderape2

  7. Offline

    timtower Administrator Administrator Moderator

    @mine2012craft You don't need static at all.
    You need instances and to pass thoese around using constructors and getters.
     
  8. Offline

    mine2012craft

    @Caderape2 I was tired last night when I imported that code, and didn't really try to make it static. I'll try again however.

    @timtower Ok, sure I'll give it a shot

    EDIT: Alright, I got it to work through getting an instance of the MobSpell class thru static methods. I'm getting the classes successfully too, but the problem is the abstract method is not working for each of the classes.

    Here is the code I'm using. It's for debugging purposes by the way.

    Code:
    for (MobSpell msp : ms.getClasses()){
                        msp.cast();
                        System.out.println(msp.toString());
                    }
     
    Last edited: Sep 22, 2017
  9. Offline

    mine2012craft

  10. Offline

    Caderape2

  11. Offline

    mine2012craft

    Code:
        public  LinkedList<MobSpell> getClasses() throws URISyntaxException
        {
            File file = new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
           
            try
            {
                @SuppressWarnings("resource")
                JarFile jar = new JarFile(file);
              
                for (Enumeration<JarEntry> entry = jar.entries(); entry.hasMoreElements();)
                {
                    JarEntry e = entry.nextElement();
                    String name = e.getName().replace("/", ".");
                  
                    if (name.endsWith(".class"))
                    {
                        name = name.split(".class")[0];
                        try{
                            Class<?> c = Class.forName(name);
                            if (MobSpell.class.isAssignableFrom(c)){
                                try {
                                    Class<? extends MobSpell> mc = (Class<? extends MobSpell>) c;
                                    mobSpellClasses.add(mc.newInstance());
                                    System.out.println(name);
                                    System.out.println("added");
                                } catch (InstantiationException ie) {
                                    // TODO Auto-generated catch block
                                    ie.printStackTrace();
                                } catch (IllegalAccessException ie) {
                                    // TODO Auto-generated catch block
                                    ie.printStackTrace();
                                }
                            }
                        } catch (ExceptionInInitializerError ie){
                            ie.printStackTrace();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            return mobSpellClasses;
        }
     
  12. Offline

    Caderape2

    @mine2012craft
    check if the super class is not null and if it's equal to your class mobSpellClasses.class
     
    Last edited: Sep 24, 2017
Thread Status:
Not open for further replies.

Share This Page