Bummer! This is just a preview. You need to be signed in with a Pro account to view the entire video.
Running a JAR from the Command Line2:48 with Chris Ramacciotti
In this video, we create a runnable JAR of our Spring app, so that we can run it with an embedded web server from the command line with the `java` command.
Using Github With This Course
You can complete this course by using the code I've made available to you in two ways:
- Use the Github repository I've made available (recommended), or
- Use the project files linked at the bottom of this page
If you choose the recommended option of using the Github repository, it can be found at
To utilize Github with this course, you can download the Github desktop client for your system or use the command line interface provided with Git.
Clone this repository to your machine using the Github desktop client, or using the following command:
git clone email@example.com:treehouse-projects/spring-deploy-weather.git
The next option we're going to cover for building in deploying our app, 0:00 is with a JAR file. 0:03 Now, you've heard of JAR files before. 0:04 JAR stands for Java archive, and it's a way to roll up all the code analysis, 0:06 of an application into a single file. 0:09 It's kind of like a ZIP file, that contains Java code. 0:11 I want to clarify a new term that I'm using here, and that is the term build. 0:14 To build an application in our case means to compile and 0:17 bundle all necessary byte code and assets like properties files and 0:21 Thymeleaf templates into a form that we can run. 0:25 The form in this scenario is a JAR file. 0:28 We're going to create a runnable JAR file that we can run from the command line. 0:31 Now doing this in IntelliJ or from the command line takes only one step. 0:34 In IntelliJ, you can use a grill tool window and 0:38 run the build task if you expand the build category. 0:41 You will see a build task there, double click on that to run it. 0:43 Or from the command line, you can use ./gradlew or 0:47 ./gradlew.bad if you're on Windows, and then build. 0:51 Note that the build task in Gradle will run unit tests. 0:58 So for this project, if you haven't copied API keys 1:02 into the api.properties files as it says in the readme of the GitHub repo, 1:06 be sure to do that now or your unit tests will fail. 1:11 Okay. 1:15 So what this task does is create a runnable JAR file in the build directory 1:16 of your project. 1:20 A runnable JAR file is a JAR file that has Java code in it and 1:21 includes a class marked as the main class. 1:24 The class marked as a main class is the one that contains our 1:27 public static void main method. 1:30 For us, that's the application class. 1:31 So in the project navigator here you should see a build directory pop up and 1:35 if you expand that build directory and 1:39 then expand the libs directory, here is the snapshot that was just created. 1:41 There is that JAR file, you see that .jar at the end of the file name. 1:46 Running the JAR file at this point will produce the same result as we got 1:51 running the Gradle boot run task in the last video. 1:54 The benefit here is that we now have a portable build, our JAR file. 1:57 We could run this from any computer or server. 2:01 Let's do that now. 2:03 In order to deploy or run this build we'll need to issue the Java command like this. 2:04 Let me clear my console here, and we'll say, java-jar. 2:09 And that's in the, build/libs, directory. 2:14 And it's called a weather-0.0.1- snapshot.jar. 2:18 And if I hit enter there, what you can see 2:26 is that we're getting the same output here as we got with the boot run task, and 2:28 that's because we're still running the application with that embedded web server. 2:32 So what if you already have a running web server and 2:38 you want to deploy this application to that server? 2:41 Well my friends, stick with me, because that's what we're going to do next. 2:44
You need to sign up for Treehouse in order to download course files.Sign up