Extracting to jar
file makes it convenient to run a Java program from command
line. But today I encountered a problem of extracting jar
file with IntelliJ Idea.
The problem is that after using the Build Artifact
function in IntelliJ Idea to
build a jar
file, I cannot run it from command line: it says “Error: Could not
find or load main class”.
I searched the Internet and finally found two solutions.
Solution 1: Manually delete some files from the jar
file
Delete from the jar
file these files: META-INF/*.RSA
, META-INF/*.SF
, META-INF/*.DSA
.
This is easy. You can open some archive manager software and delete these files and save. Then the program will run like a magic.
This solution is from Andreas Volkmann. In the solution, he mentioned that this is related to “signatures of interfering dependencies”, which inspired me to have a look at how to avoid these kind of signature of dependencies and leaded to the second solution.
Solution 2: Change extracting library jars to pack as it is
In the configuration of Artifact (Project Structure -> Artifact
), there is a
window for choosing source. What we need to do is to remove all the “Extracted”
things, and double click the library files in “Available Elements” on the right.
This is from Someone on stackoverflow.
Comments
- The problem of “could not find or load main class” here is due to that some dependencies are signed which mess up the META-INF folder.
- There are more advanced ways to excluding signature files.
- It is a good habit to keep library jars as it is when packaging a new jar.