This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 19655
    
  18

Check out java.lang.Package


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can you check for the existance of system packages?
 
Similar Threads
Reading a file without specifying any path
importing other classes and using classpath
Check the file/found or not
New Article: Adding Plugins to a Java Application
package vs directory