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

Monday, July 4, 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, April 23, 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).

Thursday, April 21, 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

Wednesday, April 20, 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.

Chitika