Preface:

Recently, I met a problem that almost drove me crazy; The problem was initially caused by using IDEA to Jar non-Maven projects; Jar package (bcprov-jdk15on-1.54.jar) has a signature, which will break the signature during the packaging process. Error: JCE cannot authenticate the provider BC.

Note: If the dependent third-party JAR package does not have a signature, the package can be successfully packaged as follows:

IDEA jar package:

Use IDEA to put the project into JAR package, and use the third-party dependent JAR package into;

1. First, look at the packaged project:

2. Specific packaging process:

2.1. Configure manifest.mf first

The manifest.mf needs to be configured first, because this is used at packaging time;

Create a new folder named meta-INF. Create a file named manifest.mf in the meta-INF folder and configure the content of the manifest.mf file. As follows:

Manifest-Version: 1.0
Class-Path: lib/log4j-core-2.0-rc1.jar lib/bcprov-jdk15on-1.54.jar 
Main-Class: com.lyl.Test.PackageTest

Copy the code

When filling in the manifest.mf configuration file, note the following:

1. Class-path must have a space after the colon; There must be a space between each dependency package that follows, and a space after the last dependency.

2, the main-class line should be followed by two blank lines;

Finally, a brief description of what manifest.MF does when packaging:

  1. Main-class specifies the entry to the program so that the program can be run directly with java-jar xxx.jar.

  2. Class-path specifies the Path of the dependent JAR package. The Class loader uses this Path to search for the dependent Class.

2.2 Specific configuration process

The following are some configurations before packaging. Before packaging, you need to configure these configurations.

(1),

(2),

(3),

(4),

(5),

(6),

(7),

(8),

2.3. Pack

After completing some configuration before packaging, you can package directly at last.

(1),

(2),

End, IDEA The jar package loading process has been completed. However, there may be problems when using the jar package directly. Let’s talk about the problems.

Extension: if you want to hit the JAR package, will rely on the third package JAR package into the words, using IDEA may be more cumbersome, but using Eclipse will be more convenient and fast, directly use Eclipse fat JAR plug-in package, very convenient.

3. Possible problems in the use of the jar package:

3.1, error: Invalid signature file digest for Manifest main attributes

Error: Invalid signature file digest for Manifest main Attributes;

This is because there are extra JARS in the prepared jar package. SF file, can be jar package suffix.sf file deletion can be.

3.2 error: JCE cannot authenticate the provider BC

JCE cannot authenticate the provider BC; JCE cannot authenticate the provider BC.

This is because the third-party JAR packages that the project relies on had signed JAR packages (bcprov-jdk15on-1.54.jar). The signature is broken when it is punched into the JAR package, causing an error to be reported when using classes in the package whose signature is broken.

3.3 Solutions:

3.3.1 Putting a signature package into the local JDK (not recommended)

This solution requires that the signature package (bcprov-jdk15on-1.54.jar) be placed in the ext folder under the lib folder of the JRE in the JDK.

For example, the JDK installation path is E:\Java\jdk1.8.0_60\jre\lib\ext; Put a copy of the signature package here.

But this solution is not recommended, and why not?

Because if you need to provide the JAR package to others, others need to change their OWN JDK in order to use your JAR package, which is not very convenient.

3.3.2 Do not rely on signature package when packaging (recommended)

When packing, do not type in the signature package. After the package is created, you need to place the dependent signature package and the generated package in the lib folder for use in the project. In this way, the dependent signature package at the same level as the generated signature package can be used.

When using this solution, it will be much more convenient than the first solution, but when using, in addition to introducing a good JAR package, also need to introduce the use of the signature package (BCprov-JDK15ON-1.54. Jar), and put it in the same level directory.

Implementation steps for not typing the signature package when packaging :(using bcprov-jdk15on-1.54.jar signature package as an example)

(1) First, modify the manifest.mf configuration file:

Before: Signature package exists

Manifest-Version: 1.0
Class-Path: lib/log4j-core-2.0-rc1.jar lib/bcprov-jdk15on-1.54.jar 
Main-Class: com.lyl.Test.PackageTest

Copy the code

Bcprov-jdk15on-1.54. Jar dependency added to bcprov-jdk15on-1.54. Jar dependency added to bcprov-jdk15on-1.54

Manifest-Version: 1.0
Class-Path: lib/log4j-core-2.0-rc1.jar
Main-Class: com.lyl.Test.PackageTest

Copy the code

(2) After modifying the configuration file, modify the imported dependent JAR package in (7) of the above packaging specific configuration steps, delete the signature package, and then click OK and repackage. The diagram below:

Previously, signature packages were also introduced:

After that, remove the imported signature package:

Bcprov-jdk15on-1.54. Jar () {if bcprov-jdk15on-1.54. Jar () {bcprov-jdk15on-1.54.Class can’t findHow to do?

Jar package “packagetest-1.0.0.jar” and bcprov-jdk15on-1.54.jar can be placed in the lib folder of the reference project (ensure that the two JAR packages are stored at the same level in the project). It will then use the class in the external reference dependency signature package, and will not report that the class was not found.

JCE cannot authenticate the provider BC because the signature of the packet is not damaged

End, I have only found these two solutions for the time being. I feel that neither of them is the optimal solution, but I really can’t find it. Alas, if there are friends who know, can you tell me? Thank you very much!

Don’t forget to leave your learning footprints

All see the article not praise are “play rascal”, hey hey ヾ(◍°∇°◍) Blue! Just kidding, move your little hands, like is done, each of you contribute (like + comment) will get more learners to join in! Thank you very much!  ̄  ̄ omega =