How to Code, Compile and Run Java Projects [2020]

Java is one of the most used programming languages in the software development industry. If you are planning to start this language, the article will help you in understanding how to develop and run programs using this object-oriented programming language. The article discussed different syntax nuances and some functionalities that will serve as a prerequisite while discussing different ways of how to run a java project

Before we dive into the technical details, we need to understand the program structure in a Java project. How and which files are necessary. The first thing to note is that every java project is required to have a class in the main java file. That is then converted into an intermediate code language. Then this intermediate code is used to get the interpreter to interpret the machine code and give the desired output. So, to put it simply, this is how it works:

1. Code the Java files.

2. Compilation of the Java files.

3. The output of the compiler is the intermediate code .class files.

4. Interpretation of the intermediate code.

5. Output generation.

As you know, Java is an object-oriented programming (OOP) language. It means everything written is in the form of objects. But why is this important? While understanding how to run a java project, it is crucial to know why every project has a .class file? 

A class is the basic building block in any java program. Everything is written in the form of a class. The file is then saved as .java and used to follow along with the different procedures. The class is like a blueprint of what needs to be done. Once it is defined, it needs to be instantiated by making an object of the said class. The object is hence called the instance of a class. The object is then used to get the program running. 

Because of the intermediate code generation, Java is also platform-independent. With the ability to run on any system, combined with object-oriented programming, makes Java so compatible with the industry-standard codes for software developments. And because it is pre-compiled, once the program files are converted into the .class files, Java only requires it to be interpreted on the desired machine. It makes Java pretty quick. 

So now that we know the basics of language usage and what is necessary, let us start with a simple program, to know the actual procedure of how to run a java project. We all know that the first program to write in any language needs to be a Hello World program. So, let us write one. 

Here is the code to output a line “Hello World” on the console

public class TestFirst {

  public static void main(String[] args) {

    System.out.println(“Hello World!”);

    System.out.println(“I just wrote my first program in Java”);

  } //End of main

} //End of TestFirst Class

Now let us first learn to run this program, then we will break the program down to its very core for clear understanding. 

First, we need to set up the Java environment. Here are the steps to successfully get Java on your computer. 

· Download and Install Java.

· Setup the environment variables.

· Verify the steps in command prompt or terminal (if macOS X).

So, for downloading Java, visit this site, and download the installer(s) from there. 

Once done with the basic installation steps, you need to set up the environment variables. It is to tell the computer that we have Java installed and where to look for it while compiling or running Java programs. It is a crucial step for the machine to know how to run a java project.

Read: Java Developer Salary in India

Setting up environment variables or path in Windows

· Simply open up the command prompt by searching it on the windows or by typing “cmd” in the Run program. Once the command prompt is open, you need to type the following:

set path=C:\Program Files\Java\jdk1.8.0_201\bin

· Note that the path of yours may be different, depending on the version you are installing and where the installation is done. So set the path accordingly. 

Setting up the JDK path in Mac OS X:

· In Mac OS X, open the terminal and enter the following command:

export JAVA_HOME=/Library/Java/Home

· Once done, you may verify the path by typing echo $JAVA_HOME in the terminal and see if the path exists or not. 

Once the environment is set up, you can verify by opening the command prompt and typing java or javac. It is just a step to see if the command prompt recognizes the java JDK or not. 

Now that the environment is set up and Java is up and running let us get started with the final steps on how to run a java project. Let us compile and run our hello world program. 

Just open up any text editor of your choice and type in the program mentioned above. Once done, save the file with a .java extension. For example, myfirstprogram.java. 

Now, wherever the file is stored, open the command prompt there or change the directory to that location, or else you would need to enter the full path. The next step is to type in the following command:

· javac myfirstprogram.java 

Note that the command is written with the consideration that the java file is located in the current working directory. If it is not the case, you would need to enter the full path. 

Next, if there are no errors, the java compiler should have compiled a .class file at the same location. In case, in future, if there are any errors or warnings, the compiler will list out the stack trace of the same in the command prompt itself. The .class file would only be generated or updated when the program is free of any errors. 

Here, you would notice one thing, especially if you have saved the file in a different name than the class name in the program file. In our mentioned case, the class file generated is named: TestFirst.class. It is important to note that Java generates the class files based on the class name specified in the program file. As the class mentioned was TestFirst, the class file has the same name.  

Once the .class files are generated, we only need to use java’s interpreter to comb through the intermediate code and produce the desired output. To do so, enter the following command:

· java TestFirst

Note that we are not having any kind of extension following the named TestFirst. It is important to remember that while running the class files, there is no need to type in the extension of the class file. 

If everything runs well, you should be able to see two lines printed on the console.

· Hello World 

· I just wrote my first program in Java

First of all, congratulations on compiling and running your first Java program. You now understand how to run a java project or any program. Now that we know the process let us understand how it all works. 

The first line is public class TestFirst. As discussed earlier, every Java program must have a class definition. Here we have used a public access modifier. Now what is an access modifier and how they work is out of the scope of this topic. Right now, you only need to know that any Java file can have multiple class definitions, but only one public class.  

The next line is the public static void main (String args[]). The public is the access modifier; hence we can call the method outside the class definition. Static means it does not need an object to be called from. Static methods can work independently. Void means it does not return anything. The keyword preceding the function name is the return type. Main is the method name. It is the entry point for any java virtual machine. And finally, the arguments of the main method. String args[] is used to take in any argument while running the program, from the user (console). 

The last part is the print command. System.out.println(“Hello World!”);

The first thing to note is that every Java code line needs to end with a semicolon (;). In Java, an import or use of a package can be done via dots (.). Hence the line System.out.println essentially means that we are importing the function named “println” from a package “out”, which is imported from a package named System. Now, as these packages are pre-defined in the JDK, we can directly use them. Println takes in a simple string argument. The “ln” at the end means a new line is created. 

It is important to close the method and class with a closing curly brace ({}). Note that in Java, the class definitions or method definitions or any of the loops or conditions, are written in blocks specified by curly braces. It is how we can keep track of where a certain block starts and ends. It also helps in identifying the variables used in a block. 

So, this is how we can install, setup, write, compile, and run any Java program. To run a pre-written Java project, we simply need to follow the compilation steps and produce the class files. Then run it via the Java command. Now you have enough knowledge to explore programming in Java, as you now possess the knowledge and system in place to run any Java program or project. This is all about how to run a java project.

Check out: 17 Interesting Java Project Ideas & Topics For Beginners

Wrapping up

If you’re interested to learn more about Java, full-stack software development, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

Land on Your Dream Job

UPGRAD AND IIIT-BANGALORE'S PG DIPLOMA IN SOFTWARE DEVELOPMENT
Learn More

Leave a comment

Your email address will not be published. Required fields are marked *

×