Is it possible to add a file (not necessarily a jar file) to java classpath at runtime. Specifically, the file already is present in the classpath, what I want is whether I can add a modified copy of this file to the classpath.
Thanks,
|
You can only add folders or jar files to a class loader. So if you have a single class file, you need to put it into the appropriate folder structure first. Here is a rather ugly hack that adds to the SystemClassLoader at runtime:
The reflection is necessary to access the protected method |
|||||||||||||||||||||
|
Try this one on for size.
This edits the system class loader to include the given library jar. It is pretty ugly, but it works. |
||||
|
The way I have done this is by using my own class loader
And create the following class:
Works without any reflection |
|||||
|
You coud try java.net.URLClassloader with the url of the folder/jar where your updated class resides and use it instead of the default classloader when creating a new thread. |
|||
|
yes, you can. it will need to be in its package structure in a separate directory from the rest of your compiled code if you want to isolate it. you will then just put its base dir in the front of the classpath on the command line. |
||||
|
Yes I believe it's possible but you might have to implement your own classloader. I have never done it but that is the path I would probably look at. |
|||
|
My solution:
|
|||||
|