Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

How to Code, Compile and Run Java Projects

Updated on 22 November, 2022

10.76K+ views
9 min read

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? 

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.

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.

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

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.

Frequently Asked Questions (FAQs)

1. How 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.

2. What 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.

RELATED PROGRAMS