JAVA
Paradigm: multi-paradigm, object-oriented, structured, imperative, functional, generic, reflective, concurrent
Desinged by : James Gosling & Sunmicrosystems
Developer : oracle corporation
First appeared: 1995 ,20y ago
Stable Release : java standard Edition 8(update 45(1.8.0-45[April 14,2015])
preview Release : java standard Edition 9 (Early Access by b46 1.9.0-ea-b46[january 20,2015])
Typing Discipline : static, strong, safe, nominative, manifest
Implementation language : c, c++
operating system : cross platform
Licence : GNU general public licence, java community process
File name extensions: .java, .class, .jar
Platform : Java EE[enterprise edition],Java ME[micro edition], java SE[standard Edition],java card[to run applet]
Principles: 5 goals in the creation of java language
1. it must be "simple , Object oriented & familiar"
2. It must be "robust & Secure"
3. It must be "architecture-neutral & portable"
4.it must exectue with "high performance"
5. it must be "interpreted , threaded, and dynamic"
Versions: Major
- JDK1.0[21-jan-1996]
- JDK1.1[19-feb-1997]
- J2SE1.2[8-Dec-1998]
- J2SE1.3[8-may-2000]
- J2SE1.4[6-feb-2002]
- J2SE5.0[30-sep-2004]
- J2SE6[11-dec-2006]
- JAVA SE7[8-jul-2011]
- JAVA SE8[8-mar-2014]
- History:
The history of java starts from Green Team. Java team members (also known as Green Team), initiated a revolutionary task to develop a language for digital devices such as set-top boxes, televisions etc.
For the green team members, it was an advance concept at that time. But, it was suited for internet programming. Later, Java technology as incorporated by Netscape.
Currently, Java is used in internet programming, mobile devices, games, e-business solutions etc. There are given the major points that describes the history of java.
1) James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language project in June 1991. The small team of sun engineers called Green Team.
2) Originally designed for small, embedded systems in electronic appliances like set-top boxes.
3) Firstly, it was called "Greentalk" by James Gosling and file extension was .gt.
4) After that, it was called Oak and was developed as a part of the Green project.
- Why Oak name for java language?
5) Why Oak? Oak is a symbol of strength and choosen as a national tree of many countries like U.S.A., France, Germany, Romania etc.
6) In 1995, Oak was renamed as "Java" because it was already a trademark by Oak Technologies.
- Why Java name for java language?
7) Why they choosed java name for java language? The team gathered to choose a new name. The suggested words were "dynamic", "revolutionary", "Silk", "jolt", "DNA" etc. They wanted something that reflected the essence of the technology: revolutionary, dynamic, lively, cool, unique, and easy to spell and fun to say.
According to James Gosling "Java was one of the top choices along with Silk". Since java was so unique, most of the team members preferred java.
8) Java is an island of Indonesia where first coffee was produced (called java coffee).
9) Notice that Java is just a name not an acronym.
10) Originally developed by James Gosling at Sun Microsystems (which is now a subsidiary of Oracle Corporation) and released in 1995.
11) In 1995, Time magazine called Java one of the Ten Best Products of 1995.
12) JDK 1.0 released in(January 23, 1996).
- Java Version History
There are many java versions that has been released. Current stable release of Java is Java SE 8.
- JDK Alpha and Beta (1995)
- JDK 1.0 (23rd Jan, 1996)
- JDK 1.1 (19th Feb, 1997)
- J2SE 1.2 (8th Dec, 1998)
- J2SE 1.3 (8th May, 2000)
- J2SE 1.4 (6th Feb, 2002)
- J2SE 5.0 (30th Sep, 2004)
- Java SE 6 (11th Dec, 2006)
- Java SE 7 (28th July, 2011)
- Java SE 8 (18th March, 2014)
- What is JAVA?
Java is a programming language and a platform.
Java is a high level, robust, secured and object-oriented programming language.
Platform: Any hardware or software environment in which a program runs, is known as a platform. Since Java has its own runtime environment (JRE) and API, it is called platform.
Where it is USED?
According to Sun, 3 billion devices run java. There are many devices where java is currently used. Some of them are as follows:
- Desktop Applications such as acrobat reader, media player, antivirus etc.
- Web Applications such as irctc.co.in, javatpoint.com etc.
- Enterprise Applications such as banking applications.
- Mobile
- Embedded System
- Smart Card
- Robotics
- Games etc.
- What are the JAVA applications?
There are mainly 4 type of applications that can be created using java programming:
1) Standalone Application
It is also known as desktop application or window-based application. An application that we need to install on every machine such as media player, antivirus etc. AWT and Swing are used in java for creating standalone applications.
2) Web Application
An application that runs on the server side and creates dynamic page, is called web application. Currently, servlet, jsp, struts, jsf etc. technologies are used for creating web applications in java.
3) Enterprise Application
An application that is distributed in nature, such as banking applications etc. It has the advantage of high level security, load balancing and clustering. In java, EJB is used for creating enterprise applications.
4) Mobile Application
An application that is created for mobile devices. Currently Android and Java ME are used for creating mobile applications.
- Features of Java
There is given many features of java. They are also known as java buzzwords. The Java Features given below are simple and easy to understand.
- Simple
- Object-Oriented
- Platform independent
- Secured
- Robust
- Architecture neutral
- Portable
- Dynamic
- Interpreted
- High Performance
- Multithreaded
- Distributed
Simple
According to Sun, Java language is simple because: |
syntax is based on C++ (so easier for programmers to learn it after C++). |
removed many confusing and/or rarely-used features e.g., explicit pointers, operator overloading etc. |
No need to remove unreferenced objects because there is Automatic Garbage Collection in java. |
Object-oriented
Object-oriented means we organize our software as a combination of different types of objects that incorporates both data and behaviour. |
Object-oriented programming(OOPs) is a methodology that simplify software development and maintenance by providing some rules. |
Basic concepts of OOPs are: |
|
Platform Independent
A platform is the hardware or software environment in which a program runs. There are two types of platforms software-based and hardware-based. Java provides software-based platform. The Java platform differs from most other platforms in the sense that it's a software-based platform that runs on top of other hardware-based platforms.It has two components:
|
Secured
Java is secured because: |
|
|
These security are provided by java language. Some security can also be provided by application developer through SSL,JAAS,cryptography etc. |
Robust
Robust simply means strong. Java uses strong memory management. There are lack of pointers that avoids security problem. There is automatic garbage collection in java. There is exception handling and type checking mechanism in java. All these points makes java robust. |
Architecture-neutral
There is no implementation dependent features e.g. size of primitive types is set. |
Portable
We may carry the java bytecode to any platform. |
High-performance
Java is faster than traditional interpretation since byte code is "close" to native code still somewhat slower than a compiled language (e.g., C++) |
Distributed
We can create distributed applications in java. RMI and EJB are used for creating distributed applications. We may access files by calling the methods from any machine on the internet. |
Multi-threaded
A thread is like a separate program, executing concurrently. We can write Java programs that deal with many tasks at once by defining multiple threads. The main advantage of multi-threading is that it shares the same memory. Threads are important for multi-media, Web applications etc.
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
- Installing and Running JAVA programs
- Download Java from
http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
- download the product based on the operating system
- after download install the software by double click
- then you will get a folder called java under C:\program files
- inside JAVA folder you will see two more folders called JDK & JRE
- after installing JDK
- we get two folders in our system
- JDK & JRE
- JDK,JRE,JVM:
JDK:
acronym for Java Development Kit. it physically exists. It contains JRE+develoment tools.
JRE:
is an acronym for Java Runtime environment.It is used to provide runtime environment. it is the implementation of the JVM . It physically exists It contains set of libraries + other files that JVM uses at runtime.
JVM:
JVM is an acronym for Java virtual machine. it is an abstract machine. It is a specification that provides runtime environment in which java bytecode can be executed.
JVM's are available for many hardware and software platforms. JVM, JRE, and JDK are platform dependent because configuration of each os is different .
But java is a platform independent.
JVM performs following tasks:
- Loads code
- verifies code
- executes code
- provides runtime environment
- What is JVM?
it is a :
1. specification where working of java Virtual machine is specified . But implementation provider is independent to choose the algorithm. Its implementation has been provided by Sun and other companies.
2. An implementation its implementation is known as JRE
3. Runtime Instance whenever you write java command on the command prompt to run the java class, and instance of JVM is created.
JVM provides definition for the :
- memory area
- class file format
- register set
- garbage collected heap
- fatal error reporting etc.
Internal Architecture of JVM
Let's understand the internal architecture of JVM. It contains classloader, memory area, execution engine etc. |
- classLoader:Classloader is a subsystem of JVM that is used to load class files.
- Class(Method) Area: Class(Method) Area stores per-class structures such as the runtime constant pool, field and method data, the code for methods.
- heap: It is the runtime data area in which objects are allocated.
- Stack: Java Stack stores frames.It holds local variables and partial results, and plays a part in method invocation and return.
Each thread has a private JVM stack, created at the same time as thread. A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes.
5. Program Counter Register: PC (program counter) register. It contains the address of the Java virtual machine instruction currently being executed.
6. Native Method Stack:
It contains all the native methods used in the application.
7. Execution Engine:
It contains: 1) A virtual processor 2) Interpreter:Read bytecode stream then execute the instructions. 3) Just-In-Time(JIT) compiler:It is used to improve the performance.JIT compiles parts of the byte code that have similar functionality at the same time, and hence reduces the amount of time needed for compilation.Here the term ?compiler? refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.
Usually, when you only care about running Java programs on your browser or computer you will only install JRE. It's all you need. On the other hand, if you are planning to do some Java programming, you will also need JDK.Sometimes, even though you are not planning to do any Java Development on a computer, you still need the JDK installed. For example, if you are deploying a WebApp with JSP, you are technicallyjust running Java Programs inside the application server. Why would you need JDK then? Because application server will convert JSP into Servlets and use JDK to compile the servlets. I am sure there might be more examples.
- HOW TO SET PATH?
The path is required to be set for using tools such as javac, java etc
If you are saving the java source file inside the jdk/bin directory, path is not required to be set because all the tools will be available in the current directory.
But If you are having your java file outside the jdk/bin folder, it is necessary to set path of JDK.
There are 2 ways to set java path:
- temporary
- permanent
1) How to set Temporary Path of JDK in Windows
To set the temporary path of JDK, you need to follow following steps:
- Open command prompt
- copy the path of jdk/bin directory
- write in command prompt: set path=copied_path
For Example:
set path=C:\Program Files\Java\jdk1.6.0_23\bin
2) How to set Permanent Path of JDK in Windows
For setting the permanent path of JDK, you need to follow these steps:
- Go to MyComputer properties -> advanced tab -> environment variables -> new tab of user variable -> write path in variable name -> write path of bin folder in variable value -> ok -> ok -> ok
For Example:
1)Go to MyComputer properties |
2)click on advanced tab |
3)click on environment variables |
4)click on new tab of user variables |
5)write path in variable name |
6)Copy the path of bin folder |
7)paste path of bin folder in variable value |
8)click on ok button |
9)click on ok button |
Now your permanent path is set.You can now execute any program of java from any drive.
- Class Path:
class path in java is path to directory or list of directory which is used by ClassLoaders to find and load class in java program. Class path can be specified using CLASSPATH environmental variable which is case insensitive, -cp or classpath command line option or class-path attribute in manifest.mf file in side JAR file in java.
Classpath is different from path.
CLASSPATH is an environmental variable which is used by JVM to locate user defined classes.
CLASSPATH is used to locate java commands. used to understand how java locates your file
PATH is used to locate binaries located in JDK installation dierctory.
- Setting CLASSPATH in WINDOWS:
You can also set classpath in windows by using DOS command like :
set CLASSPATH=%CLASSPATH%;JAVA_HOME\lib;
CLASSPATH=/home/tester/classes
java -cp $CLASSPATH Test
By default Java CLASSPATH points to current directory denoted by "." and it will look for any class only in current directory.
- Different Examples of Using Classpath:
so here i need to compile and run the class by going to the particular directory
Errors Related to ClassPATH in JAVA:
two most common issues related to javaclasspath is
1. ClassNotFoundException
2. NoClassDefFoundError
ClassNotFoundException : is an exception and can recoverable will be thrown when a java program dynamically tries to load a java class at Runtime and don't find corresponding class file on classpath.
Ex: when you try to load JDBC driver by using class.forName("driver Name"); and greeted with
java.lang.ClassNotFoundException : com.mysql.jdbc.Driver.
this error essentially comes when java try to load a class using forName() or by loadclass() method of classLoader .
NoClassDefFoundError can also comes due to various other reason like static initializer failure or class not visible to Classloaders in J2EE environment
NoClassDefFoundError: it is a critical Error. this comes when a particular class was present in Java Classpath during compile time but not available during run-time.
Ex: using log4j.jar for logging purpose and forgot to include log4j.jar on classpath in java during runtime.
SUMMARY OF CLASSPATH in JAVA:
- Classpath in java is an environment variable used by java virtual machine to locate or find class files in java during class loading.
- You can override value of Classpath in Java defined by environment variable CLASSPATH by providing JVM command line option –cp or –classpath while running your application.
- If two classes with same name exist in Java Classpath then the class which comes earlier in Classpath will be picked by Java Virtual Machine.
- By default CLASSPATH in Java points to current directory denoted by "." and it will look for any class only in current directory.
- When you use the -jar command line option to run your program as an executable JAR, then the Java CLASSPATH environment variable will be ignored, and also the -cp and -classpath switches will be ignored and In this case you can set your java classpath in the META-INF/MANIFEST.MF file by using the Class-Path attribute.
- In Unix of Linux Java Classpath contains names of directory with colon “:” separated , On Windows Java Classpath will be semi colon “;” separated while if you defined java classpath in Manifest file those will be space separated.
- You can check value of classpath in java inside your application by looking at following system property “java.class.path” System.getProperty("java.class.path")
Class-Path attribute is used to contain classpath inside manifest file. Also make sure that your manifest file must end with a blank line (carriage return or new line) , here is an example of java classpath in manifest file.
Main-Class: com.classpathexample.Demo_Classpath
Class-Path: lib/tibco.jar lib/log4j.jar
- It’s also important to note that path specified in manifest file is not absolute instead they are relative from application jar’s path. For example in above if your application jar file is in C:\test directory you must need a lib directory inside test and tibco.jar and log4j.jar inside that.
- ClassNotFoundException is an Exception and will be thrown when Java program dynamically tries to load a particular Class at Runtime and don’t find that on Java classpath due to result of Class.forName() or loadClass() method invocation.
- NoClassDefFoundError comes when a particular class was present in Java Classpath during compile time but not available during runtime on Classpath in Java.
If you are getting "Error: Could not find or load main class XXX", where XXX is the name of your main class, while running Java program then do this to solve that error :
1) If you are running Java program right from the directory where .class file is and you have CLASSPATH environment variable defined then make sure it include current directory . (dot). You can include it as set CLASSPATH=%CLASSPATH%;. in Windows and export CLASSPATH = ${CLASSPATH}:. (see the separator, in Windows its ;(semi colon) while in Linux it is (colon), also note we have included current directory in existing classpath. If you still face the issue on setting classpath, see this step by step guide to set classpath. Same thing applies if you are running your program using -cp or -classpath option.
2) If you are running Java program from the directory your .class file is and you don't have any CLASSPATH or -cp option then check whether your class is in package or not. If its in package then go outside of the package directory and run java command with fully qualified name e.g. if your program is com.abc package then run following command from parent directory of "com"
java com.abc.HelloWorld
without any classpath hints, Java will look into current directory and search for com\abc\HelloWorld.class in Windows, so if com directory exists in your current directory, your program will run otherwise you will get "Error: Could not find or load main class dto.HelloHP".
3) You can run your Java program from anywhere with the help of proper CLASSPATH or java -cp option as shown below :
java -cp C:\test\;. com.abc.HelloWorld
If you still facing any issue just check whether you have accidentally using CLASSPATH environment variable, you can check this in Windows by running echo %CLASSPATH% command and in Linux by running echo $CLASSPATH. If CLASSPATH is non empty then it will print its value otherwise just echo the same command.
4) If you are running in Java version 1.6 or 1.5, then instead of receiving "Error: Could not find or load main class", you will get Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld. Its only from JDK 1.7 onward we are started receiving this new error. Solution is exactly same, every bit of discussion applies to that case as well. So if you are not able to solve that problem by following steps here, do let me know and I will try to work with you to troubleshoot the problem.
Here is the screenshot of how I tried to reproduce and solve the error as discussed in previous paragraph :
1) If you are running Java program right from the directory where .class file is and you have CLASSPATH environment variable defined then make sure it include current directory . (dot). You can include it as set CLASSPATH=%CLASSPATH%;. in Windows and export CLASSPATH = ${CLASSPATH}:. (see the separator, in Windows its ;(semi colon) while in Linux it is (colon), also note we have included current directory in existing classpath. If you still face the issue on setting classpath, see this step by step guide to set classpath. Same thing applies if you are running your program using -cp or -classpath option.
2) If you are running Java program from the directory your .class file is and you don't have any CLASSPATH or -cp option then check whether your class is in package or not. If its in package then go outside of the package directory and run java command with fully qualified name e.g. if your program is com.abc package then run following command from parent directory of "com"
java com.abc.HelloWorld
without any classpath hints, Java will look into current directory and search for com\abc\HelloWorld.class in Windows, so if com directory exists in your current directory, your program will run otherwise you will get "Error: Could not find or load main class dto.HelloHP".
3) You can run your Java program from anywhere with the help of proper CLASSPATH or java -cp option as shown below :
java -cp C:\test\;. com.abc.HelloWorld
If you still facing any issue just check whether you have accidentally using CLASSPATH environment variable, you can check this in Windows by running echo %CLASSPATH% command and in Linux by running echo $CLASSPATH. If CLASSPATH is non empty then it will print its value otherwise just echo the same command.
4) If you are running in Java version 1.6 or 1.5, then instead of receiving "Error: Could not find or load main class", you will get Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld. Its only from JDK 1.7 onward we are started receiving this new error. Solution is exactly same, every bit of discussion applies to that case as well. So if you are not able to solve that problem by following steps here, do let me know and I will try to work with you to troubleshoot the problem.
Here is the screenshot of how I tried to reproduce and solve the error as discussed in previous paragraph :
- Internal Details of a sample program
commands for compiling and running java program
What happens at compile time?
At compile time, java file is compiled by Java Compiler (It does not interact with OS) and converts the java code into bytecode.
What happens at runtime?
At runtime, following steps are performed: |
Classloader: is the subsystem of JVM that is used to load class files. |
Bytecode Verifier: checks the code fragments for illegal code that can violate access right to objects. |
Interpreter: read bytecode stream then execute the instructions. |
Comments
Post a Comment