Detects conflicting classes in multiple jar packages.

Source: Internet
Author: User

When a project uses more and more jar packages, the code often throws an exception during running: Java. lang. nosuchmethodexception, Java. lang. nosuchfielderror is basically caused by the existence of multiple jar packages containing the same class file. This method may not be used for classes during runtime.

 

I. Ideas

In order to find a jar package with the same class in advance, I am going to write a small program. Because it is a jar package, it needs to be processed using Java code, but it is generally a class file, the user may need to call it through command line execution. I am not good at Java Desktop programs, so I thought of using it. net to develop a tool shell, internally calling Java code to execute, started to think of using.. Net to process the jar package. It provides the basic java package.. net. But loading it will also increase the size of the program by several Mb. Finally, I changed my mind and the code was written in Java ,.. net uses Java commands to execute class files written in Java and then display the output content. net tool interface, this will greatly reduce the size of the tool.

 

Ii. Technical implementation:

Add a Java console program, which has the default main Method Portal. You need to pass the jar package path to be analyzed. The parameters are passed through the outside world. net interface input parameters are passed to the main entry of Java, such as the Code:

Public static void main (string [] ARGs ){

If (ARGs. Length <1 ){

System. Out. println ("Enter the jar package path! ");

Return;

}

Then, obtain all jar package files under the path based on the input ARGs path, and round-robin all jar packages to obtain the existing class files, put the jar package and the corresponding class file into a dictionary class. After polling all the jar packages, you can use the dictionary class to obtain the files with multiple jar packages of one class. The dictionary class structure is hashmap <string, hashset <string>. The class name is used as the key. For example, it is com/Apache/ABC/demo. class: The name of multiple jar packages is used as the value, which is unique to the key. The value is a set dictionary, which facilitates the output of the class with multiple jar package values from the dictionary. The structure is as follows:

Key

Value (hashset)

Org/Apache/taglibs/standard/lang/jstl/implicitobjects. Class

 

Standard-1.1.2.jar

Jstl-1.2.jar

...

...

...

 

The main implementation code is as follows:

File file = new file (ARGs [0]);

Map <string, hashset <string> jarmap = new hashmap <string, hashset <string> ();

If (null! = File & file. exists () & file. isdirectory ()){

File [] jarfile = file. listfiles ();

For (file F: jarfile ){

If (F. isfile () & F. getname (). endswith (". Jar ")){

Try {

Jarfile jar = new jarfile (f );

Enumeration <jarentry> enumjar = jar. Entries ();

While (enumjar. hasmoreelements ()){

Jarentry je = enumjar. nextelement ();

If (JE. getname (). endswith (". Class ")){

If (jarmap. containskey (JE. getname ())){

Jarmap. Get (JE. getname (). Add (F. getname ());

} Else {

Hashset <string> set = new hashset <string> ();

Set. Add (F. getname ());

Jarmap. Put (JE. getname (), set );

 

The code for searching duplicate classes is as follows:

For (string S: Keys ){

If (jarmap. Get (s). Size ()> 1 ){

// Duplicate

Hashset <string> hsfile = jarmap. Get (s );

String sfiles = "";

For (string SF: hsfile ){

Sfiles = sfiles + SF + ",";

}

If (sfiles. Length ()> 1 ){

Sfiles = sfiles. substring (0, sfiles. Length ()-1 );

}

System. Out. println (sfiles + "repeated classes:" + S );

}

}

Finally, Java program output

System. Out. println ("[[over]");

The purpose here is to mark the end, and the. NET code displays the content based on the end mark.

The Java code will output the found jar package and conflicting classes to the console.. Net can be processed and then displayed based on the executed console string .. . Net calls the Java command by calling cmd through a new process. The Code is as follows:

Public static string runcmd (string command)

{

System. Diagnostics. PROCESS p = new system. Diagnostics. Process ();

P. startinfo. filename = "cmd.exe ";

P. startinfo. useshellexecute = false;

P. startinfo. redirectstandardinput = true;

P. startinfo. redirectstandardoutput = true;

P. startinfo. redirectstandarderror = true;

P. startinfo. createnowindow = true;

P. Start ();

P. standardinput. writeline (command );

P. standardinput. writeline ("exit ");

String result = P. standardoutput. readtoend ();

P. waitforexit (); p. Close ();

Return result;

}

Command Input: Java checksame + "interface entry path". The software interface is shown as follows:

As mentioned above, the problem of multiple classes in the jar package is detected, and the code is not complex. The implementation method is used to solve the actual problem.

 

 

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.