Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Check if package exists at runtime

Thread view: 
Bruce Lee - 27 Jan 2006 07:50 GMT
Is there anyway to check if a package exists at runtime? Class is simple -

try{
Class.forName("java.class1.Class12");
}catch(Exception e){}

but can you do this for a whole package?
Damian Brunold - 27 Jan 2006 08:00 GMT
> Is there anyway to check if a package exists at runtime? Class is simple -
>
[quoted text clipped - 3 lines]
>
> but can you do this for a whole package?

Package.getPackages() gets all packages known to the callers
ClassLoader. At least this is what the documentation says...
Robert Klemme - 27 Jan 2006 08:19 GMT
>> Is there anyway to check if a package exists at runtime? Class is
>> simple -
[quoted text clipped - 7 lines]
> Package.getPackages() gets all packages known to the callers
> ClassLoader. At least this is what the documentation says...

public static boolean packageExists(String name) {
 return Package.getPackage(name)!=null;
}

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Package.html#getPackage(java.l
ang.String
)

   robert
Chris Smith - 27 Jan 2006 15:36 GMT
> public static boolean packageExists(String name) {
>   return Package.getPackage(name)!=null;
> }

Note that this will return true ONLY when a class from the package has
already been loaded.  It does not somehow check if anything from the
package might be available in the future.  That is the JVM's definition
of whether the package exists (it doesn't exist until it's first used),
but it's probably not the definition the OP wanted.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Stefan Schulz - 27 Jan 2006 11:49 GMT
You can, if the package is sealed, just check if all classes can be
retrieved by Class.forName(). Otherwise, the notion of "whole package"
makes no sense, since there may be any number of classes also living in
that package you have never heared about.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.