Make a note. The environment in which the sample was created is Windows 10.
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!
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