How to build an artifact from simple java application project in IntelliJ?

김영석
5 min readSep 12, 2020

--

“Building Java Archive file is the most basic start of shipping codes to somewhere it’s needed.”

Continuing from the previous article about how to start a simple java application project in IntelliJ, I would like to talk about how to build an artifact(Java Archive File) from the project in IntelliJ.

Option1. Compile a single file or class.

Before we actually begin to compile the whole files or classes, I would like to show you how to compile a single file or class and what comes out of it as a result.

Step1. Press the button Recompile ‘ClassName.java’.

You can find the button in right-click menu or Build menu at the top.

What the Recompile does is to compile the java file into class file into the output directory.

as you can see the compiled Main.class file resides in out/production/SimpleApplication folder. That is because IntelliJ basically follows the output directory rules as followings:

  • Sources: <ProjectFolder>/out/production/<ModuleName>
  • Tests: <ProjectFolder>/out/test/<ModuleName>
Output directory

Here what <ModuleName> indicate is the SimpleApplication module itself.

The SimpleApplication module has been created alongside the project creation. You can see the list of modules in Project Structure window ( Shortcut : Command + ; ) as in the picture below.

Step2. Execute the compiled class file for test.

You can actually execute the class file using ‘java’ command.

of course, to use java command, you need it installed somewhere else like this. using “which java” lets you know where it is installed or symbolically linked.

Option2. Build a module or project.

The reason why I gave the option1 which is unrealistic in real world is because I am sure that every single problem that looks bigger than what it is is because we mostly tend to ignore the smallest unit of problem like option1.

In reality, we are supposed to be building entire project so that we can build an artifact from the hundreds, thousands of java files.

To test building entire classes in the target directory, I have added another class name Util and referenced it in the Main class’s method like this.

and I have completed a simple unit test by running the main class like this.

Then press the Build Project button to compile the entire target classes from Build menu’s submenus.

Then, you can see the compiled output in the output directory as below.

And even executing “java Main” resulted in printing message as intended.

Option3. Generate a JAR file.

Don’t get pissed about option 1 & 2, because those options are necessary only for making solid understanding of making a JAR file.

Go to the project structure’s artifacts menu.

and then click ‘+’ and choose type JAR and select “Fro modules with dependencies’.

and press OK.

and press “OK” leaving options as they are.

and then from the main menu, click Build | Build Artifacts.

and choose action “Build”.

and the generated jar file will be residing in the following directory.

To run the jar file, click “Edit Configuration”.

and click “+” and choose JAR Application.

and browse directory to choose the Path to JAR and open it.

and down below, click “+” to add a action to execute before actually launching the application.

Choose “Build Artifacts” to make sure that every time we run the application, IntelliJ always builds a new artifact and launch the app.

and choose the jar file to execute.

Press the run button to execute.

and the result will be printed out in the terminal console.

What’s next?

Until now, we’ve created an application that prints out some messages. But is it enough to start working on real world project? No not really. As project goes bigger and bigger, getting lots of people to work together, we must need some other frameworks and tools to centrally manage state of codes and dependencies. So for the next article, I will be talking about how to manage the source codes in real world using Git.

TO BE CONTINUED…

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

김영석
김영석

Written by 김영석

I love problem solving and hate repetition of tedious tasks. I like automating, streamlining, optimizing, things.

Responses (1)

Write a response