CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2009
    Location
    Belgium (Country in Europe)
    Posts
    44

    Loading external class

    Hey,

    I've a question regarding the loading of classes at runtime in java with a classloader.

    Situation:
    I've a directory called "plugins". In that directory there is a file called "MyNewAlgorithm2.class". This class's binary name is "implementation.MyNewAlgorithm2".

    I want to load this class at runtime and use it.

    Note that the .class file is located directly in the "plugins" folder and not in a "/plugins/implementation" folder.

    (PS: The situation above is an example to illustrate my problem.)

    This is my code (using some hardcoded strings for testing):
    Code:
            ClassFileScanner c = new ClassFileScanner();
            c.getReferencedClasses("plugins/MyNewAlgorithm2.class");
            System.out.println(c.getName());
            try
            {
                URLClassLoader loader = URLClassLoader.newInstance(new URL[] { new File("plugins").toURI().toURL() });
                Class<?> cls = loader.loadClass(c.getName());
                System.out.println("Succes");
            }
            catch(Exception e) {System.out.println(e.toString());}
    (The ClassFileScanner class is used to extract the binary name from the file.)

    Output:
    Code:
    implementation.MyNewAlgorithm2
    java.lang.ClassNotFoundException: implementation.MyNewAlgorithm2
    So I can't load the class, probably because it's not in a folder named "implementation".

    If i use:
    Code:
    Class<?> cls = loader.loadClass("MyNewAlgorithm2");
    instead (hardcoded the name for a moment).

    I get this output:
    Code:
    implementation.MyNewAlgorithm2
    Exception in thread "main" java.lang.NoClassDefFoundError: MyNewAlgorithm2 (wrong name: implementation/MyNewAlgorithm2)
    So to rephrase my question with another example:
    Eg: The classfile MyAlgorithm.class which has binary name: "xxxx.yyyy.MyAlgorithm" is located in the directory "C:\users\ultddave\project\". How can I load it? Because it's not in a "xxxx/yyyy" directory tree.
    (Given you have the filepath to the .class file and you can find out it's binary name.)

    Or is this impossible?

    Hopefully I explained my problem clearly.

    Thanks in advance.

    Kind regards.
    Dave
    Last edited by ultddave; October 12th, 2011 at 02:25 PM.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Loading external class

    The easiest way to do this is to put the class file(s) you want to load into a jar file and then place the jar file in your plugins directory. The jar must contain an implementation directory which holds your class file.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Jun 2009
    Location
    Belgium (Country in Europe)
    Posts
    44

    Re: Loading external class

    Hey,

    Thanks for the reply.

    Indeed, that would be the easiest solution. But I already support the usage of jar files (using Java's ServiceLoader), but I also wanted to add support for loading seperate class files .

    I think I found a solution; By loading the .class file into a byte array and then using the ClassLoader:efineClass method, I can load and use the class defined in the file. Seems to work perfectly.

    Or is there something bad about this solution (bad programming maybe)?

    Kind regards.
    Dave

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Loading external class

    I don't believe it's bad programming but I would question a design that allows class files to be dropped in the same directory regardless of their package space.

    You will have the problem of two classes with the same class name but different package space not being supported as you would end up overwriting one file with the other. This will create a difficult to find problem because a class that has been available and working will suddenly 'disappear' for no apparent reason.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Jun 2009
    Location
    Belgium (Country in Europe)
    Posts
    44

    Re: Loading external class

    Hey,

    Ok, good point. Didn't think of that.

    I'll see what I can do to avoid those issues. Thanks for the help .

    Kind regards.
    Dave

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured