Development
javac:
We can use this command to compile a single or group of java files.Syntax:
javac [options]A.java/A.java B.java
java:
We can use java Command to a run a .class file.Syntax:
java[option] A
NOTE: We can compile a group of .java files at a time where as we can run only one .class file at a time.
Classpath:
Classpath describes the location where required .class files are available.JVM always locates the required .class files.
The Following are the various possible ways to set the classpath:
1) Permanently by using environment variable classpath:
This class path will always remain preserved even if the system restarts.
2) At command prompt level by using set Command.
Set classpath=%classpath %;D:\path>
This class path will remain preserved only for that particular command prompt.
Once command execution accomplishes, classpath will be lost automatically.
3) At command level by using cp option:
java-cpD:\path>Test
This classpath is applicable only for this particular command.
Once command execution completes, classpath will be lost automatically.
Among the above 3 ways the most commonly used approach is setting classpath on command level.
Let's see an Example:
Now setting Classpath of this example on command level:
D:\Durgaclass\>javac Test.java
Java Test //valid
Output: Classpath Demo
D:\.java Test //NoClassDefFoundError
//invalid
D:\>java-cp D:\Durgaclasses Test //valid
Output: Classpath Demo
Java Test //valid
Output: Classpath Demo
D:\.java Test //NoClassDefFoundError
//invalid
D:\>java-cp D:\Durgaclasses Test //valid
Output: Classpath Demo
NOTE: If we set classpath explicitly then we can run java program from any location but if we are not Setting the classpath then we have to run java program only from current working directly.
Example:
C:
D:
C:\> javac Fresher.java //valid
D:\> javac Company.java // cannot find symbol
// Symbol:Class Fresher
// Location: class Company
D:\> javac -cp c:Company.java //valid
D:\javac Company //Run Time Error: NoClassDefoundError:Fresher
D:\ java-cp c:Company //Run Time Error: NoClassDefoundError:Company
D:\java-cp D:;C: Company
D:\java-cp;C:Company
E:\java-cp D:;C: Company
Output:
I want Job
Getting Job is very easy
Example:
C:
D:
E:
C:\> java -d.Ram //valid
D:\> java-d.Sham.java //Compile Error: cannot find symbol
// Symbol:class Ram
// Location:class Sham
D:\>java-cp c: -d.Sham.java //valid
E:\>javac Desha.java // Compile Error: cannot find symbol
// Symbol:class Sham
// Location:class Desha
E:\>javac-cp D:Desha.java
E:\>java Desha //Run Time Error: NoClassDefoundError:Sham
E:\>java-cp D:Desha //Run Time Error: NoClassDefoundError:Desha
E:\>java-cp ;D:Desha //Run Time Error: NoClassDefoundError:Ram
E:\>java-cp E: ;D:;C:;Desha
Notes :
1) Compiler will check only one level dependency where as JVM will check all Levels of dependency.
2) If any folder structure gets created because Of package statement it should be resolved through import statement only & Base package Location which we have to update in classpath.
3) Within the classpath the order of location is very important for the required .class file. JVM will always search the location left and right in classpath. Once JVM finds the required file the rest of the classpath won't be searched.
D:
E:
C:\> javac Developer.java //valid
D:\> javac Developer.java //valid
E:\> javac Developer.java //valid
C:\> java Developer //valid
Output:
C: Developer
D:\>java-cp C:;D:;E: Developer
Output:
C: Developer
D:\>java-cp E:;D:;C: Developer
Output:
E: Developer
D:\>java-cp D:;E:;C: DeveloperDeveloper
Output:
D: Developer
JAR File:
If several dependent files are available then it is never recommended to set each class file individually in the classpath. We have to group all those class files into a single Input file and we have to make that Zip file available in the class path. This zip file is nothing but JAR file.Example:
To develop a servlet all required .class files are available in Servlet- api.jar. We have to make this Jar file available in the classpath only then servlet will be compiled.
jar vs war vs ear :
1) jar : (java archive file) : It contains a group of .class files
2) war : (web archive file) : It represents a web application which may contain servlets, JSPS, HTML, CSS, JavaScript, etc.
3) ear : (enterprise archive file) : It represent an enterprise application which may contain Servlets, JSPS, EJBS, JNS Components etc.
Various Commands:
1) To create a jar file: jar - cvf durga.jar A.class B.class c.class *.class
2) To extract a jar File: jar- xvf durga.jar
3) To display the contains of a jar file: Jar-tvf durga.jar
Example:
c:\> javac Durga.java
c:\> jar -cvf Durga.jar durga.class
D:\> javac Desha.java //invalid
D:\>javac -cp c:Desha.java //invalid
D:\> javac -cp c:\Desha.java //valid
D:\> javac -cp;c:\ Desha.java
Output:
200
400
Note:
Whenever we are placing a jar file in the classpath we should include the name of the jar file, just Location is not enough.
Short Cut to place jar file:
If we are placing the jar file in the following location then it is not required to set classpath explicitly. It is by default available to JVM & Java compiler.
System Properties:
For every system persistence information will be maintained in the form of system properties. These may include OS name, virtual machine version, user country etc
We can get System properties by using getProperties() method of System class.
Example:
Demo program to print System Properties
We can set System Properties from the command prompt by using options.
JDK vs JRE vs JVM :
1) JDK (Java Development Kit): To develop & run java application in the required environment provided by JDK.2) JRE (Java Runtime Environment): To run java application in the required environment provided by JRE.
3) JDK (Java Virtual Machine): The machine is responsible to execute java program.
JDK=JRE+TOOLS
JRE=JVM+Libraries
Note: On client machine we have to install JRE where as on the developer machine we have to install JDK.
Difference Between Path And Classpath:
Classpath:We can use classpath to describe the location where required .class files are available.
If we are not setting the classpath then our program won't run.
Path:
We can use path variable to describe the location where required binary executable files are available.
If we are not setting path variable then java & javac commands won't work.
If m1() return type is void, then we will get Compile Time Error saying "void type not allowed here".
Various Runtime Flags
1) -ea: To enable assertions in every non-System class.
2) -enableassertions: It is Exactly same as -ea
3) -da: To disable assertion in every non-system class.
4) -disableassertions: It is same as -da.
5) -esa: To enable assertions in every system class.
6) -enabledSystemassertions: It is exactly same as -esa.
7) -dsa: To disable assertions in every System class.
8) -disableSystem asscertions: It is same as -dsa.
Example No. 1:
java -ea -esa -da -esa -ea -dsa
we can use these flag in together & all these flags executed from Left To Right
Example No. 2:
1) java -ea:pack1.A
2) java -ea:pack1.B -ea:pack1 pack2.D
3) java -ea -da packB
Example No. 3:
To enable assertion in only A class
1) Java -ea:pack1.A
To enable assertion in both B & D class.
2) Java -ea:pack1.B -ea:pack1:pack2.D
To enable assertion in every non-System class except B.
3) Java -ea:pack1. -da:pack1.B
To enable assertion in every class of pack1 & its sub except sub packages.
4) Java -ea:pack1.
To enable assertion in every where with in pack1 except pack2.
5) Java -ea:pack1. -da:pack1,pack2
6) Appropriate & Inappopriate use of asserting:
It is always inappropriate to mix programmimg logic with assert statement because there is no execution of assert statement at runtime.
Example: