Loading External Classes

Discussion in 'Plugin Development' started by rominos2, Sep 11, 2012.

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

    rominos2

    Hey I can't figure out how to load classes from another jar on the fly.

    I've try with that :

    Code:
     
                URLClassLoader cl = new URLClassLoader(new URL[]{myFile.toURI().toURL()});
                try {
                    cl.loadClass("org.package.myclass");
                    cl.close();
                    Class.forName("org.package.myclass");
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
    
    
    But I' still getting ClassNotFoundException : org.package.myclass

    Any Idea ?
     
  2. Offline

    ase34

    I would do it like this:
    Code:
    Class<?> clazz = Class.forName("org.package.myclass", true, cl);
    I also want to load external classes, but I fail at the casting of the classes.
     
  3. Offline

    rominos2

    Yes it permits not to fire exception.
    But I'm not sure that it really solved the problems.

    In fact I used that Class loader to load SQL Drivers.
    But I still get some errors :

    java.sql.SQLException: No suitable driver found for jdbc:postgresql://127.0.0.1:3306/myDatabase
     
  4. Offline

    andf54

    cl.loadClass("org.package.myclass");
    Is myclass supposed to be lower case?
     
  5. Offline

    rominos2

    andf54
    No it's just an example.
    In fact I try this with postgresql driver
    But when I want to get the connection, it sends me that it's not a suitable driver.
    So I think that the Class.forName(org.postresql.Driver, true, cl); doesn't throw an error but doesn't really load.
     
Thread Status:
Not open for further replies.

Share This Page