Creating a jar file in IntelliJ was a lot more of a hassle than it is supposed to be. Regardless, we released a Youtube video to guide one through the process easy-peasy-lemon-squeezy, by Peter, the CTO. You can view the video https://youtu.be/1OoIf5nR3kk .

Create the Jar file

Create an empty Java project in IntelliJ and add the following code in a class called executejar

import java.util.Scanner;

public class executejar {
    public static void main(String[] args) {
        System.out.println("Please enter your name: ");
        Scanner kb = new Scanner(System.in);
        String name = kb.nextLine();
        System.out.println("Your name is: " + name);
    }
}
Go to File tab and select Project Structure
Select File>Project Structure> Artifacts and then the plus sign and Add JAR.
Select modules with dependencies.
Input the name of the Java main class and press OK.

Then on the following screen just Apply the changes and press OK.

Hover over Build>Build Artifacts and select build artifacts to create the Jar file.
The jar file will then appear in the out directory or wherever you saved it.
Right click on the executable jar file and select Copy Path Reference and copy the Absolute Path File.

Running the Executible Jar file in the terminal

Open Command Prompt by searching for CMD

Enter the command after e.g. C:\Users\Dev> java -jar path/to/the/jar and press enter

In our case, this is where the jar file is found, press enter and you are good to go.

CONGRATULATIONS! You are now able to run Java Jar files from the terminal and keep your super secret code safe and a hidden mystery.

Leave a Reply

Your email address will not be published.