File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Adding files....dynamically. to the classpath. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adding files....dynamically. to the classpath." Watch "Adding files....dynamically. to the classpath." New topic
Author

Adding files....dynamically. to the classpath.

A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,

Suppose we have a application accessing a properties file and the JVM starts and application starts using the properties file...

Now i want to modify the properties file ...

Does this require the restart of the application..

Are there any features like hot deployment of applications in servers which would allow me to dynamically access the modified version as the latest version??

Regards
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You can create a new ClassLoader every time you find a changed class file and want to load it. I looked at JUnit source code as an example of how to do it. Google for articles on custom class loaders; it's non-trivial to get completely right.

It's possible to get into weird places with custom class loaders, equals() or compareTo() methods, reflection, static variables, Singletons and such. If you run into trouble later, remember custom loaders might be involved.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi Stan ,

Thanks for responding ...

I dont have much idea about this....Suppose the server has already started and the app. has started ..Later the file /class has changed...

Now how can i load the changed .class file using the classloader...mechanism..

Should i write another program...for this???
Bakul Ghude
Greenhorn

Joined: Nov 28, 2006
Posts: 7
I think if you are asking about *.properties file and
if you made changes in that *.properties file then you must restart it for getting required changes that you have made in that file.
ok
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
I think if you are asking about *.properties file and
if you made changes in that *.properties file then you must restart it for getting required changes that you have made in that file.



Cant we make us of the class loader mechanisms here???
Ravindra Rawat
Ranch Hand

Joined: Dec 09, 2004
Posts: 34
If i understand the problem correctly, you need to reload changes to your configuration etc from a properties file on the fly.

If this properties file is on the filesystem then you can check the timestamp of properties file using . If modified you can invoke the method configuring your application(synchronously) and blocking access to configuration till reloaded. This is fine in case changes are done in off peak hours for the application else handling is complex and better to reload the app, if possible.

It is similar to a file watchdog e.g http://logging.apache.org/log4j/docs/api/org/apache/log4j/helpers/FileWatchdog.html

Thanks
Ravindra
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
BTW: Google for "java reload class" got a first hit called Dynamically Reloading a Modified Class. Search around and you'll find far more expert advice than mine.

Reloading configuration is a very cool capability. In my last big work project I made a "dashboard" with a button to reload part or all of configuration from a variety of sources. In my Wiki I edit the configuration pages through the application and the "save" operation publishes an event that different components listen for.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding files....dynamically. to the classpath.