Create a java project in 3 ways | Tips from an expert

Tech Discover > All Articles  > Create a java project in 3 ways | Tips from an expert
How to Create a Java Project

Create a java project in 3 ways | Tips from an expert

In this article, we are going to talk about how to create a java program in three different ways and after that, you can decide which of them suits you better. We’ll show you how to create a java project without IDE and execute it from the command line, create a java project from an IDE and generate Java source files from maven.

 

1. Prerequisites

      • Java JDK
      • IntelliJ IDEA
      • Maven

 

To create and run a java project, you are going to use Java JDK and an IDE which will help you to edit your java classes easily. If you don’t have them installed, please follow How to install Java JDK article or watch How to install IntelliJ IDEA IDE

 

2. Create a java program and execute it from the command line

 

First of all, open a command prompt and navigate to the folder where you want to create the project.

Create a new folder, let’s say “HelloWorld” by typing the command of creating a new directory:

 

mkdir HelloWorld

 

Browse to the HelloWorld folder

 

cd HelloWorld

 

Create the java file

 

echo "" > HelloWorld.java

 

Open the HelloWorld.java file in any text editor and provide a sample java code.

 

I am going to choose a basic java program which will print the “Hello World” string to the console.

 

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

Compile it

 

Compile the java class by using the standard Java Compiler which creates bytecode for Java Virtual Machine from java code.  javac HelloWorld.java

 

Run it

 

Finally, to run the java class that we just created, type java HelloWorld.java in the cmd terminal.

 

3. Create a java project from IDE

 

For creating a new java project, first of all, you open your IDE and follow the next steps.

 

Click on Create New Project

 

Create new Java Project

Select the project type

 

On this step, you will need to choose the type of your project. IntelliJ Idea provides several project type, such as Java, Maven, Gradle and so and so forth, but we will go further with Maven. Make sure that you didn’t click on “Create from archetype” checkbox button, otherwise, you will create a project which will extend the selected archetype.

 

Create a Maven Java Project

Add a few words about the project

 

On this step, you will need to provide some details such as GroupId, ArtifactId and Version. If you didn’t work with maven before, click here to read about Maven Naming Convention Guide

 

Fill the GroupId, ArtifactId and Version Maven properties

Create the Java Class

 

Now, the java project is ready to be filled with nice java code. To create your first java class, right-click on the java package, click on New and then select Java Class and give a proper name to it.

 

Create a new Java Class and add some java code to it

 

Compile and Run it

 

To run your first Java project you need to click on the play button, which is next to the java class name, as the next image is describing it.

 

Run the Java Class from IntelliJ Idea

 

If your code is successfully compiled by the JDK, you should have the “Hello World” string printed in the Run terminal as below:

 

Check the output in the console

 

4. Generate a java project with Maven

 

Maven offers the possibility to generate a java project by simply running a maven goal. To do that, you just need to provide the groupId, artifactId and the archetypeArtifactId. The last one is the parameter by which you pass the artifactId of the maven template project. In this example, we are going to use the maven-archetype-quickstart.
For executing the maven goal, you need to pass the parameter values like this:

 

mvn archetype:generate -DgroupId=net.techdiscover -DartifactId=java-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

 

and copy-paste them into a command prompt window which is opened on the path where you want to put the java project.

The output of the maven goal should be like this:

 

Run the Maven archetype from command prompt

 

What is interactiveMode?

 

This property has two possible values: true or false. If you want to generate the project without headaches, you can provide the properties values like above, or you can execute the maven goal without overriding any of those properties, and maven will ask you to provide them.

For a good experience, you can try to execute the following goal to see what is happening:

 

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart

 

And the output is:
Run the Maven archetype without overriding the properties

 

Overriding the properties

 

You probably also asking yourself why the groupId, artifactId, archetypeArtifactId and interactiveMode has -D characters in front of them. Those characters are there because this is the way to tell the goal that you want to override those properties.

 

Conclusion

 

As you saw in this post, there are several ways to create a simple java project, it depends on your programmer style, some of you are thrilled to write the commands by themselves end execute them in command prompt window, despite on doing some clicks on the IDE. If you feel that this article was helpful to you, don’t hesitate to share or like this post on social media.
See you in the next article. Happy coding until then!

No Comments

Post a Comment