How to create a jar file or war file using the jar command

How to create jar file and war file with jar command

Make a note. The environment in which the sample was created is Windows 10.

How to make a jar file

The sample source file is as follows.

App.java


package sample;
public class App {
	public static void main(String[] arg) {
		System.out.println("Hello World!");
	}
}

The directory structure is as follows. The .class files are placed in the classes folder after compilation. Manifest files to be imported into jar are placed under META-INF.

C:\jar file creation sample>tree /F
List of folder paths:Volume OS
Volume serial number is XXXX-XXXX
C:.
├─classes
├─META-INF
│      MANIFEST.MF
│
└─src
    └─sample
            App.java

The manifest file specifies the Java class in which the main method will be executed when the jar is executed.

MANIFEST.MF


Main-Class: sample.App

Now let's compile before creating the jar. Compile with the javac command. Use -sourcepath to specify the folder where the source files are located. Use -d to specify the folder where the compiled files will be placed.

javac -sourcepath src -d classes src\sample\App.java

Compiling will create a .class file under the classes folder.

C:\jar file creation sample>tree /F
List of folder paths:Volume OS
Volume serial number is XXXX-XXXX
C:.
├─classes
│  └─sample
│          App.class
│
├─META-INF
│      MANIFEST.MF
│
└─src
    └─sample
            App.java

Then use the jar command to create a jar file. Specify the manifest file to be imported into the jar file with m. The subordinates of the folder specified by -C are compressed and imported into the jar file.

jar cvfm sample.jar META-INF\MANIFEST.MF -C classes .

The structure of the created jar file is as follows.

C:\jar file creation sample>jar tf sample.jar
META-INF/
META-INF/MANIFEST.MF
sample/
sample/App.class

The execution result is as follows.

C:\jar file creation sample>java -jar sample.jar
Hello World!

How to make a war file

The sample source file is as follows.

SampleServlet.java


package sample;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/sample")
public class SampleServlet extends HttpServlet {
	
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html");
		resp.setCharacterEncoding("UTF-8");
		ServletOutputStream out = resp.getOutputStream();
		out.println("Hello World!");
		out.flush();
	}
}

The directory structure is as follows. The .class files are placed in the WebContent \ WEB-INF \ classes folder after compilation. The libraries required to compile the above source files are placed under lib.

C:\war file creation sample>tree /F
List of folder paths:Volume OS
Volume serial number is XXXX-XXXX
C:.
├─lib
│      javax.servlet-api-3.1.0.jar
│
├─src
│  └─sample
│          SampleServlet.java
│
└─WebContent
    └─WEB-INF
        └─classes

Now let's compile before creating the war. Compile with the javac command. Use -sourcepath to specify the folder where the source files are located. The library is specified by -classpath. All files under the lib folder can be specified by using *. Use -d to specify the folder where the compiled files will be placed.

javac -sourcepath src -classpath lib\* -d WebContent\WEB-INF\classes src\sample\SampleServlet.java

Compiling will create a .class file under the WebContent \ WEB-INF \ classes folder.

C:\war file creation sample>tree /F
List of folder paths:Volume OS
Volume serial number is XXXX-XXXX
C:.
├─lib
│      javax.servlet-api-3.1.0.jar
│
├─src
│  └─sample
│          SampleServlet.java
│
└─WebContent
    └─WEB-INF
        └─classes
            └─sample
                    SampleServlet.class

Then use the jar command to create a war file. The subordinates of the folder specified by -C are compressed and imported into the war file.

jar cvf sample.war -C WebContent .

The structure of the created war file is as follows.

C:\war file creation sample>jar tf sample.war
META-INF/
META-INF/MANIFEST.MF
WEB-INF/
WEB-INF/classes/
WEB-INF/classes/sample/
WEB-INF/classes/sample/SampleServlet.class

If you deploy the created war file to Tomcat etc. and access it from a browser, Hello World! Will be displayed.

Recommended Posts

How to create a jar file or war file using the jar command
Create a jar file with the command
[chown] How to change the owner of a file or directory
[Rails] How to create a graph using lazy_high_charts
How to delete a controller etc. using a command
How to run a GIF file from the Linux command line (Ubuntu)
[Java] Create a jar file with both compressed and uncompressed with the jar command
How to create a server executable JAR and WAR with Spring gradle
Create a Jar file with two lines of command
How to convert A to a and a to A using AND and OR in Java
How to create an Excel form using a template file with Spring MVC
How to debug the generated jar file in Eclipse
How to create a method
How to create a form to select a date from the calendar
How to create a placeholder part to use in the IN clause
How to change the contents of the jar file without decompressing
[Java] How to create a folder
How to create a registration / update function where the table crosses
How to make a jar file with no dependencies in Maven
[Rails 6] How to create a dynamic form input screen using cocoon
Create a static file that expands variables using the ERB class
How to create a new Gradle + Java + Jar project in Intellij 2016.03
Easy way to create a mapping class when using the API
How to run a Kotlin Coroutine sample from the command line
Add a time stamp to the JAR file name in Gradle
[Xcode] How to add a README.md file
How to execute a contract using web3j
How to sort a List using Comparator
[Java] How to use the File class
Create a large number of records with one command using the Ruby on Rails seeds.rb file
How to delete the wrong migration file
How to delete the migration file NO FILE
How to add jar file in ScalaIDE
How to create a Maven repository for 2020
[Swift5] How to create a splash screen
[rails] How to create a partial template
[Swift5] How to create a .gitignore file and the code that should be written by default
How to place geckodriver (Selenium WebDriver) on the path using Maven command
How to return a value from Model to Controller using the [Swift5] protocol
How to create a query using variables in GraphQL [Using Ruby on Rails]
How to find out the Java version of a compiled class file
[Java] How to get to the front of a specific string using the String class
[Rails] How to create a table, add a column, and change the column type
How to download and run a Jar package directly from the Maven repository with just the command line
How to create a database for H2 Database anywhere
Write to a file using ShiftJIS-Read a file (Kotlin / JVM)
[Ethereum] How to execute a contract using web3j-Part 2-
How to create pagination for a "kaminari" array
How to implement the breadcrumb function using gretel
How to create a class that inherits class information
How to create a theme in Liferay 7 / DXP
To manually deploy Struts2 as a war file
[1st] How to create a Spring-MVC framework project
How to easily create a pull-down in Rails
How to generate a primary key using @GeneratedValue
Double-click to open the jar file on Windows
[Rails] How to create a Twitter share button
How to create hierarchical category data using ancestry
[Spring Boot] How to refer to the property file
How to create a route directly from the URL you want to specify + α
[Note] Java: Create a simple project while learning how the configuration file works.