Day 2
Day 2
If Java is installed and the PATH is configured correctly, you will see output as below:
NOTE: The exact messages will vary depending on the operating system used and the Java
version installed.
Additionally, you can also look for installed versions of Java (if it is installed) in Control Panel ->
Programs ->> Programs and Features
Features:
If Java is NOT present on your system, then follow below steps to install Java:
https://www.oracle.com/java/technologies/downloads/
If you want to download latest version of Java, then click on ““Java downloads”” tab (you will be on this
tab by default)
If you want to download a previous version of Java, then click on the ““Java archive”” tab.
Currently the latest version is JDK 21. Hence select JDK 21, then select your Operating System (my OS is
Windows so I have selected it as shown in below diagram) and then click on Download link present in
front of x64 Installer. Refer below screenshot:
Once you click on Download link, installation will start as shown below:
Wait for installation to complete. Once installation is complete, click on the downloaded exe file directly
or go to the downloads (default download path can be different on your computer) and double click on
“EXE” (jdk-21_windows-x64_bin.exe).
x64_bin.exe).
The Java Setup window w will open as shown below. Click on Next button present on the window and
complete the installation process:
Once you install Java on your computer, the next step is to set “PATH” and “JAVA_HOME” variables.
The JAVA_HOME environment variable points to the JDK installation directory. Other Java-dependent
programs (Apache Tomcat and other Java EE application servers, build tools such as Maven or Gradle)
can use this variable to access the JDK/JRE path.
The operating system uses the PATH environment variable to find the native executable programs (.exe
files) to run.
Once we point the executable program’s directory in PATH, we can invoke these executable programs in
the command-line without the need to specify its full path.
For Java programs to run, we need to set the PATH variable pointing to the JDK installation directory
along with the bin directory. The bin directory holds the Java executables.
As a best practice, setting both the JAVA_HOME and PATH environment variables is always
recommended. This way, we can remove the compatibility issues between using old and new programs.
When we try to run a Java program from the command-line, the operating system looks into the PATH
variable to locate and run the JVM. If there’s no JDK on the PATH variable, the command terminates
with an error:
C:\Users\rohit>java HelloWorld
Another common issue is having different JDK versions in the PATH variable. In such a case, the
operating system considers the first occurrence of JDK in the PATH variable for execution. However, it’s
better to remove multiple versions in the PATH variable to avoid confusion at a later stage
Depending on the Windows Operating System version on your computer, you can follow below steps to
open Environment Variable window :
Windows 11:
● Right-click
click on the Windows Start button and select System.
● In the System window, click on Advanced System Settings on the left sidebar.
● In the System Properties window (Advanced tab):
○ Click the Environment Variables button.
Windows 7:
Set JAVA_HOME:
In the User Variables section, see if JAVA_HOME is already present or not. If NOT present then click on
the New button but if JAVA_HOME is present then select this entry and click on Edit button.
Then, set Variable name as JAVA_HOME and Variable value as path of JDK installation directory.
directory Refer
above screenshot for details.
Set PATH:
In the User Variables section, see if PATH is already present or not. If NOT present then click on the New
button but if PATH is present then select this entry and click on Edit button.
If PATH is already there then check if an entry of the old version of Java is present there or not. If
present then remove this entry.
Click on OK button of User Variable and then click on OK button of Environment Variables window
Now,
ow, you can also check if JAVA_HOME is correctly set or not by below command in command prompt:
echo %JAVA_HOME%
NOTE: % used before and after JAVA_HOME is to treat JAVA_HOME as a variable (environment
variable)
System.out.println(
.println("Hello Java");
NOTE: If no access specifier is mentioned then default access specifier will be used. That means
if we do not mention “public” in class name then tha class will use “default” access specifier.
You can run your program if the main class is using the default acc
access
ess specifier but if you use
“private” for your main class then your program will not be executed.
1. First, you need to go to the directory where the Hello.java program is present. To do so, use
“cd” command as below:
cd “C:\RohitData\CODESY
CODESY\Core Java Batches\Programs”
javac Hello.java
If there is no syntax
tax error, then the program will get successfully compiled. As a result of this
compilation, .class file (Hello.class) will be created at the same path where Hello.java file is
present. This is a ByteCode file generated by Java Compiler.
Once you compile your program, you can execute it using Java Interpreter. Follow the instructions
below:
java Hello
Your Java program will get executed and you will see output as below:
javah - C header and stub generator. Used tto write native methods.
Download Eclipse:
4. After clicking Download, wait for some time (max 1 or 2 minutes) for the Save As
window to appear.
Note: If Save As window is not displayed in 1 or 2 minutes, click on ““click here link as
click here”
below
5. When Save As screen appears, select path where you want your Eclipse to be present
and click Save button
6. Once download is completed, you can see the ".zip” file of eclipse present at your
selected location. Right click on this “.zip” file and select Extract All button
7. Select path where you want your unzipped eclipse package files (you can keep default
path as it is) and click Extract button
8. Once extraction completes, you can go inside that folders and see eclipse.exe file
9. Double click on eclipse.exe file. Eclipse IDE Launcher will display. Select Workspace and click
the Launch button.
Note: To create a new workspace, click the “Browse” button and select the folder where you
want the workspace to be created. If you need to create a new folder, right click and New ->
Folder (Give name to that folder) and go inside that folder. Then click the Select Folder button.
11. For creating normal Java Project, Click File -> New -> Java Project