Java traverses all classes in the jar package-Subsequent

Source: Internet
Author: User

Previously, I wrote an article about all classes in a Java traversal package. However, a netizen reminded me that this Article can only be applied to packages in the src project, when the package is in jar, classes in jar cannot be traversed. In this regard, I have refined the code and further improved the functions.
Before sharing the source code, let's talk about the difficulties I encountered when traversing the classes in the jar package.
This is the jar package I used for testing. The structure is as follows:

Whether the package is in src or in jar, the fundamental idea is to give the package domain name (such as: com. wang. vo. request. hotel. test) locate the resource object of the package. In src, we can process the package as a File. Therefore, in src, the package is represented as a folder, but in jar, the meaning of a package is somewhat different. We cannot treat it as a File (in this case, you or get the jar File object ). So how can we get the resource objects in the jar package?
The Code is as follows:
[Java]
Public static void main (String [] args) throws Exception {
String packageName = "com/wang/vo/request/hotel/test ";
URL url = Thread. currentThread (). getContextClassLoader (). getResource (packageName );
If (url! = Null ){
System. out. println (url. getPath ());
}
}
However, after running the code above, we found that the url is always empty. We have tried various methods to obtain resources (such as getResourceAsStream), which cannot be solved, I tried other jar packages (such as spring), but found that the same code url has a value. After repeated tests and analysis, I found that the problem occurs when a jar package is generated.
Generally, the steps for creating a jar package are as follows:



And then Finish. This seems okay, but the problem occurs in this step:

The jar package generated by default contains only class files, but does not have the folder directory structure as we know. Maybe most of us think that the com. test. Student class files should be in the test folder under the com folder. This is actually one-sided and a misunderstanding!
Com. the true meaning of test is the package domain name, just like. the namespace in. net is just a concept proposed to distinguish, summarize, and uniquely identify different classes. It is different from the hierarchical structure of folder directories, we just get used to displaying packages in folders. But the package does not have to be displayed in a folder directory.
We can use the following code to further illustrate this problem.
[Java]
Public static void main (String [] args) throws Exception {
// Physical path of the jar package in the project
String jarName = "E:/Work/stsf_skisok_product/WebRoot/WEB-INF/lib/testpackage. jar ";
JarFile jarFile = new JarFile (jarName );
Enumeration <JarEntry> entrys = jarFile. entries ();
While (entrys. hasMoreElements ()){
JarEntry jarEntry = entrys. nextElement ();
System. out. println (jarEntry. getName ());
} Www.2cto.com
}
The default jar package is generated. The running result is as follows:
[Plain]
META-INF/MANIFEST. MF
Com/wang/util/DateStyle. class
Com/wang/util/PropertiesUtil $1. class
Com/wang/util/PropertiesUtil. class
Com/wang/util/Week. class
Com/wang/util/DateUtil. class
Com/wang/vo/request/hotel/test/PopularCityRequest. class
Com/wang/vo/request/hotel/test/Economy srequest. class
Com/wang/vo/request/hotel/test/define productvouchrequest. class
Com/wang/vo/request/hotel/test/QueryOrderListRequest. class
Com/wang/vo/request/hotel/test/sharelistqueryrequest. class
Com/wang/vo/request/hotel/test/RoomReserveRequest. class
Com/wang/vo/request/hotel/test/paionequeryrequest. class
Com/wang/vo/request/hotel/test/javasbrandrequest. class
If you check the jar package generated by the Add directory entries option, the running result is as follows:
[Plain]
META-INF/MANIFEST. MF
Com/
Com/wang/
Com/wang/util/
Com/wang/util/DateStyle. class
Com/wang/util/PropertiesUtil $1. class
Com/wang/util/PropertiesUtil. class
Com/wang/util/Week. class
Com/wang/util/DateUtil. class
Com/wang/vo/www.2cto.com
Com/wang/vo/request/
Com/wang/vo/request/hotel/
Com/wang/vo/request/hotel/test/
Com/wang/vo/request/hotel/test/PopularCityRequest. class
Com/wang/vo/request/hotel/test/Economy srequest. class
Com/wang/vo/request/hotel/test/define productvouchrequest. class
Com/wang/vo/request/hotel/test/QueryOrderListRequest. class
Com/wang/vo/request/hotel/test/sharelistqueryrequest. class
Com/wang/vo/request/hotel/test/RoomReserveRequest. class
Com/wang/vo/request/hotel/test/paionequeryrequest. class
Com/wang/vo/request/hotel/test/javasbrandrequest. class
This explains why getResource is always empty after the jar package is compiled.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.