Single java file is typed as executable jar package


1 Overview

Use the jar and Java provided by JDK to print a single java file into an executable jar package and run it.
Of course, you can also use ide. You only need a simple package to use maven, but you don’t need to be so “fierce” for a single file.

2 new test file

The famous Hello World:

public class Main
    public static void main(String [] args)
        System.out.println("Hello world.");

3 compilation

Class files are required to generate jar packages. In other words, they need to be compiled first. It is recommended to create a temporary folder to store the class files.

mkdir test && mv test && cd test;

4 packing

jar --create --verbose --file Main.jar --main-class Main *.class

Describe the parameters as follows:

  • --create: create jar
  • --verbose: generate output when packaging
  • --file: the name of the packaged jar file
  • --main-class: specify entry class
  • *.class: package all class files. The acceptable parameters here can be*, means packing all files in the directory, or directory name, packing all files in the specified directory

The default package is used here. If it is a custom package, use the


That’s fine.

Note that some online tutorials use abbreviations when packing:

jar -cvf Main.jar *.class

In this way, it can be packaged, but when running directly, you will be prompted:

no main manifest attribute, in Main.jar

Can be added--main-classParameters or directly update theMANIFEST.mfDocuments, plus:

Main-Class: Main

Of course, it is recommended to pack in place one step by one using the above methods.

5 operation

java -jar Main.jar

Recommended Today

nuxt.js Imitating wechat app communication chat | Vue + nuxt chat | imitating wechat interface

Project overview be based on vue.js + nuxt.js +Chat room imitating wechat app interface developed by vuex + webpack + node + vant and other technologiesNuxtchatroom project。 It realizes the functions of card type drag and slide, message / expression sending, picture / video preview, red packet / circle of friends and so on. Technology […]