After knowing the brief history of Java, and understanding its importance and applications. Let us learn how to setup Java in your own system, to build and run Java Programs. Some PCs might have Java installed already. You can check whether Java is installed through Command Prompt(cmd.exe) using the command 'java -version'. On its execution, it will display the version of Java installed on your System. If it is not installed in your System already. Then follow our guide and start your career in Java Programming.
Setting-up Java
- Go to oracle.com And download the latest version of JDK(Java Development Kit) available.
-
Now click on 'New' Button and add the path where Java is installed in your system.
-
And then paste this path, in the edit environment variable window in Step 4. And then press OK.
And Java is installed in your system, Then check the installation by opening
cmd.exe and typing 'java -version'.
And on getting this output, after executing the above command. It means Java
is installed successfully on your System.
Selecting an IDE for Java
Integrated Development Environment(IDE) is a software for building
applications or programs that combines common developer tools into a single
Graphical User Interface(GUI). These are very much helpful in increasing the
productivity of the programmer.
I would prefer you to go with Eclipse-IDE. It is one of the best IDEs for
Java and other programming languages. You can also go with VSCode, Visual
Studios, IntelliJ and other IDEs as well, it is upto you.
Download Eclipse IDE for Java Developers from
eclipse.org.
And install the program after being downloaded.
And your Java Set-up is finally ready✌, Lets write our very First program in
our Eclipse IDE.
Java Quick-Start
- Open Eclipse IDE.
- It will ask you to set a directory for saving all the programs you are going to make in Eclipse. Select any directory of your wish. Then click Launch button
-
Then after creating a package, double-click on the package name in the Package Explorer Window. And and right click on the src folder and select 'Class'. This will create a new Java class for you.
-
Then start writing your code there.
For the time being, this is your first step in the world of Java
Programming, now we'll write a simple Hello World program in Eclipse. And
compile and run it.
Write the following code in the code panel.
class Main
{
public static void main(String args[])
{
System.out.println("Hello World");
}
}
* We'll learn
about every aspect of the code in the later tutorials, but for now it is
nothing but a piece of text to be copied and pasted in the IDE.
And then click the run button as shown in the image below.
Congratulations!🎉, finally you compiled and ran your first
Java Program👍.
Compiling and Running Programs through CMD
Yes, you can compile and run programs through
cmd.exe
also. But for this also you need java-jdk installed in your system(Discussed in the Upper section of post). For doing this, write a code in
simple notepad file, and save it with a extension
.java. Also remember the location, where you are saving the file.
- Copy the above Hello world code in notepad file, saving it with the name hello.java
- Open cmd.exe
- Navigate to the folder where you have created the file, using the 'cd'(change directory) command. And then list all the items on reaching the desired folder using 'dir' command.
- Then execute the command 'javac filename.java', this command will compile the code and create a new file with the same file name but with .class extension, Only if the code is compiled without any errors. This file created is called the Byte Code.
- Now again go to cmd and execute 'java filename', this will run your compiled code. And output would be printed the command prompt itself.