-
October 12th, 2011, 02:17 PM
#1
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.
-
October 13th, 2011, 04:56 AM
#2
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.
-
October 13th, 2011, 08:47 AM
#3
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
-
October 15th, 2011, 06:10 AM
#4
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.
-
October 15th, 2011, 11:14 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|