开发者

checking whether a package is existent or not

开发者 https://www.devze.com 2022-12-25 07:28 出处:网络
How can i check whether a package like javax.servlet.* exists开发者_StackOverflow or not in my installation of java?Java can only tell you if it can load a class. It can\'t tell you if a package exist

How can i check whether a package like javax.servlet.* exists开发者_StackOverflow or not in my installation of java?


Java can only tell you if it can load a class. It can't tell you if a package exists or not because packages aren't loaded, only classes.

The only way would be by trying to load a class from that package. e.g., For javax.servlet.* you could do:

try {
    Class.forName("javax.servlet.Filter");
    return true;
} catch(ClassNotFoundException e) {
    return false;
}


Check if package is present as a resource:

// Null means the package is absent
getClass().getClassLoader().getResource("javax/servlet");

Alternatively, check if some class of this package can be loaded via Class.forName(...).


If you look in the API docs for the installation you have, it will tell you all the installed packages, eg: http://java.sun.com/j2se/1.5.0/docs/api/

In code, you can do something like this:

Package foo = Package.getPackage("javax.servlet");

if(null != foo){
  foo.toString();
}else{
  System.out.println("Doesn't Exist");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号