Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Jar Library Without a Main Class

Tags:

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.

like image 249
Thibstars Avatar asked May 11 '15 18:05

Thibstars


People also ask

Can I create a jar without main class?

Yes, but since it is a library this class will be available in it too. It just seems weird to add a class in that has no function whatsoever. just delete the Main class. You didn't need to create it in the first place.

How do I create a JAR file without manifest?

yes zip it and change extension thats it :) a . jar file is nothing but a zip file with a different extension. a manifest and main class are only needed if you want to run your jar as executable, but a jar can just as well be used as a library of classes, used after importing in a project.

How do I manually create a JAR file?

Open the Jar File wizard In the Package Explorer select the items that you want to export. If you want to export all the classes and resources in the project just select the project. Click on the File menu and select Export. In the filter text box of the first page of the export wizard type in JAR.


2 Answers

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/

like image 183
Darrell Teague Avatar answered Sep 17 '22 14:09

Darrell Teague


You can do it in few ways, for example from command line, from IDE, maven or other build tool, I describe 2 ways:

Command line:

You can create jar file from command line (without IDE), Here is reference: https://docs.oracle.com/javase/tutorial/deployment/jar/build.html

jar cf jar-file input-file(s) 

where jar-file is .jar file name you want and input-file(s) are files you want to put inside your library (can be a wildcard, e.g.: *.class)

Intellij Idea:

Create Artifact like in this article, but without specifying Main class http://blog.jetbrains.com/idea/2010/08/quickly-create-jar-artifact/

Then click Build > Build artifact > Build.

This works even if there is no Main class.

like image 41
dey Avatar answered Sep 17 '22 14:09

dey