简体   繁体   中英

Setting up classpath dynamically

I need to develop a client which can support any version of server. every time a new server is connected using the client, then appropriate server info is stored on client and the class pat h needs to be set to current server specific class path location. Based on server version class path needs to be set. I am facing setting up this class path dynamically. I always get ClassNotFound Exception. Could you please suggest how do I achieve this.

Classpath can't be set dynamically on runtime. You can do two things here: get familiar with OSGi or write your own classloader. For this particular example writwing own classloader would be easier. What do you keep in server specific classpath? Classes, resources, what? Some details could help.

I don't know what exactly are you trying to achieve, but assuming that you have all the needed libraries in your client machine and you are just trying to change the classpath at runtime based on a specific parameter received from the server, then I would give a go to ClassWorlds .

I would try defining a set of class realms using the ClassWorlds' configuration file and client's main class would be implemented using the ClassWorld argument in the main method . Then, the parameter received from the server would be the name of any of those realms. So, just after reading the realm parameter you just need to switch the current thread's classloader to the one created by ClassWorlds for the specific realm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM