[Error record] IntelliJ IDEA exports executable jar package execution error (java.lang.ClassNotFoundException | No dependent library selected when packaging)






1. Error message



Refer to 【IntelliJ IDEA】Export executable JAR package blog, export executable JAR package from IntelliJ IDEA;

Execute the menu bar / Build / Build Artifacts option to compile the artifacts project configured in the Module;
insert image description here

After compiling the artifacts, go to the project directory\out\artifacts\project name directory;

implement

java -jar Xxx.jar

command, execution error;

Final error: The reason for the error is that no dependent library was found, and additional dependencies need to be set;

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)

insert image description here





2. Solution 1



The reason for the above problems is that when configuring "Project Structure", there is no option to package and configure the dependent libraries together;

insert image description here

When creating Artifact, the first option "extract to the target JAR" only exports the target jar package;

The second option is to export the target jar package and all dependencies;

If you choose the first option, then if there is a dependent library, there will be a problem that the dependency is not found;

insert image description here

In this way, when exporting, the dependent library will be automatically copied to the output directory;

Dependent libraries will be configured into Class-Path one by one;

Manifest-Version: 1.0
Main-Class: Xxx
Class-Path: kotlin-stdlib-1.4.32.jar jedis-3.2.0.jar mssql-jdbc-9.4.0.jr
 e8.jar json-20210307.jar commons-pool2-2.6.2.jar kotlin-stdlib-common-1
 .4.32.jar rxjava-2.2.21.jar slf4j-api-1.7.25.jar annotations-13.0.jar r
 eactive-streams-1.0.3.jar commons-math3-3.6.1.jar






3. Solution 2



Configure the used jar package into the Class-Path key-value pair, and separate multiple dependencies with spaces;

Manifest-Version: 1.0
Main-Class: cn.zkhw.schedule.graph.tools.LargeMapCanvas
Class-Path: . mssql-jdbc-9.4.0.jre8.jar rxjava-2.2.21.jar slf4j-api-1.7.25.jar reactive-streams-1.0.3.jar kotlin-stdlib-common-1.4.32.jar kotlin-stdlib-1.4.32.jar json-20210307.jar jedis-3.2.0.jar commons-pool2-2.6.2.jar commons-math3-3.6.1.jar annotations-13.0.jar

It can only be patched based on the current state, then copy the jar package of the dependent library to the same level directory as the exported jar package, and execute

java -jar xxx.jar

Order ;


Copy all dependent libraries to the same directory as the executable jar package, execute java -jar xxx.jarthe command directly, and the program can be executed normally;

insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/132580716