javamavenjarexecutableexecutable-jar

The simplest way to create a jar file?


I have a Java project and need to create a jar file for it. What is the simplest way to do this?


Solution

  • 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.