Create Jar Library Without a Main Class

Thibstars picture Thibstars · May 11, 2015 · Viewed 21.5k times · Source

Today I have just created a Java Library. I created it using a Main class, since IntelliJ IDEA 14 asked me to add one. However I want it to be a normal library, without any Main classes. Is it possible to create a jar file from such a project without having a single class with the main method? If so, how do you create such a jar.

It just seems a bit silly to have a main method if you never use it.

Answer

Darrell Teague picture Darrell Teague · Apr 3, 2018

Use a build tool like Maven (no IDE dependencies but can be called from IDE for convenience) with the shade plugin to create an 'uber' JAR (that includes all needed dependencies into one final JAR for the project)...

"pom.xml"

...

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.3</version>
    <executions>
       <!-- Run shade goal on package phase -->
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

Documentation to Shade plugin:

https://maven.apache.org/plugins/maven-shade-plugin/