0% found this document useful (0 votes)
15 views23 pages

First App Development

This document outlines the steps to prepare and run a Java application, including downloading and installing Java software and editor, setting environment variables, and compiling and executing Java files. It provides detailed instructions on managing multiple Java versions and emphasizes the importance of proper file naming conventions for Java classes. Additionally, it discusses the use of batch files for easier switching between Java versions and the requirements for compiling Java files.

Uploaded by

abhijit3069
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views23 pages

First App Development

This document outlines the steps to prepare and run a Java application, including downloading and installing Java software and editor, setting environment variables, and compiling and executing Java files. It provides detailed instructions on managing multiple Java versions and emphasizes the importance of proper file naming conventions for Java classes. Additionally, it discusses the use of batch files for easier switching between Java versions and the requirements for compiling Java files.

Uploaded by

abhijit3069
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Steps To Prepare First Java Application:

—----------------------------------------
1. Download and Install Java Software
2. Download and Install Java Editor Software
3. Open Java Editor , open Java File and Write Java program
4. Save Java File
5. Compile Java File
6. Execute Java Application

Download and Install Java Software:


—-----------------------------------
Download the java softwares 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

JAVA17:
https://www.oracle.com/java/technologies/javase/jdk17-archive-
downloads.html

To install Java Software we have to use the following steps.


1. Double Click on “jdk-6u45-windows-x64.exe”.
2. Click on the “Next” button.
3. Change the JDK installation location to “C:\Java\jdk1.6.0_45” by
clicking on the “Change” button.
4. Click on the “Next” button.
5. Change the jre installation location to “C:\Java\jre6” by
clicking on the “Change” button.
6. Click on the “Next” button.
7. Click on the “Close” button.

Note: Similarly install JAVA 7, JAVA8 and JAVA 17.


After Installation of the Java Software we have to set the “path”
environment variable to the location where all java commands exist in
order to use all java commands in the command prompt.

There are two ways to set the “path” environment variable.


1. Temporary Setup
2. Permanent Setup

Temporary Setup:
To set the path environment variable Temporarily we have to use the
following command on 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.

1. Right Click on “This PC” on Desktop.


2. Select “Properties”.
3. Select “Advanced System Settings”.
4. Select the “Advanced” tab.
5. Click on the “Environment Variables…” button.
6. If the “path” environment variable exists already in the USer
variables then select it and edit with a new value like “C:\Java\
jdk1.8.0_202\bin”.
a. Select “path”.
b. Click on the “Edit” button.
c. Click on the “New” button.
d. Provide “C:\java\jdk1.8.0_202\bin” value .
e. Click on the “OK” button.
7. If the “path” environment does not exist in the USer variables
window
then create a new path environment variable with the
“C:\java\jdk1.8.0_202\bin” value.

a. Click on the “New” button.


b. Provide the following7 details.
Variable Name: path
Variable Value : C:\java\jdk1.8.0_202\bin
c. Click on the “OK” button.

8. Click on the “OK” button.


9. Click on the “OK” button.

Note: Open Multiple Command prompts and check for “javac” .

Q)Is it possible to install more than one java software in a single


computer?
—---------------------------------------------------------------------
-------
Ans:
—---
Yes, it is possible to install more than one java software in a
single computer with the following conditions.
1. All the Java softwares must be of the same Operating System.
2. All the Java softwares must be with different versions like
jDK1.6, JDK1.7, JDK1.8….

Q)If we install multiple java softwares in a single computer then


which version of the java software will be available to the command
prompt to use?
—---------------------------------------------------------------------
---
Ans:
If we install multiple java softwares in a single computer then the
java version which we set to “path” environment variable will come to
the command prompt to use.

EX:

C:\Users\Administrator>set path=C:\java\jdk1.6.0_45\bin;

C:\Users\Administrator>javac -version
javac 1.6.0_45

C:\Users\Administrator>set path=C:\java\jdk1.7.0_80\bin;
C:\Users\Administrator>javac -version
javac 1.7.0_80

C:\Users\Administrator>set path=C:\java\jdk1.8.0_202\bin;

C:\Users\Administrator>javac -version
javac 1.8.0_202

C:\Users\Administrator>

Q)If we install multiple java softwares in a single computer and if


set multiple java versions to the path environment variable then which
java version will come to the command prompt inorder to use?
—---------------------------------------------------------------------
-----
Ans:
—--
If we set multiple java versions to the “path” environment variable
then the java version which we set as first one to the path
environment in the order that java version will come to the command
prompt to use.

EX:
C:\Users\Administrator>set path=C:\java\jdk1.6.0_45\bin;C:\java\
jdk1.7.0_80\bin;C:\java\jdk1.8.0_202\bin;

C:\Users\Administrator>javac -version
javac 1.6.0_45

C:\Users\Administrator>set path=C:\java\jdk1.7.0_80\bin;C:\java\
jdk1.8.0_202\bin;C:\java\jdk1.6.0_45\bin;

C:\Users\Administrator>javac -version
javac 1.7.0_80

C:\Users\Administrator>set path=C:\java\jdk1.8.0_202\bin;C:\java\
jdk1.6.0_45\bin;C:\java\jdk1.7.0_80\bin;

C:\Users\Administrator>javac -version
javac 1.8.0_202

C:\Users\Administrator>
Q) If we set “JDK1.6” version to the path environment variable
temporarily and if we set “JDK1.8” version to the “path” environment
variable permanently then which java version will come to the command
prompt to use?
—---------------------------------------------------------------------
------
Ans:
—---
If we set JDK1.6 version temporarily and JDK1.8 version permanently
then temporary setup provided java version will come to the command
prompt that is JDK1.6 version will come to the command prompt to use.

IN Java applications, if we want to compile and execute a java


application in multiplier java versions then we have to change java
from one version to another version frequently, where to switch java
from one version to another version we have to set “path” environment
variable explicitly, it is not susceptible, here to simplify the
process of switching java from one version to another version we will
use “batch” files.

Prepare a separate batch file for each and every java version and
provide the respective “path” environment variable setup in the
respective batch file.

D:\java10\java6.bat
set path=C:\Java\jdk1.6.0_45\bin;

D:\java10\java7.bat
set path=C:\Java\jdk1.7.0_80\bin;

D:\java10\java8.bat
set path=C:\Java\jdk1.8.0_202\bin;

To run the above batch file we have to write the batch file name and
click on enter button.

On COmmand Prompt:

D:\java10>java6.bat

D:\java10>set path=C:\java\jdk1.6.0_45\bin;
D:\java10>javac -version
javac 1.6.0_45

D:\java10>java7.bat

D:\java10>set path=C:\java\jdk1.7.0_80\bin;

D:\java10>javac -version
javac 1.7.0_80

D:\java10>java8.bat

D:\java10>set path=C:\java\jdk1.8.0_202\bin;

D:\java10>javac -version
javac 1.8.0_202

D:\java10>

Download and Install Java Editor Software:


—------------------------------------------
Editor: It is a software, it will provide a very good environment to
write Java programs and to save Java programs in our computer.

Note: Editors are not responsible for the Compilation and Execution of
the programs, but some editors are giving compilation and Execution
processes also.

EX: Notepad, Editplus, Notepad++, Sublime Text, Atom, Brackets,...

Note: Editors are suggestible up to the learning process, but Editors


are not suggestible in the real time Project Development, in Real time
project development IDEs[Integrated Development Environment] are
suggestible.

EX: Eclipse, Intellij IDEA , Netbeans,...

To download Editplus Editor use the following link.

https://www.editplus.com/download.html

To install the Editplus use the following steps.


1. Double Click on “epp570_4352_64bit.exe” file.
2. Click on the “Accept” button.
3. Click on the “Start Copy” button.
4. Click on the “OK” button.

Open Java Editor and Java File then Write Java program:
—-------------------------------------------------------
After opening the Java file in Editplus Editor we have to write a Java
program with the following elements.

1. Main Class.
2. main() method.
3. In the main() method use System.out.println() to display data on
command prompt.

Save Java File:


—---------------
To save Java files in our computer we have to use the following
conditions.

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

2. 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:\java10\abc.java
class Test{
public static void main(String[] args){
System.out.println("First Java Appliction.....");
}
}

D:\java10>javac abc.java

D:\java10>java Test
First Java Appliction.....

D:\java10>

EX: D:\java10\Test.java
class Test{
public static void main(String[] args){
System.out.println("First Java Appliction.....");
}
}

D:\java10>javac Test.java

D:\java10>java Test
First Java Appliction.....

D:\java10>

EX: D:\Java10\Test.java
public class A{
}
class Test{
public static void main(String[] args){
System.out.println("First Java Appliction.....");
}
}

D:\java10>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

D:\java10>

EX: D:\Java10\A.java
public class A{
}
class Test{
public static void main(String[] args){
System.out.println("First Java Appliction.....");
}
}

D:\java10>javac A.java

D:\java10>java Test
First Java Appliction.....

D:\java10>

Q)Is it possible to provide more than one public class in a single


java file?
—---------------------------------------------------------------------
-------
Ans:
—--
No, It is not possible to declare more than one public class in a
single java file, because if we provide more than one class as public
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:\java10\A.java
public class A{
}
public class B{
}
class Test{
public static void main(String[] args){
System.out.println("First Java Appliction.....");
}
}

D:\java10>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:\java10>

EX: D:\Java10\B.java
public class A{
}
public class B{
}
class Test{
public static void main(String[] args){
System.out.println("First Java Appliction.....");
}
}

D:\java10>javac B.java
B.java:1: error: class A is public, should be declared in a file named
A.java
public class A{
^
1 error

D:\java10>

EX: D:\java10\AB.java
public class A{
}
public class B{
}
class Test{
public static void main(String[] args){
System.out.println("First Java Appliction.....");
}
}

D:\java10>javac AB.java
AB.java:1: error: class A is public, should be declared in a file
named A.java
public class A{
^
AB.java:3: error: class B is public, should be declared in a file
named B.java
public class B{
^
2 errors

D:\java10>

Compile Java File:


—-----------------
Q) What is the requirement to compile java files?
—--------------------------------------------------
Ans:
—---
The main intention of the compilation is
1. To translate a java program from Source code to Bytecode.
2. To check developers' mistakes in java applications.

To perform Compilation, JAVA has provided a separate command like


“javac”.

javac FileName.java

EX:
D:\java10>javac Test.java

When we use the above command on command prompt then the Operating
System will perform the following actions.

1. Operating System will take javac command from the command prompt
and the Operating System will search for javac in its internal
commands list and at the locations referred to the “path”
environment variable.
2. If javac is not identified at the above two 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.

3. To make available all java commands to the operating system then


we have to set the “path” environment variable to the location
where all java commands are available, that is C:\java\jdk1.8.0\
bin.

set path= C:\java\jdk1.8.0\bin;

4. When the Operating System identifies a javac command through the


path environment variable then the Operating System will execute
the javac command , with this Operating System will activate the
Java Compiler Software.

WEhen Java Compiler SOftware is activated , Java compiler will


perform the following actions.

a. Compiler will take the java file name from the command
prompt.
b. Compiler will search for the java file at the current
location.
c. If the required java file does not exist then the compiler
will raise an error.

javac: file not found: Test.java

d. If the required java file exists then the compiler will


start compilation right from the starting point of the file
to the ending point of the file.
e. In the compilation process, if any error is identified in
the java file then the compiler will raise an error. If no
error is identified in the java file 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 have compiled, it is
depending on the number of classes, abstract classes,
interfaces, enums and inner classes.
EX:
D:\java10\Test.java
enum E{
}
interface I{
}
abstract class A{
}
class B{
class C{
}
}
class Test{
public static void main(String[] args){
System.out.println("Hello World!");
}
}

D:\java10>javac Test.java

D:\java10>dir
Volume in drive D is Data
Volume Serial Number is 68E9-29EB

Directory of D:\java10

13-07-2023 11:24 <DIR> .


13-07-2023 11:24 <DIR> ..
13-07-2023 11:24 179 A.class
13-07-2023 11:24 270 B$C.class
13-07-2023 11:24 223 B.class
13-07-2023 11:24 611 E.class
13-07-2023 11:24 86 I.class
11-07-2023 11:26 35 java6.bat
11-07-2023 11:27 35 java7.bat
11-07-2023 11:27 36 java8.bat
13-07-2023 11:24 414 Test.class
13-07-2023 11:22 180 Test.java
10 File(s) 2,069 bytes
2 Dir(s) 347,551,645,696 bytes free

D:\java10>
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.

D:\Fullstack10AM>javac -d E:\abc\xyz Test.java

EX:
D:\Fullstack10am\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:\Fullstack10AM>javac -d E:\abc\xyz Test.java

D:\Fullstack10AM>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:\Fullstack10AM\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:\Fullstack10AM>javac -d E:\abc\xyz Test.java

D:\Fullstack10AM>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?
—---------------------------------------------------------------------
------
Ans:
—---
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.

EX:
D:\Fullstack10am
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:\Fullstack10AM>javac A.java

D:\Fullstack10AM>dir
Volume in drive D is Data
Volume Serial Number is 68E9-29EB

Directory of D:\Fullstack10AM

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
02-07-2023 10:33 <DIR> apps
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
13-07-2023 12:13 53,125 FirstApp.png
11-07-2023 11:26 35 java6.bat
11-07-2023 11:27 35 java7.bat
11-07-2023 11:27 36 java8.bat
22-06-2023 17:23 18,202 Pointers_Vs_Reference.png
14-07-2023 10:57 230 Test.java
14 File(s) 72,667 bytes
3 Dir(s) 347,551,588,352 bytes free

D:\Fullstack10AM>

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:\Fullstack10am
A.java
public class A {

B.java
public class B{
}

C.java
public class C{

D.java
public class D{

D:\Fullstack10AM>javac A.java B.java C.java D.java

D:\Fullstack10AM>dir
Volume in drive D is Data
Volume Serial Number is 68E9-29EB

Directory of D:\Fullstack10AM

14-07-2023 11:20 <DIR> .


14-07-2023 11:20 <DIR> ..
14-07-2023 11:20 176 A.class
14-07-2023 11:17 24 A.java
02-07-2023 10:33 <DIR> apps
14-07-2023 11:20 176 B.class
14-07-2023 11:17 23 B.java
14-07-2023 11:20 176 C.class
14-07-2023 11:18 23 C.java
14-07-2023 11:20 176 D.class
14-07-2023 11:18 23 D.java
13-07-2023 12:13 53,125 FirstApp.png
11-07-2023 11:26 35 java6.bat
11-07-2023 11:27 35 java7.bat
11-07-2023 11:27 36 java8.bat
22-06-2023 17:23 18,202 Pointers_Vs_Reference.png
14-07-2023 10:57 230 Test.java
14 File(s) 72,460 bytes
3 Dir(s) 347,551,588,352 bytes free

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

EX:
D:\Fullstack10am
A.java
public class A {

B.java
public class B{
}

C.java
public class C{

D.java
public class D{

}
D:\Fullstack10AM>javac *.java

D:\Fullstack10AM>dir
Volume in drive D is Data
Volume Serial Number is 68E9-29EB

Directory of D:\Fullstack10AM

14-07-2023 11:25 <DIR> .


14-07-2023 11:25 <DIR> ..
14-07-2023 11:25 198 A.class
14-07-2023 11:17 24 A.java
02-07-2023 10:33 <DIR> apps
14-07-2023 11:25 261 B.class
14-07-2023 11:17 23 B.java
14-07-2023 11:25 176 C.class
14-07-2023 11:18 23 C.java
14-07-2023 11:25 176 D.class
14-07-2023 11:18 23 D.java
14-07-2023 11:25 706 E.class
13-07-2023 12:13 53,125 FirstApp.png
14-07-2023 11:25 105 I.class
11-07-2023 11:26 35 java6.bat
11-07-2023 11:27 35 java7.bat
11-07-2023 11:27 36 java8.bat
22-06-2023 17:23 18,202 Pointers_Vs_Reference.png
14-07-2023 11:25 454 Test.class
14-07-2023 10:57 230 Test.java
18 File(s) 74,178 bytes
3 Dir(s) 347,551,588,352 bytes free

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:\Fullstack10am>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:\Fullstack10am>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:\Fullstack10am>javac *Account*.java

Execute Java Application


—-----------------------
To execute Java applications we have to use the “java” command
provided by the Java Software.

java MainClassName

EX:
D:\Fullstack10am>java Test

If we use the above command on command prompt then the Operating


System will perform the following actions.

1. Operating System will take java command from command prompt,


Operating System Will search for java command in its internal
commands list and at at the locations referred by the path
environment variable.
2. The Operating System will identify java command at the location
“C:\Java\jdk1.8.0\bin” through “path” environment variable, with
this Operating System will execute java command , where JVM
software will be activated and JVM will perform the following
actions.

a. JVM will take the main class name from the command prompt.
b. JVM will search for the main class .class file at current
location, at the java predefined library and at the
locations refer by the “classpath” environment variable.
c. 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:Test
JAVA7: Error: Could not find or load main class Test

d. If we know the location where the main class .class file


exists then we have to provide main class .class file
location to the JVM, for this we have to set “classpath”
environment variable to the location where main
class .class file exist.
set classpath=E:\abc;

e. 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.
f. After loading 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: main


JAVA7: Error: Main method not found in class Test, please
define
the main method as:
public static void main(String[] args)

g. If the main() method exist 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.
h. Main Thread will execute the complete main() method, at the
end of the application execution, automatically Main Thread
will come to the Dead State.
i. When Main Thread is in Dead state , JVM will stop all of
its internal processes and JVM will got to shutdown mode.

Q) What is the difference between “path” and “classpath” environment


variables?
—---------------------------------------------------------------------
-------
Ans:
—----
“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 the 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.

You might also like