Friday, February 5, 2016

How to set path in Java

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:
  1. temporary
  2. 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
Let's see it in the figure given below:

how to set path in java

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

Tuesday, February 2, 2016

Classpath

Java
Classpath is a parameter in the Java Virtual Machine or the Java compiler that specifies the location of user-defined classes and packages. The parameter may set either on the command-line, or through an environment variable


Setting the path to execute Java programs

Supplying as application argument

Suppose we have a package called org.mypackage containing the classes:
  • HelloWorld (main class)
  • SupportClass
  • UtilClass
and the files defining this package are stored physically under the directory D:\myprogram (on Windows) or /home/user/myprogram (on Linux).
The file structure looks like this:



Microsoft Windows
Linux
D:\myprogram\
      |
      ---> org\  
            |
            ---> mypackage\
                     |
                     ---> HelloWorld.class       
                     ---> SupportClass.class   
                     ---> UtilClass.class     
/home/user/myprogram/
            |
            ---> org/  
                  |
                  ---> mypackage/
                           |
                           ---> HelloWorld.class       
                           ---> SupportClass.class   
                           ---> UtilClass.class     


When we invoke Java, we specify the name of the application to run: org.mypackage.HelloWorld. However we must also tell Java where to look for the files and directories defining our package. So to launch the program, we use the following command:

Microsoft Windows
Linux
 java -classpath D:\myprogram org.mypackage.HelloWorld
 java -cp /home/user/myprogram org.mypackage.HelloWorld 
where:
  • java is a java application launcher, a type of sdkTool(A command-line tool, such as javac, javadoc, or apt)
  • -classpath D:\myprogram sets the path to the packages used in the program (on Linux, -cp /home/user/myprogram) and
  • org.mypackage.HelloWorld is the name of the main class

Setting the path through an environment variable

The environment variable named CLASSPATH may be alternatively used to set the classpath. For the above example, we could also use on Windows:

set CLASSPATH=D:\myprogram
java org.mypackage.HelloWorld

The rule is that -classpath option, when used to start the java application, overrides the CLASSPATH environment variable. If none are specified, the current working directory is used as classpath. This means that when our working directory is D:\myprogram\ (on Linux, /home/user/myprogram/), we would not need to specify the classpath explicitly. When overriding however, it is advised to include current folder "." into the classpath in the case when loading classes from current folder is desired.
The same applies not only to java launcher but also to javac, the java compiler.

Setting the path of a Jar file

If a program uses a supporting library enclosed in a Jar file called supportLib.jar, physically in the directory D:\myprogram\lib\ and the corresponding physical file structure is:
D:\myprogram\
      |
      ---> lib\
            |
            ---> supportLib.jar
      |
      ---> org\
            |
            --> mypackage\
                       |
                       ---> HelloWorld.class
                       ---> SupportClass.class
                       ---> UtilClass.class


the following command-line option is needed:

java -classpath D:\myprogram;D:\myprogram\lib\supportLib.jar org.mypackage.HelloWorld
or alternatively:

set CLASSPATH=D:\myprogram;D:\myprogram\lib\supportLib.jar
java org.mypackage.HelloWorld

Adding all JAR files in a directory

In Java 6 and higher, one can add all jar-files in a specific directory to the classpath using wildcard notation.

Windows example:
java -classpath ".;c:\mylib\*" MyApp


Linux example:
java -classpath '.:/mylib/*' MyApp

This works for both -classpath options and environment classpaths.





Thursday, January 28, 2016

Hibernate


Hibernate an open source Java persistence framework project. Perform powerful object relational mapping and query databases using HQL and SQL. Hibernate is a great tool for ORM mappings in java. It can cut down a lot of complexity and thus defects as well from your application, which may otherwise find a way to exist. This is specially boon for developers with limited knowledge of SQL.
In this page, I have categorize all available hibernate tutorials in this blog. This page will be updated every time, a new hibernate tutorial is published in this blog.

Hibernate Framework


Hibernate framework simplifies the development of java application to interact with the database. Hibernate is an open source, lightweight, ORM (Object Relational Mapping) tool.
An ORM tool simplifies the data creation, data manipulation and data access. It is a programming technique that maps the object to the data stored in the database.
hibernate tutorial, An introduction to hibernate
The ORM tool internally uses the JDBC API to interact with the database.



Advantages of Hibernate Framework

There are many advantages of Hibernate Framework. They are as follows:
1) Opensource and Lightweight: Hibernate framework is opensource under the LGPL license and lightweight.

2) Fast performance: The performance of hibernate framework is fast because cache is internally used in hibernate framework. There are two types of cache in hibernate framework first level cache and second level cache. First level cache is enabled bydefault.

3) Database Independent query: HQL (Hibernate Query Language) is the object-oriented version of SQL. It generates the database independent queries. So you don't need to write database specific queries. Before Hibernate, If database is changed for the project, we need to change the SQL query as well that leads to the maintenance problem.

4) Automatic table creation: Hibernate framework provides the facility to create the tables of the database automatically. So there is no need to create tables in the database manually.

5) Simplifies complex join: To fetch data form multiple tables is easy in hibernate framework.


6) Provides query statistics and database status: Hibernate supports Query cache and provide statistics about query and database status.







Wednesday, January 27, 2016

Operators

. Operators are special symbols that perform specific operations on one, two, or three operands, and then return a result.

As we explore the operators of the Java programming language, it may be helpful for you to know ahead of time which operators have the highest precedence. The operators in the following table are listed according to precedence order. The closer to the top of the table an operator appears, the higher its precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence. Operators on the same line have equal precedence. When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.
Operator Precedence
OperatorsPrecedence
postfixexpr++ expr--
unary++expr --expr +expr -expr ~ !
multiplicative* / %
additive+ -
shift<< >> >>>
relational< > <= >= instanceof
equality== !=
bitwise AND&
bitwise exclusive OR^
bitwise inclusive OR|
logical AND&&
logical OR||
ternary? :
assignment= += -= *= /= %= &= ^= |= <<= >>= >>>=

In general-purpose programming, certain operators tend to appear more frequently than others;

for example, the assignment operator "=" is far more common than the unsigned right shift operator ">>>". With that in mind, the following discussion focuses first on the operators that you're most likely to use on a regular basis, and ends focusing on those that are less common. Each discussion is accompanied by sample code that you can compile and  and run. Studying its output will help reinforce what you've just learned

Tuesday, January 26, 2016

Difference between Java and C++ language:

  • According to some experts, Java is pure object oriented programming language while C++ is object based programming language.
  • The code written in Java can run on different platforms whereas this not possible with C++.
  • Java is mainly used for developed applets and e-commerce based applications while C++ is used for developing system software.