0

I have a java project and i need to create jar file from my project . any body can tell me what is the simplest way to make this?

Abbas Rahimi
  • 202
  • 1
  • 5
  • 15

5 Answers5

6

From scratch.. with CMD

The command line is what almost every other application will use to build your JAR file. They just wrap it up a little nicer for you. In truth, it's very simple to do yourself. Obviously Java have explained this way in detail so there is no sense in me repeating it.

Note: You need to have your JDK/bin directoy appended onto your %PATH% system variable to be able to use this method.

Double Note: As pointed out in the comments, I'd suggest you keep trying this method until you understand it. It is very important that you get these things at a low level, so if something goes wrong with the IDE, you have a much better understanding of how to solve it.

Eclipse

Eclipse offers a nice interface for it. You can find a step by step tutorial here. The long and short of it is..

Right click on Project -> Export -> JAR File -> Select the Java files to include

When you've done this, hit finish and you're golden. The tutorial also adds some additional tips in to make it as seamless as possible.

IntelliJ IDEA

My personal favourite IDE. This question offers a nice explanation for how to export as a JAR file. Again, the long and short of it..

File -> Project Structure -> Artifacts

In there, you can then create a new artifact by clicking the + icon. This will give you an option for the file type, which is .JAR and which modules you want to include in your artifact. When you're done, you go to..

Build -> Build Artifacts

And it will create the JAR file from your project.

Using Maven

I've often found this to be a pretty awesome tool, and definitely one worth considering. In IntelliJ, by double clicking the install procedure in the life cycles..

enter image description here

This will create a new JAR file in your .target directory.

Note: Some IDEs (like IntelliJ) will hide the .target directory by default. Make sure you make it visible in the project settings.

christopher
  • 26,815
  • 5
  • 55
  • 89
  • Using the 'package' is enough to create the jar. The 'install' goal is used to store it inside your local maven repository. – Jeroen Jul 08 '14 at 13:47
6

The basic format of the command for creating a JAR file is:

jar cf jar-file input-file(s)

The options and arguments used in this command are:

  • The c option indicates that you want to create a JAR file.
  • The f option indicates that you want the output to go to a file rather than to stdout.

  • jar-file is the name that you want the resulting JAR file to have. You can use any filename for a JAR file. By convention, JAR filenames are given a .jar extension, though this is not required.

  • The input-file(s) argument is a space-separated list of one or more files that you want to include in your JAR file. The input-file(s) argument can contain the wildcard * symbol. If any of the "input-files" are directories, the contents of those directories are added to the JAR archive recursively.

  • The c and f options can appear in either order, but there must not be any space between them.

Vipin CP
  • 3,642
  • 3
  • 33
  • 55
3

A JAR file is nothing but a ZIP file with added meta-information for the Java Runtime Environment. So the easiest way is to actually zip your classes files including that META-INF folder by hand, then rename the file to JAR. How you get your classes files however is a different story.

The easiest practical way is to hit the build button in your IDE, which will then compile your code into class files, create an appropriate set of meta information and then conveniently zips the whole thing into a JAR file.

TwoThe
  • 13,879
  • 6
  • 30
  • 54
2

Asking for a maven solution, you would need to specify a single pom.xml in your project

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>you.specify.something.preferably.a.domain.name</groupId>
  <artifactId>name-of-project</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
</project>

And with this you can do a

mvn package

To build the project. However, it relies on a lot of defaults, you would probably want to differ.

Niels Bech Nielsen
  • 4,777
  • 1
  • 21
  • 44
1

To build a jar file from a module

On the main menu, choose Build | Build Artifact.
From the drop-down list, select the desired artifact of the type JAR. 

see this

bumbumpaw
  • 2,522
  • 1
  • 24
  • 54