aspose file tools*
The moose likes Java in General and the fly likes How can you check for the existance of system packages? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How can you check for the existance of system packages?" Watch "How can you check for the existance of system packages?" New topic
Author

How can you check for the existance of system packages?

Steve Bentley
Greenhorn

Joined: May 08, 2009
Posts: 1
Hi,

I need to be able to check whether certain system packages exist when my Java app loads. For my specific use-case the system packages are listed in a .properties file. I assumed that I could use for example ClassLoader.getSystemResource("javax/swing"); to test whether a specific package exists, but this doesn't work. However the following works for other packages on my classpath: myClassLoader.getResource("com/example/mypackage");

This is a simple runnable example:

The console output you will see when you run this program is:
Found [com/example/mypackage] with myClassLoader
Could not find [javax/swing]

Is there any way to check for system packages without having to know the classes they contain?

For the curious, my application is embedding the Felix OSGi runtime into a webapp. The system classes which my OSGi bundles need are specified in a .properties file and I would like to be able to check that all the packages exist before I boot OSGi. Without the check, the only way I know if a system package doesn't exist (for example, I miss spelt it in the .properties file) is when an OSGi bundle that needs it tries to load a class and gets a NoClassDefFoundError.

Thanks in advance for your help.
Steve Bentley
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Check out java.lang.Package


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
jQuery in Action, 2nd edition
 
subject: How can you check for the existance of system packages?