Reputation: 95
I'm writing something that will be receiving new types of objects. Before it gets these objects I send over the class file of these new types. When I read in the new objects a ClassNotFoundException is thrown unless the class file is in the applications running classpath (bin folder).
I would like to save my class files somewhere else on the system (Windows). I have looked into using a custom classloader but have not been able to use one successfully. The easier solution would appear to be just adding another location/classpath to look for the class files. But adding the location to the Windows CLASSPATH variable doesn't seem to help.
Upvotes: 1
Views: 76
Reputation: 59667
You'll ultimately need to use a ClassLoader
to do something like this, regardless of how you're injecting your classes (over the network or touching the filesystem).
However, there are libraries that will handle this for you, Jodd in particular will handle this. According to the docs, if you're getting the class bytes over the network, you can add the class with a one-liner:
Class c = ClassLoaderUtil.defineClass("MyNewClass", classBytes);
See: ClassLoaderUtil.defineClass()
This will hide the ClassLoader
details from you, but using the ClassLoader
to do what you want - pulling class bytes over the network - is pretty easy. You should post specific problems that you're having here, with some of your code, and someone should be able to give you a useful answer, and then you won't need another library.
Upvotes: 3