Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 4616

How to Run a Class Within a WAR File Using the Command Line

$
0
0

1. Overview

A WAR file, short for web application archive or web application resource file, stores the resources of a Java web application. WAR packages all the web components into a single unit. It contains JAR files, JavaServer Pages, Java servlets, Java class files, XML files, HTML files, and other resources required for web applications.

In this tutorial, we’ll show how to call a class within a WAR file using the CLI.

2. Structure of a WAR file

WAR files use the .war extension and package web applications that we can deploy on any Servlet/JSP container.

Here’s an example layout of a typical WAR file structure:

META-INF/
    MANIFEST.MF
WEB-INF/
    web.xml
    jsp/
        helloWorld.jsp
    classes/
        com/baeldung/*.class
        application.properties        
        static/        
        templates/
    lib/
        // third party *.jar files as libs
index.html

Inside, there is a META-INF directory holding useful information in the MANIFEST.MF about the web archive. The META-INF directory is private and inaccessible from the outside.

The WEB-INF directory is a special directory that contains all deployment information and application code. The WEB-INF/classes and WEB-INF/lib directories contain Java class files and JAR libraries, respectively. The WAR file also has all the static web resources, including HTML pages, images, and JS files.

3. Running a Class Within a WAR file

Let’s look at different approaches to running a class from a WAR file via the command line.

3.1. Using java Command With -classpath Option

The Java Virtual Machine (JVM) uses the classpath environment variable to locate and load classes when running a Java program. It specifies a list of directories, JAR files, and ZIP files where the JVM should look to find and load class files.

Let’s look at the below java command to run a WAR and its output. We can also use -cp instead of the -classpath option:

java -classpath target/maven-generate-war/WEB-INF/classes:target/maven-generate-war/WEB-INF/lib/* com.baeldung.MavenGenerateWarApplication

The above command executes the main method, produces the below output, and then terminates the server once it is completed:

2025-01-26 23:46:02.332  INFO 46563 --- [    main] c.baeldung.MavenGenerateWarApplication   :

Starting MavenGenerateWarApplication using Java 23.0.1 on MacBook-Pro local with PID 46563 2025-01-26 23:46:02.334 INFO 46563 --- [ main] c.baeldung.MavenGenerateWarApplication :
No active profile set, falling back to 1 default profile: "default" 2025-01-26 23:46:02.782 INFO 46563 --- [ main] c.baeldung.MavenGenerateWarApplication :
Started MavenGenerateWarApplication in 0.766 seconds (JVM running for 1.064)

3.2. Using java Command With –jar Option

We can also build a WAR file with Spring Boot, including an embedded server (like Tomcat) directly within the application. We don’t need to deploy a WAR file to an external servlet container like Apache Tomcat. The WAR contains everything we need to run the application, including the server.

We’ve specified the MavenGenerateWarApplication class as the mainClass in our pom.xml. Now, we’ll execute the below command to run the WAR file:

java -jar target/war-with-main-class.war

Running the above command will run the MavenGenerateWarApplication class, start the server, and keep it running. We’ll see the below output, which shows the Spring Boot application has started:

2025-01-26 01:25:19.762  INFO 99652 --- [    main] o.apache.catalina.core.StandardService   :

Starting service [Tomcat] 2025-01-26 01:25:19.763 INFO 99652 --- [ main] org.apache.catalina.core.StandardEngine :
Starting Servlet engine: [Apache Tomcat/9.0.74] 2025-01-26 01:25:20.256 INFO 99652 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] :
Initializing Spring embedded WebApplicationContext 2025-01-26 01:25:20.256 INFO 99652 --- [ main] w.s.c.ServletWebServerApplicationContext :
Root WebApplicationContext: initialization completed in 1009 ms 2025-01-26 01:25:20.378 INFO 99652 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping :
Adding welcome page template: index 2025-01-26 01:25:20.450 INFO 99652 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer :
Tomcat started on port(s): 8080 (http) with context path '' 2025-01-26 01:25:20.455 INFO 99652 --- [ main] c.baeldung.MavenGenerateWarApplication :
Started MavenGenerateWarApplication in 1.575 seconds (JVM running for 1.834)

4. Conclusion

In this article, we discussed different approaches to running a WAR file via a command line in Java.

Running a WAR file with java -jar is the best option for running modern web applications that use embedded servers. On the other hand, java -classpath works better for running non-web components without needing a servlet container.

As always, the code used in this article can be found over on GitHub.

The post How to Run a Class Within a WAR File Using the Command Line first appeared on Baeldung.
       

Viewing all articles
Browse latest Browse all 4616

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>