Steps to Prepare First Java Application:
- Download and Install Java Software
- Download And Install Java Editor
- Open Java File and Write Java program
- Save Java File
- Compile Java File
- Execute Java Program
1 Download and Install Java Software
Download the Java software from the following URLs:
JAVA6: https://www.oracle.com/in/java/technologies/javase-java-archive-javase6-downloads.html
JAVA7: https://www.oracle.com/in/java/technologies/javase/javase7-archive-downloads.html
JAVA8: https://www.oracle.com/in/java/technologies/javase/javase8-archive-downloads.html
To install Java Software on our computer, use the following steps :
- Double click on the “jdk-6u45-windows-x64.exe” file.
- Click on the “Next” button.
- Change the JDK installation location to “C:\Java\jdk1.6.0_45” by clicking on the “Change…” button.
- Click on the “Next” button.
- Change the JRE installation location to “C:\Java\jre6” by clicking on the “Change….” button.
- Click on the “Next” button.
- Click on the “Close” button.
Note: Similarly, install JAVA 7 and JAVA 8 versions.
After installation of the Java software, if we want to use all java commands in the command prompt then we have to set the “path” environment variable to the location where all java commands exist, that is :
“C:\java\jdk1.8.0_202\bin”.
There are two ways to set the “path” environment variable:
- Temporary Setup.
- Permanent Setup.
Temporary Setup:
To set the “path” environment variable temporarily we have to use the following command in the command prompt.
set path=C:\java\jdk1.8.0_202\bin;
If we set the “path” environment variable temporarily in a command prompt then that path environment variable is applicable for the present command prompt only, it is not applicable for all the command prompts, that is , in every command prompt we must set the path environment variable explicitly, it is not suggestible.
Permanent Setup:
In this approach, we will set the “path” environment variable in system settings one time and it will be available to all the command prompts, no need to set the path environment variable every command prompt, this approach is a suggestible approach. To set the “path” environment variable permanently we have to use the following steps.
- Right Click on the “This PC”.
- Select “properties”.
- Click on the “Advanced System Settings” link.
- Select “Advanced” tab[Bydefault selected].
- Click on the “Environment variables….” button.
- Go to “User/System Variables Block”,
- If “path” exists already
Select “path”.
Click on the “Edit” button
Click on the “New” button.
Provide value “C:\java\jdk1.8.0_202\bin”.
Click on the”OK” button.
- If “path” does not exist.
Click on the “New” button.
Provide the following details.
Variable Name : path
Variable Value : C:\java\jdk1.8.0_202\bin
Click on the “OK” button.
7. Click on the “OK” button.
8. Click on the “OK” button.
Note: Open Multiple Command prompts and check for “javac” .
In Java applications, if we want to switch java from one version to another version then we have to use batch files.
- Take a text file and save it with “fileName.bat” .
- Provide “set path” command with the respective java version.
- In Command prompt, goto the location where batch files are saved and type batch file name then click on enter button.
D:\FullStackJava
java6.batset path=C:\java\jdk1.6.0_45\bin;
java7.batset path=C:\Java\jdk1.7.0_80\bin;
java8.batset path=C:\Java\jdk1.8.0_202\bin;
On Command prompt:
D:\FullStackJava>java6.bat
D:\FullStackJava>set path=C:\java\jdk1.6.0_45\bin;
D:\FullStackJava>javac -versionjavac 1.6.0_45
Q)Is it possible to install more than one java software in a single computer?
Yes, we can install more than one java software in a single computer but with the following conditions.
- All Java softwares must be of the same Operating system.
- All the java softwares must be from the different versions like java6, java7, java8,….
Q)If we install more than one java software in a single computer then which version will come to the command prompt to use?
If we install more than one java software in a single computer then the java software which we set in the “path” environment variable will come to the command prompt inorder to use java commands.
Q)If we install more than one java software in our computer and if we set more than one java software to the path environment variable then which java software will come to the command prompt in order to use?
If we install more than one java software in our computer and if we set more than one java software to the “path” environment variable then the java software which we provided as first one in the path environment variable that java version will come to the command prompt in order to use.
Q)In the computer, if we set “JAVA 8” version to the path environment variable permanently and “JAVA 6” version to the path environment variable temporarily then which java version will come to the command prompt?
If we set “java 8” version to the path environment variable permanently and “java 6” version to the path environment variable temporarily then the Java version which we set to the path environment variable temporarily that java version will come to the command prompt.
2 Download And Install Java Editor
Editor: Editor is a software , it is able to provide an environment to write java programs and to save java files in our computer.
EX: Notepad, Editplus, Notepad++, sublime text, atom, VS Code,….
Download Editplus from the URL:
https://www.editplus.com/download.html
To install Editplus we have to use the following steps.
- Double Click on the “epp570_4352_64bit.exe” file.
- Click on the “Accept” button.
- Click on the “Start Copy” button.
- Click on the “OK” button.
Note: Editors are suggestible up to the learning process, Editors are not suggestible for the real time application development, in the real time application development IDEs are suggestible.
IDE: Integrated Development Environment.
EX: Eclipse, IntelliJ IDEA, etc
3 Open Java File and Write “Hello World” Java program
After the installation of Editplus, we have to open a java file and we have to write java code.
File —> New —-> Java
To write a Java program we need the following elements.
- Main Class.
- main() method.
- In the main() method use
System.out.println()to display data on command prompt.
D:\FullStackJava\Test.java
public class Test
{
public static void main(String[] args)
{
System.out.println("Welcome To Java Programming");
}
}D:\FullStackJava>javac Test.java
D:\FullStackJava>java TestWelcome To Java Programming
D:\FullStackJava>
4 Save Java File
To save a java file we have to use the following format for the file name:
fileName.java
To save java files in the computer, we have to use the following rules and regulations.
- In the present java file if we have any public class / public abstract class / public interface and public enum then we must save that java file with a public element[class, abstract class, interface, enum] name only. If we violate this rule then the compiler will raise an error.
- If no element[class, abstract class, interface, enum] is public in the present java file then it is possible to save java file with any name like abc.java, xyz.java,… in this case , java has given a suggestion to save java file with main class name instead of using any other name like abc.java, xyz.java,….
EX:
D:\FullStackJava\abc.java
class Test{
public static void main(String[] args){
System.out.println("Welcome To Java Programming....");
}
}
D:\FullStackJava>javac abc.java
D:\FullStackJava>java Test
Welcome To Java Programming....
EX:
D:\FullStackJava\Test.java
class Test{
public static void main(String[] args){
System.out.println("Welcome To Java Programming....");
}
}
D:\FullStackJava>javac Test.java
D:\FullStackJava>java Test
Welcome To Java Programming....
EX:
D:\FullStackJava\Test.java
public class A{
}
class Test{
public static void main(String[] args){
System.out.println("Welcome To Java Programming....");
}
}
D:\FullStackJava>javac Test.java
Test.java:1: error: class A is public, should be declared in a file named A.java
public class A{
^
1 error
EX:
D:\FullStackJava\A.java
public class A{
}
class Test{
public static void main(String[] args){
System.out.println("Welcome To Java Programming....");
}
}
D:\FullStackJava>javac A.java
D:\FullStackJava>java Test
Welcome To Java Programming....
D:\FullStackJava>
Q) Is it possible to declare more than one public class in a single java file?
No, it is not possible to provide more than one public class in a single java file, because if we provide more than one class as public in a single java file then we must save that java file with more than one name , it is not possible in all the Operating systems.
Note: Every Java file allows at most one public element[Class / Abstract class / Interface / Enum].
EX:
D:\FullStackJava\A.java
public class A{
}
public class B{
}
class Test{
public static void main(String[] args){
System.out.println("Welcome To Java Programming....");
}
}
D:\FullStackJava>javac A.java
A.java:3: error: class B is public, should be declared in a file named B.java
public class B{
^
1 error
D:\FullStackJava>
5 Compile Java File
Q) What is the requirement to compile java files?
The main requirement to compile java files is:
- To translate the program from high level representations to the low level representations.
- To check developers’ mistakes we need compilation.
To perform compilation, JAVA has provided a predefined command in the form of “javac”.
javac FileName.java
EX:D:\FullStackJava>javac Test.java
When we use the above command on the command prompt, then the Operating System will perform the following actions:
The Operating System will take javac command from the command prompt and the Operating system will search for the javac command in its internal commands list and at the locations referred by the “path” environment variable.
If the javac command does not exist at both the locations then the Operating system will provide the following message on command prompt.
‘javac’ is not recognized as an internal or external command, operable program or batch file.
To make available all java commands to the Operating system we have to set the “path” environment variable to the location where all java commands exist.
set path=C:\Java\jdk1.8.0_202\bin;
When the Operating system identifies the “javac” command through the “path” environment variable , the Operating system will execute the javac command and the Operating SYstem will activate java compiler software.
When the Java compiler software is activated , the Java compiler will perform the following actions:
- Compiler will take the java file name from the command prompt.
- Compiler will search for the java file at the current location.
If the provided java file does not exist then the compiler will provide the following message.
javac: file not found: Test.java
If the provided java file exists at the current location then the compiler will start compilation from the starting point of the file to the ending point of the file.
In the compilation process, if any error is identified then the compiler will provide an error message on the command prompt.
If no error is identified then the compiler will generate .class files.
Note: Generating number of .class files is not at all depending on the number of java files which we compiled, it is completely depending on the number of classes, abstract classes, interfaces, enums and inner Classes.
D:\FullStackJava\Test.java
enum E{
}
interface I{
}
abstract class A{
}
class B{
class C{
}
}
class Test{
public static void main(String[] args){
System.out.println("Welcome To First Java Application");
}
}
D:\FullStackJava>javac Test.java
D:\FullStackJava>dir
Volume in drive D is Data
Volume Serial Number is 68E9-29EB
Directory of D:\FullStackJava
17-08-2023 09:54 <DIR> .
17-08-2023 09:54 <DIR> ..
17-08-2023 09:54 179 A.class
17-08-2023 09:54 270 B$C.class
17-08-2023 09:54 223 B.class
17-08-2023 09:54 611 E.class
17-08-2023 09:54 86 I.class
17-08-2023 09:54 435 Test.class
17-08-2023 09:54 201 Test.java
11 File(s) 53,574 bytes
2 Dir(s) 350,445,043,712 bytes free
D:\FullStackJava>
In Java applications, if we want to send the generated .class files to a particular location after the compilation then we have to use -d option along with the javac command.
EX:
D:\FullStackJava\Test.java
enum E{
}
interface I{
}
abstract class A{
}
class B{
class C{
}
}
class Test{
public static void main(String[] args){
System.out.println("Welcome to First Java Application");
}
}
D:\FullStackJava>javac -d E:\abc\xyz Test.java
D:\FullStackJava>e:
E:\>cd abc
E:\abc>cd xyz
E:\abc\xyz>dir
Volume in drive E is New Volume
Volume Serial Number is 7E96-6D63
Directory of E:\abc\xyz
14-07-2023 10:51 <DIR> .
14-07-2023 10:51 <DIR> ..
14-07-2023 10:51 179 A.class
14-07-2023 10:51 270 B$C.class
14-07-2023 10:51 223 B.class
14-07-2023 10:51 611 E.class
14-07-2023 10:51 86 I.class
14-07-2023 10:51 435 Test.class
6 File(s) 1,804 bytes
2 Dir(s) 284,862,418,944 bytes free
In Java applications, if we use a package statement in a java file and if we want to prepare folder structure w.r.t the package name then we have to use -d option along with javac command.
EX:
D:\FullStackJava\Test.java
package com.durgasoft.core;
enum E{
}
interface I{
}
abstract class A{
}
class B{
class C{
}
}
class Test{
public static void main(String[] args){
System.out.println("Welcome to First Java Application");
}
}
D:\FullStackJava>javac -d E:\abc\xyz Test.java
D:\FullStackJava>e:
E:\abc\xyz>cd com
E:\abc\xyz\com>cd durgasoft
E:\abc\xyz\com\durgasoft>cd core
E:\abc\xyz\com\durgasoft\core>dir
Volume in drive E is New Volume
Volume Serial Number is 7E96-6D63
Directory of E:\abc\xyz\com\durgasoft\core
14-07-2023 10:59 <DIR> .
14-07-2023 10:59 <DIR> ..
14-07-2023 10:59 198 A.class
14-07-2023 10:59 346 B$C.class
14-07-2023 10:59 261 B.class
14-07-2023 10:59 706 E.class
14-07-2023 10:59 105 I.class
14-07-2023 10:59 454 Test.class
6 File(s) 2,070 bytes
2 Dir(s) 284,862,418,944 bytes free
E:\abc\xyz\com\durgasoft\core>
Q) Is it possible to compile more than one java file by using a single javac command?
Yes, it is possible to compile more than one java file by using a single javac command with the following cases.
Case#1:
If we prepare multiple classes in multiple java files , if we use one class name in another class which is in a different java file and if we compile the first java file then the compiler will compile all java files which contain the dependent classes.(Here, in this case, classes are interdependent.)
EX:
D:\FullStackJava
A.java
public class A {
B b = new B();
}
B.java
public class B{
C c = new C();
}
C.java
public class C{
D d = new D();
}
D.java
public class D{
}
D:\FullStackJava>javac A.java
D:\FullStackJava>dir
Volume in drive D is Data
Volume Serial Number is 68E9-29EB
Directory of D:\FullStackJava
14-07-2023 11:14 <DIR> .
14-07-2023 11:14 <DIR> ..
14-07-2023 11:14 231 A.class
14-07-2023 11:11 38 A.java
14-07-2023 11:14 231 B.class
14-07-2023 11:11 37 B.java
14-07-2023 11:14 231 C.class
14-07-2023 11:12 37 C.java
14-07-2023 11:14 176 D.class
14-07-2023 11:11 23 D.java
14 File(s) 72,667 bytes
3 Dir(s) 347,551,588,352 bytes free
D:\FullStackJava>
Case#2:
If the classes are not interdependent, then we have to provide all the java file names with a space separator along with a single javac command.
EX:
D:\FullStackJava
A.java
public class A {
}
B.java
public class B{
}
C.java
public class C{
}
D.java
public class D{
}
D:\FullStackJava>javac A.java B.java C.java D.java
Case#3:
If we want to compile all Java files which are available at the current folder then we have to use ‘*’ notation in place of file names.
javac *.java
Case#4:
If we want to compile all java files which are started with Employee and which may end with anything then we have to use the following command.
D:\FullStackJava>javac Employee*.java
Case#5:
If we want to compile all java files which may start with anything but it must be ended with Address then we have to use the following command.
D:\FullStackJava>javac *Address.java
Case#6:
If we want to compile all java files which start with anything and which ends with anything but it contains Account then we have to use the following command.
D:\FullStackJava>javac *Account*.java
6 Execute Java Program
To execute Java applications we have to use the “java” command provided by the Java Software.
java MainClassName
EX:
D:\FullStackJava>java Test
If we use the above command on command prompt then the Operating System will perform the following actions:
- Operating System will take java command from command prompt, Operating System will search for java command in its internal commands list and at the locations referred by the “path” environment variable.
- The Operating System will identify java command at the location “
C:\Java\jdk1.8.0\bin” through the “path” environment variable, with this Operating System will execute java command , where JVM software will be activated and JVM will perform the following actions.
- JVM will take the main class name from the command prompt.
- JVM will search for the main class .class file at the current location, at the Java predefined library and at the locations referred to by the “classpath” environment variable.
- If the required main class .class file does not exist at all the above locations then JVM will provide the following exception messages.
JAVA6: java.lang.NoClassDefFoundError:TestJAVA7: Error: Could not find or load main class Test
If we know the location where the main class .class file exists then we have to provide the main class .class file location to the JVM, for this we have to set the “classpath” environment variable to the location where the main class .class file exists.
set classpath=E:\abc\xyz;
When JVM identifies the main class .class file location then JVM will load main class bytecode to the memory, this process is called Class Loading.
After loading the main class, JVM will search for the main() method, if the required main() method does not identified then JVM will provide the following exception messages.
JAVA6: java.lang.NoSuchMethodError: mainJAVA7: Error: Main method not found in class Test, please define the main method as:
public static void main(String[] args)
If the main() method exists in the main class then JVM will create a thread to execute main() method, here the thread which is created by JVM to execute main() method is called Main Thread.
Main Thread will execute the complete main() method, at the end of the application execution, automatically Main Thread will come to the Dead State.
When the Main Thread is in Dead state , JVM will stop all of its internal processes and JVM will go to shutdown mode.
Q) What is the difference between “path” and “classpath” environment variables?
“path” environment variable is able to provide the location details to the Operating System where all java commands exist, that is C:\Java\jdk1.8.0\bin.
set path=C:\Java\jdk1.8.0\bin;
“classpath” environment variable is able to provide .class files location details to the compiler and JVM where the required .class files exist.
set classpath=E:/abc;
Note: “path” environment variable value is fixed, so we can set it permanently, but classpath environment variable value is not fixed, it is varied from one application to another application, so set it temporarily.
JDK and JRE
Understanding the JDK (Java Development Kit) and JRE (Java Runtime Environment) is essential for Java application development. Here’s an explanation of each:
- JDK (Java Development Kit):
- Definition: JDK is a software development kit used for developing Java applications. It contains tools and libraries necessary for developing, debugging, and monitoring Java applications.
- Components:
- Compiler: Converts Java source code into bytecode, which can be executed by the JVM.
- Java Runtime Environment (JRE): Includes the Java Virtual Machine (JVM) and libraries necessary for running Java applications.
- Development Tools: Tools such as
javac(Java Compiler),java(Java Interpreter),javap(Java Disassembler), etc. - APIs and Libraries: A rich set of libraries and APIs for developing Java applications, including the core Java API, JDBC for database connectivity, JavaFX for building desktop applications, etc.
- Use Case: Developers use the JDK to write, compile, and debug Java code. It’s required for developing Java applications from scratch.
- JRE (Java Runtime Environment):
- Definition: JRE is an environment that enables Java programs to run on a computer. It includes the JVM (Java Virtual Machine) and libraries necessary for executing Java bytecode.
- Components:
- Java Virtual Machine (JVM): Interprets and executes Java bytecode. It’s responsible for translating bytecode into machine-specific instructions.
- Core Libraries: A set of libraries necessary for running Java applications, including classes for I/O operations, networking, data structures, etc.
- Java Launcher: A tool for launching Java applications.
- Use Case: End-users who want to run Java applications need only the JRE installed on their system. It’s not required for development but necessary for executing Java applications.
In summary, the JDK is used for Java development, containing tools and libraries for writing and compiling Java code, while the JRE is used for running Java applications, providing the necessary runtime environment including the JVM.