• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Designing a pluggable application.

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am in the midst of designing a Java application that will use plug-ins to extend its functionality. I have figured out that I can use the Class object and its forName() and newInstance() methods to create objects from classes stored in a plug-in JAR.

Here is my question:

All of my plug-ins will store there JAR files in a single directory of my application directory on the file system. How do I get the JVM to search in this plug-in directory when I use the forName() method of the Class object? Do I have to get the user to add the directory to there classpath, or is there another way around this? I didn't see a way to pass the location of the plug-in directory of my application when using the forName() method.

Thanks,

Landon

P.S. - When I load a class in this way, are all classes in the JAR loaded, or just the one I specify? If it is just the one I specify, how does the object created call other objects in its JAR? If all classes are loaded, how do I unload them when a plugin is no longer needed in my application?
[ March 08, 2006: Message edited by: Landon Blake ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can cause each jar file to contain a configuration file that tells the plugins it contains for whatever interfaces you have.

Or the file could be outside the jar. Take a look at Eclipse plugins. There is some kind of configuration file in each directory in the plugins area.

Once you have the configuration (the jar, the interface and the implementing class) you can use a ClassLoader with a modified classpath to create objects with Class.forName().newInstance().

Any of those sound useful?
[ March 08, 2006: Message edited by: Stan James ]
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Do I have to get the user to add the directory to their classpath?


Yes and no.
The user has to add the jar-file to the classpath, or a directory containing extracted classes (with their subdirectories).

The classpath is a list of things, which contain classes.

That might be jars or directories, but not directories containing jars.
(It took me 15 years to understand that simple thing )

I don't have an idea of how to restrict the searching to a part of the classpath.

You could create a script to extract all classnames, and tell the user to run it after putting new plugins there:


and search that list. Perhaps the easiest solution.

If it is just the one I specify, how does the object created call other objects in its JAR?


That's the job of the classloader. He will do it.

If all classes are loaded, how do I unload them when a plugin is no longer needed in my application?


Job of the GarbageCollection. Let it do it.
 
Landon Blake
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stefan and Stan for your quick responses.

I was able to answer some of my questions while reading online. It turns out that you can specify an application specific class path when you launch a Java application. I can build this into the script file that launches my application. (A Java application launched this way still has access to the system classes.)

If what Stefan said was correct, I'll have to figure out how to set the classpath to each JAR in the directory, and not the directory itself. This might be possible, as I will know ahead of time what JARS I will need to add to the classpath. I have to find out how to modify the classpath during runtime. (Or maybe use a custom class loader?)

Thanks for the help.

Landon
[ March 08, 2006: Message edited by: Landon Blake ]
 
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic