Showing posts with label First JAVA File. Show all posts
Showing posts with label First JAVA File. Show all posts

Saturday, December 24, 2016

How to Install JAVA part-3

5. Compile JAVA file:

--------------------------------------------------------------------------------------------------------------------
The main Intention to compile JAVA files is:

A. To translate JAVA code from high level representation to low level representation.
B. To check developers mistake in JAVA application

To compile JAVA files, we have to use the following command on command prompt:

Javac File_Name.java

EX: D:\java10>javac FirstApp.java
---------------------------------------------

C. If we provide the above command on command prompt then operating system will perform the following actions:
i. Operating System will take “javac” command prompt
ii. Operating System will search for javac command at its predefined command list and at  the location refereed by “path” environment variable.
iii. If “javac” is not identified at both the location then operating system will display the following message.
iv. ‘javac’ is not recognized as an internal or external command, operable program or batch file.

Note: To give all java commands location information to the operating system, we have to set “path” environment variable to “C:\java\jdk1.7.0_79\bin;
On command Prompt:

D:\Java10>set path=C:\java\jdk1.7.0_79\bin;

D. If javac is identified by the operating system at either of the above location then operating system will execute “javac” command, with this, JAVA compiler software will be activated and JAVA compiler will perform the following actions.
 I.     Compiler will take the provided JAVA file from command prompt.
II.     Compiler will check whether the specified JAVA file is existed or not at current location
III.    If the specified JAVA file is not existed then compiler will provide the following message :   

         javac: file not Found: FirstApp.java
run java through command prompt
File not found in JAVA


IV.     If the specified in the JAVA file then the compiler will display error message on command  prompt.
V.     If no error is identified in the present JAVA file then the compiler will generate .class files.

Note: Generating Number of .class files is not at all depending on the no. of .java files which we compiled, it is completely depending on the no. of classes, abstract classes, interfaces, enums and inner classes which we used In present JAVA file.

If we want to compile JAVA file from current location & if we want to select generated .class file to a particular target file then we have to use ‘-d’ option.
EX: D:\java10\javac –d C:\abc FirstApp.java

If we want to compile JAVA file from current location and if we want to generate .class files by creating folder, structure with respect to the package name then we have to use ‘-d’ option along with “javac” command.

EX: File Name: D:\java10\FirstApp.java
enum E
{          
}
interface I
{
}
abstract class A{
           
}
class B {
            class C{
                       
            }
}
class FirstApp {
            public static void main(String[] args){
                        System.out.println("First JAVA Application");
            }
}

On command Prompt:
D:\java10>javac –d C:\abc FirstApp.java
compilation process of a java file
Compilation Format of JAVA File


If we want to compile all JAVA files which are existed at current location then we have to use the following command on command Prompt.
 D:\java10>javac *.java

If we want to compile all JAVA files which started with a common prefix then we have to use the following command on command prompt:
D:\java10>javac student*.java

If we want to compile all JAVA files which are having a common postfix then we have to use the following command on command prompt:
D:\java10>javac *Emails.java

If we want to compile all java files which contains a common word then we have to use the following command on command prompt:

D:\java10>javac *Account*.java

How to Install JAVA Part-2

4. Save JAVA File:

---------------------------------------------------------------------------------------------------------------
To save JAVA File, we have to use following conditions:

A. If the present java file contains any public element [class, interface, abstract class, enum] then the present JAVA file must be saved with public element name only, if we violate this condition then compiler will rise an error.

B. If no pubic element is existed in present JAVA file then it is possible to save JAVA file with any name like abc.java or xyz.java, but it is suggestible to save JAVA file with main class name instead of any other name.

EX: File Name: abc.java
--------------------------------
class FirstApp {
            public static void main(String[] args) {
                        System.out.println(“First JAVA Application”);
            }
}
Status: No compilation error, but not suggestible to save abc.java


EX: File Name: FirstApp.java
class FirstApp {
            public static void main(String[] args){
                        System.out.println("First JAVA Application");
            }
}
Status: No compilation Error & Suggestible.


EX: File Name: FirstApp.java
public class A {
}
class FirstApp {
            public static void main(String[] args){
                        System.out.println("First JAVA Application");
            }
}
Status: Compilation Error


EX: File Name: A.java
public class A {
}
class FirstApp {
            public static void main(String[] args){
                        System.out.println("First JAVA Application");
            }
}

Status: No Compilation Error.

Q. Is it possible to provide more than one public class with in a single java file?
ANS. No, it is not possible to provide more than one public class with in a single JAVA file, because, if we provide more than  one public class with in a single JAVA file, then we must save that java file with more than one name, which is not possible in OS.

EX: File Name: A.java
public class A {
}
public class B {
}
class FirstApp {
            public static void main(String[] args){
                        System.out.println("First JAVA Application");
            }
}
Status: compilation Error.


Thursday, December 22, 2016

How to Install JAVA Part-1

Step Prepare First JAVA Application:

--------------------------------------------------------------------
1. Install JAVA Software
2. Select JAVA Editor
3. Write JAVA Program
4. Save JAVA File
5. Compile JAVA File
6. Execute JAVA Problem

1. Install JAVA Software:

--------------------------------------
i. Download jdk file from Oracle or Click Here.
   (Accept the terms and click on the link according to your OS).
ii. Double click on jdk-8u111.exe file.
iii. Click on “Yes” Button.
iv. Click on “Next” Button.
v. Change jdk Installation location from “C:\Program file(x86)\Java\jdk1.8.0_111” to
      “C:\Java\jdk1.8.0_111” by clicking on change button and OK button.
vi. Click on “Next” Button.
vii. Change jre installation location from “C:\Program file(x86)\Java\jre8” to
       “C:\Java\jre8” by clicking change Button and OK button.
viii. Click on “Next” Button.
ix. Click on “Close” Button.

After Installation of JAVA Software, we have to set “path” environment variable to the location where JAVA commands like javac, java, javap, javadoc,… are existed, that is  “C:\Java\jdk1.8.0_111\bin”.

In command prompt:

set path= C:\Java\jdk\1.8.0_111\bin;

Note: the main intention to set “path” environment variable to give location information of all JAVA commands like javac, java, … to the operating system in order to execute.

The above “path” setup is temporary setup, it is valid up to the present command prompt only. If we want to set “path” environment permanently then we have to use the following steps:
a) Right Click on “This PC” on desktop.
b) Select “Properties”.
c) Click on “Advanced System Setting”.
d) Select “Advanced Tab” (By Default Selected).
e) Click on “Environmental Variable” Button.
f) Goto user variables section and click on “New” Button.
g) Provide the following details:
variable name: path
variable value: C:\Java\jdk1.7.0_79\bin;
h) Click on “Ok” Button.
i) Click on “Ok” Button.
j) Click on “Ok” Button.
----------------------------------------------------------------------------------------

2. Select JAVA Editor:

Editor is a Software, it will provide very good environment to write java programs & to save JAVA program into our system.
EX: Notepad, Notepad++, EditPlus…

Note: In real time application development, it is not suggestible to use editors, always it is suggestible to use IDEs[Integrated Development Environments]
EX: Eclipse, MyEclipse, Netbeans, Intellij,…..

---------------------------------------------------