Search code examples
javaxmlosgiclassloaderrcp

OSGi Classloader cant find class ClassNotFoundException


enter image description hereim programming an rcp application for a XML document.

Im trying to load my extensions without creating an object of it.

Sadly I get an ClassNotFoundException when I start the application.

I have no Idea why it dosent find my classes, do you guys have any idea how to fix this?

Part of my XML document. Labels are my classes:

                        <Label>main</Label>
                        <Fields>
                            <Field id="main.text" type="text">
                                <Label>Flag</Label>
                            </Field>
                            <Field id="main.multilinetext" type="multilinetext">
                                <Label>Multiline Flag</Label>
                            </Field>
                            <Field id="main.negativnumber" type="number">
                                <Label>num</Label>
                            </Field>
                            <Field id="main.positivenumber" type="positivenumber">
                                <Label>positiv num</Label>
                            </Field>
                            <Field id="main.negativnumber" type="negativnumber">
                                <Label>negativ num</Label>
                            </Field>
                            <Field id="main.negativnumber" type="image">
                                <Label>img</Label>
                            </Field>
                        </Fields>
                    </Node>

Thats my Code for loading my classes :

        public void execute() {
        IExtensionRegistry registry = Platform.getExtensionRegistry();
        
        IConfigurationElement[] config = registry.getConfigurationElementsFor(FIELDWIDGET_ID);
        for (IConfigurationElement configElement : config) { 
            if (configElement.getAttribute("type") != null) {
                System.out.println(configElement.getAttribute("type"));
                String pluginId = configElement.getContributor().getName();
                Bundle bundle = Platform.getBundle(pluginId);
                try {
                    Class<?> theClass = bundle.loadClass("class");
                    Constructor<?> con = configElement.getClass().getConstructor(Field.class, Composite.class);
                    hashMap.put(configElement.getAttribute("type"), con);
                } catch (NoSuchMethodException | SecurityException | ClassNotFoundException e1) {
                    e1.printStackTrace();
                }
            }
            
        }
    }

Solution

  • You need to get the value of the class attribute from the configuration element, load that class, and then access the constructor on that class.

    So replace

    Class<?> theClass = bundle.loadClass("class");
    Constructor<?> con = configElement.getClass().getConstructor(Field.class, Composite.class);
    

    with

    String className = configElement.getAttribute("class");
    
    Class<?> theClass = bundle.loadClass(className);
    
    Constructor<?> con = theClass.getConstructor(Field.class, Composite.class);