Creating a Run Configuration
Using an IntelliJ IDEA run configuration for the JAR file we created.
Now you've created your JAR file, you need to run it to check that the behaviour is the same as running your main method in your HelloWorld.java
file. To run your Java application as a JAR, you can create a new run configuration.
-
Press ⇧⇧ (macOS) / Shift+Shift (Windows/Linux) to use search everywhere. If you don't know a shortcut in IntelliJ IDEA, search everywhere is a great way to search for what you need. In this instance, you want to create a new run configuration, so type into the dialog edit configurations.
-
When the correct option appears you can use the arrow keys to select it and press Enter.
- In the run configuration dialogue, you can see the run configuration that IntelliJ IDEA automatically created for running the main method in our HelloWorld.java class.
-
Let's create a new configuration for running our new JAR file. You can do that by pressing + (macOS) / Plus (Windows/Linux) and select JAR Application from the list.
-
First, give your new run configuration a name such as
HelloWorld JAR
so you can tell the difference between this one and the one that IntelliJ IDEA created earlier. -
Click on the browse button for the Path to the JAR. You need to navigate to your JAR file. Remember it's in the output for your project.
-
Scroll down to the Before launch section at the bottom of the dialog. Press + (macOS) / Plus (Windows/Linux) here and select Build Artifacts from the drop-down list.
-
Select and choose your
HelloWorld.jar
artifact and press OK. This means IntelliJ IDEA will rebuild yourHelloWorld.jar
file before we run this configuration.
- Press OK again to save the new run configuration. Your new run configuration is now displayed in the navigation bar by default.
If you press the black drop-down arrow adjacent to the HelloWorld JAR run configuration, you can see both your HelloWorld original run configuration and the new one we just created. Let's run your new HelloWorld JAR configuration to see if it works. Press the green arrow to the right of the run configuration with HelloWorld JAR selected in the drop-down.
You should get same "Hello World" output as you did from running the class file.
This proves that it works the way we expected. However, how do you know this is running your JAR file and not simply your class file?
If you look in the Run window, you can see that IntelliJ IDEA is using the -jar
argument to run the JAR file. It also shows the full path to a HelloWorld.jar
file.
../IdeaProjects/HelloWorld/out/artifacts/HelloWorld_jar/HelloWorld.jar
Beforehand, it showed the fully qualified class name, including the package. You can be confident that your new run configuration did run your HelloWorld.jar
that you created. Finally, let's check that your JAR file is being rebuilt each time you use the run configuration that we created as we configured it to do. We will do this in the next step of this tutorial by making a change to our class file and checking the output.