Showing posts with label classpath. Show all posts
Showing posts with label classpath. Show all posts

Monday, 4 July 2011

Java package

Package Definition

A package is a collection of related classes.
It helps Organize your classes into a folder structure and make it easy to locate and use them.
More importantly,It helps improve re-usability.

Syntax:-
package <package_name>;

Package declaration

The first statement, other than comments, in a Java source file, must be the package declaration.
Following the optional package declaration, you can have import statements, which allow you to specify classes from other packages that can be referenced without qualifying them with their package.

Default package. Altho all Java classes are in a directory, it's possible to omit the package declaration. For small programs it's common to omit it, in which case Java creates what it calls a default package. Sun recommends that you do not use default packages.

Example of using package

Example1 : Creating the package:
package p1;
class c1{
public void m1(){
System.out.println("Method m1 of Class c1");
}
public static void main(String args[]){
c1 obj = new c1();
obj.m1();
}
}

Example 2 : Creating the sub packages:
package p1.p2;
class c2{

public void m2(){
System.out.println("Method m2 of Class c2");
}
public static void main(String args[]){
c2 obj = new c2();
obj.m2();
}
}


Compiling the above Classes

When a class is placed in a package the class file must be located in a sub-directory under one of the directories listed in the CLASSPATH. The sub-directory structure must map exactly onto the package name.
Given a CLASSPATH=/lib the above class must reside in one of:
./p1 
/lib/p1

Or  in case of example2 :
./p1/p2
or /lib/p1/p2

The javac compiler creates class files in the current directory by default, even if they are specified as belonging to a package. To get around this, use the -d flag to the compiler . Which will create the necessary directories under the /lib directory, if your classpath is /lib.

Take example1, Save the file as Demo.java.
Compile the file as,
javac – d . Demo.java
Run the code as
java p1.c1

Taking example2:
Save the file as Demo2.java.
Compile the file as
javac – d . Demo2.java

Run the code as
java p1.p2.c2

Importing a package


To create an object  of a class (bundled in a package), in your code, you have to use its fully qualified name.
Ex.
java.awt.event.actionListner object = new java.awt.event.actionListner();

But , it could become tedious to type in the long dot-separated package path name for every class you want to use. Instead it is recommended you use the import statement.

Syntax
import <package_name>;

Once imported , you can use the class without mentioning its fully qualified name.

import java.awt.event.*; // * signifies all classes in this package
import javax.swing.JFrame // here only the JFrame class is imported
//Usage
JFrame f = new JFrame; // without fully qualified name.

To import package

// Using packages created in earlier assignment
package p3;
import p1.*; //imports classes only in package p1 and NOT in the sub-package p2
&nbsp;
class c3{
public void m3(){
System.out.println("Method m3 of Class c3");
}
public static void main(String args[]){
c1 obj1 = new c1();
obj1.m1();
p1.p2.c2 obj2 = new p1.p2.c2();
obj2.m2();
}
}

Save the file as Demo2.java . Compile the file using the command
javac –d .Demo2.java

Execute the code using the command
java p3.c3

Packages - points to note:


  • To avoid naming conflicts packages are given names of the domain name of the company in reverse Ex :  com.guru99. com.microsoft, com.infosys etc.
  • When a package name is not specified , a class is into the default package (the current working directory) and the package itself is given no name. Hence you were able to execute assignments earlier.
  • While creating a package, care should be taken that the statement for creating  package must be written before any other import statements
  • When importing packages with * format, only classes in that packages are imported and not sub packages.

Sunday, 3 July 2011

Wildcard in Java classpath

One of the less known new features of JDK 6 is the ability to use wildcards in classpaths.
That means that instead of doing…
java -cp lib\x.jar;lib\y.jar;lib\z.jar; com.company.MyProggy
…you could just do…
java -cp lib\*.jar; com.company.MyProggy
That generally turns out to be quite a time saver. Especially so if you are trying out something at a remote box and vi is your only editor.
However, if you use the wild card, there is no way to predict the order in which the jars will be used. In some cases, the order is important – and in those cases, you will need to add in the jars like one normally does by enumerating each one explicitly.
Also, don’t forget the semi colon at the end of the classpath. Sometimes lots of time is lost due to this. It can be really frustrating!

java.lang.NoClassDefFoundError in java

This is normally a CLASSPATH problem. Try resetting the CLASSPATH, and/or make sure all third party JAR's are actually present in the correct locations. In some rare cases you will get this problem when the JVM has problems loading a third party jar due to version incompatibilities.

To set classpath in java, just see how to reset it. Following links may be useful to read:

Saturday, 23 April 2011

Setting path and classpath for java in windows XP, Vista and Windows 7

  • Click the Start button in the lower left of the screen.
  • Select Control Panel from the pop-up menu.
  • Choose System from the submenu.(Click Advanced system settings on left in Vista and Windows 7)
  • Click the Advanced tab.
  • Click the Environment Variables button near the bottom and you will see two lists of variables.
  • Look in the System variables list for a variable named CLASSPATH. If you find it, click Edit. If you don't find it, click New and enter CLASSPATH in the Variable name field.
  • The Variable value field is a list of file paths of directories or jar files. The first thing in this list should be the current directory, which is represented in windows just as it is in Unix, as a single period. If there's more than one thing in this list, separate items with a semicolon. For example, my CLASSPATH variable starts with these three items (there are a couple more, but this should be enough to give you the idea). The only part you need is the first "dot".
    .;C:\classpath\com.fredswartz.ezgui.jar;c:\classpath\TableLayout.jar;
    I put extra libraries that I want to be searched in a directory named classpath, but you can choose any name. 
  • Now just add the following depending on your environment installed on computer. eg.:
    D:\Program Files\Java\jdk1.6.0_24\bin
Similar way go for path as well.

Determining the current values of PATH and CLASSPATH

Unix

Type these commands in a command window:
echo $PATH
echo $CLASSPATH
If you get a blank command line in response to either of these, then that particular variable has no value (it has not yet been set).

Windows

Type these commands in a command window:
echo %PATH%
echo %CLASSPATH%
If you get the message "echo is on" for either of these, then that particular variable has no value (it has not yet been set).

Wednesday, 20 April 2011

Path and classpath in java

For running a simple program we need to set the java path on command prompt(for temporary )& in Environment variable using PATH variable & CLASSPATH variable :

PATH variable 
In JDK the PATH variable contains directories where binary files (e.g. EXE files in Windows) will be looked for.We set the PATH variables like this i.e path C:\Java\jdk1.6.0_03\bin 

(i) On command prompt
C:\>set path=%path;C:\Java\jdk1.6.0_03\bin%
When you open a command prompt and type "javac", you're supposed to have the "bin" directory of your sdk into the PATH, otherwise you'll get an infamous "Command not found" error message.

CLASSPATH 
In JDK the CLASSPATH contains directories (or JAR files), from where your java compiler/runtime will look for .class files (and some others). For example, "java Hello.class" will not work unless you set the directory (or JAR file) Hello.class is in, into your CLASSPATH.
i.e.classpath  C:\Java\jdk1.6.0_03\lib
For setting CLASSPATH using command prompt  Java class path can be set using either the -classpath option when calling an SDK tool (the preferred method) or by setting the CLASSPATH environment variable. The -classpath option is preferred because you can set it individually for each application without affecting other applications and without other applications modifying its value.

On command prompt 
C:\>set classpath=%classpath;C:\Java\jdk1.6.0_03\lib%

JARs on the classpath
Java compiler and run-time can search for classes not only in separate files, but also in `JAR' archives. A JAR file can maintain its own directory structure, and Java follows exactly the same rules as for searching in ordinary directories. Specifically, `directory name = package name'. Because a JAR is itself a directory, to include a JAR file in the class search path, the path must reference the JAR itself, not the directory that contains the JAR. This is a very common error. Suppose I have a JAR jarclasses.jar in directory /jarclasses. The Java compiler look for classes in this jar, we need to specify: javac -classpath /jarclasses/jarclasses.jar ...
and not merely the directory jarclasses.  

For the CLASSPATH  use  CLASSPATH Environment Variable 
In java programming language we use the following packages such as  java.io.*;  java. util.*;These  packages are  just a set of classes, you will want to store them in one place, separate from the directory where you are developing your program. Sometimes these libraries have a set of files stored in a single jar file ( with a .jar extension). From where  the Java compiler and run programs must be able to find any of these. This is done for command line use of Java and for some editors and IDEs by setting an environment variable called CLASSPATH. For  Windows, the CLASSPATH environment variable should look like this :
c:\MyJavaLib;c:\MyJavaLib\myutils.jar;c:\MyJavaLib\blackboxclasses.jar;.
At the end " . "  includes the current directory in the search for classes.
  • Suppose we have a directory MyJavaLib on the C-drive that contains some utility classes or the directories that correspond to some packages. This  is the part before the first semi-colon.
               
  • Second part indicates that we have some classes stored in a file myutils.jar
             
  • Third part indicates that we have a jar file blackboxclasses.jar. 
Finally, after the last semi-colon we have the period( . ), indicating that the current directory is on the CLASSPATH. If some things are stored in directories that are subdirectories, the complete path, starting with "c:\" should be in the CLASSPATH.

Setting classpath in windows 7,Vista and xp graphically

Tuesday, 19 April 2011

Significance of searching class path in java

Java is an OOP language. In java programming language for compiling the program we use the compiler that converts the source code into the byte code after that, that byte code is interpreted by JVM that converts the bytecode into the machine independent code. In java how the Java compiler and the JVM use the class search path to locate classes when they are referenced by other Java code. Searching class path is important for all Java developers.Many development tools have their own ways of manipulating the class path, which vary from product to product.For doing this we will use only simple command-line tools to carry out the compile operations. No difference, between the way that the Java compiler searches for classes, and the way that the JVM does it at run time. The compiler has the ability to compile classes from source code, where the JVM does not. We will use the compiler, but similar issues apply at run time.

Searching of multiple class directories

  • javac (compiler) search the files in only one directory at a time.
  • But, class search path will contain numerous directories and JAR archives. The -classpath option to javac and java allows multiple entries to be specified, but the syntax is different for Unix and Windows systems.

For Unix like this :
javac -classpath dir1:dir2:dir3 ...

For Windows like this:
javac -classpath dir1;dir2;dir3 ...

The difference is that Windows uses the colon (:) character as part of a filename, so it can't be used as a filename separator. Naturally the directory separator character is different as well: forward slash (/) for Unix and backslash (\) for Windows.