Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconFull Stack Developmentbreadcumb forward arrow iconHow to Code, Compile and Run Java Projects [2024]

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

Last updated:
6th Oct, 2022
Views
Read Time
9 Mins
share image icon
In this article
Chevron in toc
View All
How to Code, Compile and Run Java Projects [2024]

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.

Ads of upGrad blog

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? 

Check out our free courses to get an edge over the competition.

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.

Explore our Popular Software Engineering Courses

Check out upGrad’s Advanced Certification in DevOps

Read: Java Developer Salary in India

upGrad’s Exclusive Software Development Webinar for you –

SAAS Business – What is So Different?

 

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. 

Check out upGrad’s Advanced Certification in Blockchain

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. 

Explore Our Software Development Free Courses

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.  

In-Demand Software Development Skills

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.

Read our Popular Articles related to Software Development

Ads of upGrad blog

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

Learn Software Engineering Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Wrapping up

If you’re interested to learn more about Java, full-stack software development, check out upGrad & IIIT-B’s Executive PG Program 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.

Profile

Rohan Vats

Blog Author
Software Engineering Manager @ upGrad. Passionate about building large scale web apps with delightful experiences. In pursuit of transforming engineers into leaders.

Frequently Asked Questions (FAQs)

1How does JVM works?

The Java Virtual Machine is a software implementation of the Java Platform, Standard Edition. The Java Virtual Machine loads and executes Java bytecode. The Java Virtual Machine is conceptually similar to Microsoft's Common Language Runtime or the virtual machine used in the Python language. The Java Virtual Machine is a purely software implementation that executes Java bytecode in a manner comparable to how a hardware-based virtual machine would interpret and execute instructions of an abstract machine. The Java Virtual Machine performs the language-related and system-related tasks of the Java Platform, Standard Edition.

2What is the difference between JDK and JVM?

The Java platform is a software stack that consists of the Java language and class libraries including the virtual machine, runtime and the standard class libraries. The Java virtual machine (JVM) is one implementation of the Java platform. The Java Development Kit (JDK) includes the JVM and the Java language engine. The Java Runtime Environment (JRE) is the Java virtual machine without the Java language engine. The JVM is a software implementation of the Java virtual machine specification which is used to interpret bytecodes within applications written in the Java programming language. The Java runtime environment is responsible for running the applications written in the Java programming language.

3What are Java jars?

Explore Free Courses

Suggested Blogs

How to Rename Column Name in SQL
46652
Introduction We are surrounded by Data. We used to store information on paper in enormous file organizers. But eventually, we have come to store it o
Read More

by Rohan Vats

04 Mar 2024

Android Developer Salary in India in 2024 [For Freshers & Experienced]
900973
Wondering what is the range of Android Developer Salary in India? Software engineering is one of the most sought after courses in India. It is a reno
Read More

by Rohan Vats

04 Mar 2024

7 Top Django Projects on Github [For Beginners & Experienced]
51018
One of the best ways to learn a skill is to use it, and what better way to do this than to work on projects? So in this article, we’re sharing t
Read More

by Rohan Vats

04 Mar 2024

Salesforce Developer Salary in India in 2024 [For Freshers & Experienced]
908395
Wondering what is the range of salesforce salary in India? Businesses thrive because of customers. It does not matter whether the operations are B2B
Read More

by Rohan Vats

04 Mar 2024

15 Must-Know Spring MVC Interview Questions
34526
Spring has become one of the most used Java frameworks for the development of web-applications. All the new Java applications are by default using Spr
Read More

by Arjun Mathur

04 Mar 2024

Front End Developer Salary in India in 2023 [For Freshers & Experienced]
902195
Wondering what is the range of front end developer salary in India? Do you know what front end developers do and the salary they earn? Do you know wh
Read More

by Rohan Vats

04 Mar 2024

Method Overloading in Java [With Examples]
25565
Java is a versatile language that follows the concepts of Object-Oriented Programming. Many features of object-oriented programming make the code modu
Read More

by Rohan Vats

27 Feb 2024

50 Most Asked Javascript Interview Questions & Answers [2024]
3846
Javascript Interview Question and Answers In this article, we have compiled the most frequently asked JavaScript Interview Questions. These questions
Read More

by Kechit Goyal

26 Feb 2024

OOP Concepts and Examples That Every Programmer Should Know
25126
In this article, we will cover the basic concepts around Object-Oriented Programming and discuss the commonly used terms: Abstraction, Encapsulation,
Read More

by Rohan Vats

26 Feb 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon