Implicit objects

are java objects in jsp which will be created by servlet container. usually if we want to access any data in a class, we have to create a object using new operator and then access the methods on that object. for implicit objects, we can use them directly on jsp pages.
JSP is used in mvc model architecture so i will clearly separates presentation logic with model logic. JSP used as presentation logic which we can use the objects with out creating the instances.

In servlets API, we have to create the objects, calls the methods on the objects. but to use the servlet specific information in JSP, implicit object can be used without creating any object.
I want blog this as part of j2ee tutorials in my blog

1.request
2.response
3.page,session,application
4.pageContext
5.config
6.exception
7.out

1. request implicit Objects:-
request scope is similar to HttpServletRequest object which holds request specific information. request object in jsp is equal to HttpServlet in servlets.
request object is used to persist the request scope parameters, and reqeust meta header information like
2. response implicit Objects:-

response is an similar to HttpServletResponse object in a servlet. Response object holds the information returned to client. This object holds cookies, content and type and http header
3. Page implicit Objects:- page indicates current jsp page and has the scope of jsp page execution

Session implicit Objects:- Session object is HttpSession class in Servlets. Session holds personalized data of a user.
In a real scenarios, Session object is created one for each user

application implicit Objects:- application is global object similar to in Servlets. application objects holds information about webapplication and only one application for each webapplication
4
PageContext implicit Objects:- jsp pageContext is object of PageContext in servlets.

5.Config implicit Objects:- Jsp config is instance of ServletConfig class. which holds configuration parameters at servlet level.

Web application can have Config objects in such way that each servlet only one config objects.
6.Exception implicit Objects:- Exception object is instance of Throwable class.

javac compiler

Java Compiler

Javac is java compiler tool developed by Sun framework. Java compiler used to read the java program and compiles this java program into machine readable byte code.Javac tool is shipped as part of JDK software installation and found in bin folder of jdk installed folder. This tool is mostly basic tool to learn for java programming developer. This article talks about the javac tutorials and examples with known issues with javac for the developers who are new to java.


javac command usage:-
javac is a command in java used to compile the java programs, there are three parameters are passed.
javac {options} {sourcejavafile} {commandlinearguments}

Options in javac are standard options provided by jdk framework
sourcejavafile is java program to read and compile
command line arguments are arguments passed to the java program at command line which read by java program at runtime using argument String[] args of main() method.

javac command line options in java:-

Following are the all options for javac command using -help command

C:\work>javac -help
Usage: javac  
Where possible options include:
  -g                         Generate all debugging info
  -g:none                    Generate no debugging info
  -g:{lines,vars,source}     Generate only some debugging info
  -nowarn                    Generate no warnings
  -verbose                   Output messages about what the compiler is doing
  -deprecation               Output source locations where deprecated APIs are used
  -classpath           Specify where to find user class files and annotation processors
  -cp                  Specify where to find user class files and annotation processors
  -sourcepath          Specify where to find input source files
  -bootclasspath       Override location of bootstrap class files
  -extdirs             Override location of installed extensions
  -endorseddirs        Override location of endorsed standards path
  -proc:{none,only}          Control whether annotation processing and/or compilation is done.
  -processor [,,...]Names of the annotation processors to run; bypasses default discovery process
  -processorpath       Specify where to find annotation processors
  -d              Specify where to place generated class files
  -s              Specify where to place generated source files
  -implicit:{none,class}     Specify whether or not to generate class files for implicitly referenced files
  -encoding        Specify character encoding used by source files
  -source           Provide source compatibility with specified release
  -target           Generate class files for specific VM version
  -version                   Version information
  -help                      Print a synopsis of standard options
  -Akey[=value]              Options to pass to annotation processors
  -X                         Print a synopsis of nonstandard options
  -J                   Pass  directly to the runtime system


Compile java file using javac:-
D:\work>javac First.java
First.java is read by java compiler and complies into First. Class and place this file in current directory.
Frequent important options for java coder.
How to set Classpath with javac:-
usually javac with classpath settings are configured to have dependency of class for the compiled java class.
classpath is environment settings configuration value that are used by java runtime system to find the classes and other java related objects when java programs are running. Usually CLASSPATH is configured at machine level as well as inline classpath settings. At machine level, we can configure using environment variable CLASSPATH and inline classpath settings are configured by supplying -cp or -classpath option to jdk tools like javac.
We can set classpath in javac with classes as well as jar. Configuring javac classpath in windows and javac classpath in linux environments are same.

javac classpath example :-
C:\javac -classpath .;d:/project or 
C:\javac -cp .;d:/project
we can also set classpath at command prompt using the below command.javac compiled directory is current directory in the above example.
C:\set CLASSPATH=.;pathtodirectoryforclasses or jars;
classpath value should be either current directory (representing .) or any other directory which points to .class files, jar and zip files.
classpath default settings are current directory. if we set CLASSPATH using javac -classpath commandline options overrides the default directory.
javac sourcepath option :-
Whenever java programs are complied, javac looks for the source of java files in the current directory from where javac is executing. if javac wants to search for the java source files other than current directory, we have to provide source path options to javac.
C:\javac -sourcepath c:\javawork\javafiles;.; MyFirstProgram.java
javac source option :-
javac can also be used to compile the java programs with supporting lower versions of jdk version using -source option. With this option, we can always configure the options to have the java source code compatibility for the desired java releases. This option is flexibility to have lower version of java releases for java source only.
C:\javac -source 1.4 MyFirstProgram.java
javac target option :- By using target option in javac, compiled classes are combatable to the jdk versions releases. Once .class files are generated with this option , those are combatable to particular jvm only
Javac Known issues for basic java programmer:-

For the first time user, when we are compiling java program, we used to get a lot of issues. i am listing down the two important issues that i have faced while learning java
Fix for Javac' is not recognized as an internal or external command, operable program or batch file issue in java:-

javac is the tool available jdk software, once jdk is installed say(d:\jdk1.6), javac.exe is available in d:\jdk1.6\bin location.
Suppose you are running javac command from different directory say d:\code, with command as javac First.java, it will give error "Javac' is not recognized as an internal or external command, operable program or batch file" in java.
javac should available in the directory where java files are compiling.
We can fix this in many ways by using inline path settings or environment variable settings.

javac -classpath settings:-

d:\code\ set path=%path%;.;d:\jdk1.6\bin;
Configure environment variable path for java in windows:-
when java is installed, windows doest not know where javac is located, we have to set environment variables path pointed to bin folder.
Go to My Computer, and right click on it and select properties, go to advanced tab, add Path environment variable which point to location of d:\jdk1.6\bin
path=%path%;.;d:\jdk1.6\bin;
Once these settings are configured, we can able to compile in windows system from any location
javac: file not found: issue in java:-
"javac: file not found" is one of other error that used to get for basic java developer. This error is
C:\Work>javac MyFirstProgram.java
javac: file not found: first.java
Usage: javac  
use -help for a list of possible options

When we compile java file with javac command, the above error is thrown in the system console. javac is working fine no issues with javac settings.
But the possible solution is that there is no java file located in the current directory.
The solution is to find the java file in the directory using dir command in windows or ls command with Linux, if java file is not there, give the complete path of the java as input to javac compiler. Hope it works and compiles fine.

Please leave a comment if you like this.

How to fix for java.lang.NoClassDefFoundError error in java


fix for NoClassDefFoundError in java
NoClassDefFoundError in java is one of the strange error used to get in java applications.
Usually when we run java programs, we used to get the Exception in thread “main” java.lang.NoClassDefFoundError: in our programs I got this exception alot of times. Solving this NoClassDefFoundError is very easy but we have to know the ways to solve this. NoClassDefFoundError exception will be in different cases like windows,linux,unix and tomcat/websphere,jboss. I am going to explains about the sample program assumes that which throws NoClassDefFoundError  and fix for this error.


public class HelloWorldDemo {
 public static void main(String args[]) {
  System.out.println("Hello world test message");
 }
}

This is sample hello world program,When i compile this program using javac HelloWorld.java command, this compiles fine and generates HelloWorld.class in the current directory,

After running this programm using java HelloWorld command, I am getting the below exceptions.

Exception in thread "main" java.lang.NoClassFoundError: HelloWorld


thread main throws this error and exit the program abnormally.

This reason for this error is java virtual machine can not find class file at run time. java command looks for the classes that are there in the current directory, so if your class file is not in current directory, you have to set in classpath, so the solution is to place this .class file in the classpath

classpath is the enviornment variable in every system which points to class files in the directories. if you classfile is in jar file, jar should be in classpath. classpath can be absolute(complete path) or relative path( related to directory )


Many ways to solve java.lang.NoClassDefFoundError exception.
Following are the different cases where this error is occured and fix for them.

1. solve java.lang.NoClassDefFoundError :-

HelloWorld.class is not avialble at runtime, so we have to set the class file to java command using -classpath option

java -classpath . HelloWorld

This is for fixing NoClassDefFoundError error by setting classpath inline for java command.

We are instructing the jvm to look for the HelloWorld.class in the current directory by specifying .

if class file is in different directory, we need specify the complete directory absolute or relative path instead of . for java command

2. Fix for java.lang.NoClassDefFoundError in windows:-

To solve NoClassDefFoundError error in windows , we have to set CLASSPATH environment variable.

to set classpath in windows, we have to configure the below values

set CLASSPATH=%CLASSPATH%;.;

%CLASSPATH% means existing classpath to be added and . points to current directory

After setting classpath,
java HelloWorld
command works fine and prints hello world message

3. Fix for java.lang.NoClassDefFoundError in linux/unix:-

This is like setting classpath in linux
so we have to set the class or directory of the class files to classpath environment variable.

set $CLASSPATH=$CLASSPATH%;.;

After setting classpath in linux,
java HelloWorld
command works fine and prints hello world message

4. Fix for java.lang.NoClassDefFoundError in eclipse:-
java.lang.NoClassDefFoundError in eclipse usually ocurrns that your project is missing some of required jars in your build path.
we have to first configure your project build path.
we can configure by right click on project-->properties-->java build path--> select libraries for jars or for classes in folder select source..

5. Fix for java.lang.NoClassDefFoundError error for jar files:-

and also we found that when we are running java class file presented in jar file, java.lang.NoClassDefFoundError error occurs, sho you have to set classpath in MANIFEST.MFin eclipse:-

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.2
Created-By: 1.6.0-beta2-b86 (Sun Microsystems Inc.)
Implementation-Title: 
Implementation-Version: 3.0
Implementation-Vendor: 
Class-Path: exntension.jar
class-path attribute value jar is missing in your classpath, so we have to set the jar to the classpath

5. Fix for java.lang.NoClassDefFoundError error in tomcat,jboss,websphere-
NoClassDefFoundError error in servers occurs because of the jar is not in classpath. To fix this, see the complete stack trace and find the jar which contains jar and tries to copy in the server classpath or application classpath. Please leave a comment if you are seeing any issues with servers.


we have seen that NoClassDefFoundError throws at runtime but what about java.lang.ClassNotFoundException error. both looks similiar and related to classpath only but ClassNotFoundException exception throws application tries load the class using name of the class or class calling other class which is not loaded.

what is java.lang.ClassNotFound Exception : classnot found exception is thrown when classes are loaded by different classloaders at runtime. here child class which load in parent class is not available for class loader to load the class. then what is different between java.lang.NoClassDefFoundError and java.lang.ClassNotFoundException? we will see in the below paragraph

Difference between ClassNotFoundException and NoClassDefFoundError?
Both are unable to load at run time,
NoClassDefFoundError errors thrown if .class file cannot be found,
where as ClassNotFoundException error occurs if only when you try to load a class by string name by ClassLoader using class.forName or Class loader's findSystemClass method or LoadClass in ClassLoader.

Hope you understand the fix to solve NoClassDefFoundError in java. Please leave a comment and share this.

Static Import in java 5:-

static import is java language feature introduced in java version 5 alias code name as tiger version. This was introduced to simplify the access of static variables and static members without using classname.Before static import is introduced, we have normal import statetements for every java class to use the methods and classes with using class name

How the java code looks like before static import feature is introduced?

Normally in java coding, if we want to use the any static member variables or a method of a class in our code,
we have to first import that class using normal import keyword, after that we have to use variables/methods with class name in the code (Math.E)). below is the example for normal import usage with access the member variables using qualified class name.
import java.lang.*;
public class ImportDemo {
 public static void main(String args[]) {
  System.out.println("This is sample program without using static import functionality" + Math.E);
 }
}

In the above program, we have used System and Math classes are used from java.lang package. In java program, we don't need to import any java.lang package as default package imported is java.lang package for every program. i have written in the above program and imported java.lang package for our this tutorial purpose

The basic difference between normal import and static import is normal import can call the member variables with class name in the package.
static import can be defined and static member fields can not refere any class name and use directly without classname.


static import usage example:-

Now we will see the example for static import feature which we can use the static member variables of java.lang.System and java.lang.Math classes without qualified class name.

import static java.lang.System.*;
import static java.lang.Math.*;
public class ImportDemo {
 public static void main(String args[]) {
  out.println("This is sample program without using static import functionality" + E);
 }
}
out is static field in java.lang.System class and E is static member variable for java.lang.Math class

For using static member variable E in Math class in java.lang Package, we have coded by adding static import something like import static java.lang.Math.*; or java.lang.Math.E. It will always good idea to define for each static member(Math.E) instead of all static members(Math.*) in static import declaration

Advantages of Static import feature:-

To call the static members without using class name in our code. By making static import code statments makes all the static members of that class are avilable to declared java class
Static import can be used for static member variables as well as static members without refering class name in java code. It simplifies the code readablity for the constants.

Disadvantages of Static import feature:-

if we are importing the two same static constansts from different classes in the same class. there is possiblity of ambiguity for the compilier to throw error.

Please let us know your comments on this.