Start Coding A Basic Java Program – PART 2

Home /

Table of Contents

You have to download java on your machine to be enabled to compile and run Java Applications. You may be wondering what downloading java actually means. We will keep the juicier bits for later. As of now, downloading “Java” means downloading the Java Development Kit, also known as the JDK. Among the many tools that this “JDK” contains, there are tools that you will be used to compile and run a Java Application. The OpenJDK project and Oracle officially distribute the JDK. As its name suggests Java Development Kit is used to develop Java Applications

There are some other elements you may have heard of, that are also called “Java”. One of them is JRE which stands for Java Runtime Environment. It is actually a subset of JDK. It is used to run an already compiled Java Application as it only contains the tools needed to run a Java Application. The java code can not be compiled by the tools provided in the JRE. OpenJDK or Oracle do not distribute this anymore.

There are other “Java”s called J2EE, Java EE, or Jakarta EE that you may also have heard about. All these acronyms refer to the Java Enterprise Edition. The tools and libraries (we will explain them later) provided by Java Enterprise Edition are used to create enterprise-class applications. This is different from the JDK and is outside the scope of this course.

How Do I Download And Run This JDK?

You can download the JDK from different places but there is a one-stop page that always refers to the latest version of JDK.

image 16 - Start Coding A Basic Java Program - PART 2

 

Now you will see an option "Ready for use: JDK{version}". Click here.
image 17 - Start Coding A Basic Java Program - PART 2
It will redirect you to the page where you can download the version of the JDK you need.
3VVcfVJ7C X3uzoI3SRkDVfPlh375eRzCMLtoHudQNNiqFLVGBth9XwgV B4wAV3jvzM9Vsg1oGo7LMxYHg9V 5yoOExwFnauw4aMhaH0mEoXj - Start Coding A Basic Java Program - PART 2
There are four versions in this page for you to download:

           Linux/AArch64

           Linux/x64

           macOS/AArch64

macOS/x64

           Windows/x64

This page provides production-ready open-source builds of the Java Development Kit, an implementation of the Java SE Platform under the GNU General Public License, version 2, with the Classpath Exception.

As we have already told you, we are using a windows machine to develop this course. so we will be downloading the Windows version. Click on zip (sha256).

8 XLiLiXiyUGPCw XaQZlBVrNY92UchymtAn0TNLwUroXuP5x2kGYbAiUQoitSGZnq0xnCUvzzoiFUoxq0tAA362ksOfA721Z8zaBdCYtXZdFbgFaMJnzGyZp7pvAAXpjLyEWEQOmSLAw P1aQ - Start Coding A Basic Java Program - PART 2

You will be downloading a zip file of size about 180MB. Wait for the download to be completed. You can open this with any ZIP utility software. This zip file contains the JDK. You can unzip the contents of this file anywhere on your computer.

After extraction go to the extracted folder. It will be saved as "openjdk-{version}_windows-x64_bin" in the directory you choose. 
- Start Coding A Basic Java Program - PART 2
Double click on it to access the file. There will be a folder named "jdk-{version}"
u4FszLiXYthsV9HZfsiSunFCzUK34NO9WRROOvuKEx2udJYvHAF0rf49KpexO FyB4hw4qwbiBJBMSM1vH4Ea8GVVeoJwGAKcA60p 2zjzVjrDWbATf Jr12c4wurDGcoaH2w8liFFg9 ofJFw - Start Coding A Basic Java Program - PART 2
Open this folder. Find the bin folder.
- Start Coding A Basic Java Program - PART 2
Open the bin folder. There are two tools there : java and javac among hundreds of other tools. 
aViob67DPGH lwUeQTmRYhi6crqCEn6x02fvaIA2YD lT4eOz7rDBd IZYxUfF0TByT60aqOXxPaKOlzys JriGQAjOZDBptkhBjM3JxDWuc02Xhr7w8m12gkVE - Start Coding A Basic Java Program - PART 2

We talked about these tools earlier. javac is needed to compile the Java Source Code and java is needed to run the Java Application.

Your computer needs to know about the existence of these two tools to use. You can do this by updating the PATH variable of your system to add the bin directory of your JDK directory to it.

You need to be very cautious while setting the PATH variable because a single mistake like an added white space or a missing semicolon will result in failure.

To check whether the path variable is successfully updated to add the bin directory of the JDK directory open the command prompt. Type java -version. If you have an output as the following the java is set in your machine to run Java Application.

java version "{version}"

Java(TM) SE Runtime Environment (build {version}})

Java HotSpot(TM) 64-Bit Server VM (build {version}, mixed mode)

If it doesn’t give an output like the above then follow the above process until you have an output like this.

To check if the compiler has been set up properly you need to type javac in the command prompt. If the output is as the following then you are all set and ready to compile the Java Source Code and Generate the byte code to run the Application.

9lW63s0tRyqOUKaAF4Yrc49 1EMf BUKdTjIpLnnTDgRQSBjv2BiANmvLFzEqqigHKMJjaA55aMVI7q0N6GggrqYKK0rMqljqPXOz4Y91eNx6LtT 7Y P7yFJH3Xwtp hZR73yUzj8TzDtsYRQ - Start Coding A Basic Java Program - PART 2

If it doesn’t match the above then follow the previous steps again until you get this output.

One important thing to keep in mind:

You have to check if your java version and javac(compiler) version are the same or you may not run the Application. This happens due to version incompatibility.

If they match then alright. You are all set to compile and run the Java Application. Otherwise, follow the steps mentioned above again.

It is very unlikely to be a version mismatch of java and javac while you are directly downloading the JDK..
Once you have properly set your JDK and PATH variable, you are ready to compile your HelloWorld program.

How To Compile And Run A Java Program?

The remaining instructions are the same whether you are using Windows, Linux, or macOS.

        1. Open the command prompt again.

        2. Change to the directory where you have saved the HelloWorld.java.

        3. You are all set to compile HelloWorld.java. 

sHlj bGe37G6vrQdW9eCJAsFQfbG7rb1LfZB5YVD ODgLZsApMpI7P fSNsIirhvcZaqcaoJK7u rXJ ZkvZCB1RpgAR0AWJrP9etXwYvHOly9ITeCeYAXEhuNrEYamXLhw - Start Coding A Basic Java Program - PART 2

Type the following:

            javac HelloWorld.java
SjL wbEb1tyJzjuXKq3Gpp AuHkhfmuBo4nX335CbgSVMEg0D6Bs6oz 8yJdTO1FdkM MkE D7rvQxxiS9F0i6sAqYPbVPMZozUUWkCunDH9WmEHqE KxdDD4l1pUf5Xc9sTtL1GX 8jue726w - Start Coding A Basic Java Program - PART 2
At this point two things may happen. Your code may have some errors(in this case it won't happen) and due to this your compiler can not compile your code. The compiler will give you error messages. You need to fix the errors for being able to compile the source code and generate byte/executable code.

Or your compiler remains silent and doesn't complain about anything.

     

LGjcY4AqLlfj664EJ0gWue HiJWZUqLN75VI6D DFmFjvU jKumY4R3EIFPVDnNr9SWZtUUC36QLiHer GtWACoUSAJ7ACYMffXtdS260Dgi 3KsixFIvsrWvvFq6xg 7TjtPh5LhjNJWKdBPA - Start Coding A Basic Java Program - PART 2

Congratulations! You did it! You have generated the byte/executable code from the source code using the java compiler. Your java code has now been properly compiled. You can check this by checking in the directory where HelloWorld.java is saved. You will find a new file there in the directory: HelloWorld.class. 

IhZdaRgYB7 8uv1plFzyMJHma6kO1dvMXrAXrPHx3gTPhHdFkLsz5K8k1kbkWzWxxLElLh6uHU1rvTRuILQVErKqQvyFXRE6blQIe32bKxO xdrB u3LDQV6Ydk85fi - Start Coding A Basic Java Program - PART 2

This is the compiled file containing the byte code/executable code. Make sure that the compiler created the HelloWorld.class for you.

        To run the application all you have to do is type the following command in the opened command prompt:

java HelloWorld

This should print Hello, World! on the console.

di2c4JXZBvawsnpykbKZ2Z0JeUVIBJGR8cibVNaZDnmd5M Eq6LcaG7JZXwMCF AEzHbSjMABV8APucbqzRZvSm4y4 - Start Coding A Basic Java Program - PART 2

If this is the case: Congratulations. You just run your first Java Application. If it didn’t check the source code and make sure you have typed everything accordingly.

The first Java Application that you wrote, compiled, and ran showed you the basic steps every Java Developer follows to run an application.

1.Create(Type) source code in a .java text file or in a set of .java text files for creating large,complex Java Applications.

2.Compile this(these) file to produce a(or a set of) corresponding .class file(s) called binary file(s) containing byte code/executable code that machine understands and can execute(We will talk about it in detail later. There are some gotchas here in Java).

3. Run it(or them together) as an application.

Why Aren’t We Using IDEs?

You may or may not have heard about Integrated Development Environments or IDE. Developers that work on large applications use them instead of plain text editors to manage their source code. IDEs are complex software applications. They specialize in software development.  These applications handle the compilation of your source code automatically, they can help you to track errors in the syntax of your Java code and nail down bugs in its execution, among other things.

At the beginner level or at least until the end of this course we strongly advise you to not use them. Use plain Text Editor, Compile using javac in the command prompt, and run them as a console application. IDE can protect you from many small details that go under the hood to create a Java Application. We don’t want that. We want you to get bruised. This way you can become a seasoned Java Developer. Knowing all the little details will help you in the long run.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.

Other Recommended Article