Skip to main content

How to setup and use system classpath (CLASSPATH)?

--
Some utility code that you have developed or have obtained from a third party can usually be used in more than one Java program. These may sometimes be stored as packages, just as the Java utilities are in packages such as java.util.*. Whether or not they are packages or 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 (which is a file using zip format with a .jar extension). In order to make this work, the Java compiler and JVM must be able to find them.

Rather than specifying class search path on the javac command line by using the -classpath, we can make use of a 'system' class path. The CLASSPATH is an environment variable , in both Unix and Window systems, which will be used by both the Java compiler and the JVM to determine where to look for classes referenced by a Java program. Setting the system CLASSPATH is a useful procedure if you have JARs full of classes that you use all the time. The CLASSPATH environment variable can specify the location of classes in directories and in JAR files.


For example, We have myJar1.jar and myJar2.jar files under myLibs directory. In Windows, the CLASSPATH environment variable should look like the following:
 
CLASSPATH=c:\myLibs\myJar1.jar;c:\myLibs\myJar2.jar;.

where the `.' indicates `current directory'. This includes the current directory in the search for classes.

Specification order

The order in which you specify multiple class path entries is important. The Java interpreter will look for classes in the directories in the order they appear in the class path variable.

What do not need to include in the CLASSPATH

In the CLASSPATH, you do not need to specify the location of normal J2SE packages and classes such as java.util or java.io.IOException.

You also do not need an entry in the CLASSPATH for packages and classes that you place in the ext directory (normally found in a directory such as C:\j2sdk\jre\lib\ext). Java will automatically look in that directory. So, if you drop your JAR files into the ext directory or build your directory structure off the ext directory, you will not need to do anything with setting the CLASSPATH. But this directory was intended for official Java extensions from Sun. Its use by unofficial Java extensions isn't recommended.

Example for Setting the CLASSPATH in Microsoft Windows XP

To configure a persistent CLASSPATH under Windows XP:
  1. Click the Start Button
  2. Select the Control Panel
  3. Click Switch to Classic View
  4. Click the System button
  5. Select the Advanced tab
  6. Click the Environment Variables button
  7. Click the New button to create a CLASSPATH or the Edit button to edit an existing CLASSPATH
  8. Enter CLASSPATH as the variable name
  9. Enter the list of directories or jar files in your CLASSPATH as the variable value, using a semicolon to separate entries in your list
  10. Click OK
  11. Click OK
  12. Click OK

Combine CLASSPATH  with "-classpath" option

It is easy to overlook that the -classpath option on the command line replaces the default, system class path; it does not add to it. So what should I do if I want to set the class path to include the default system classpath plus some other entries? I could simply use the -classpath option and list the default entries in addition to my extras. However, a better way is to reference the CLASSPATH environment variable. The syntax for this is different -- of course -- on Windows and Unix systems.

On Unix:
javac -classpath $CLASSPATH:dir1:dir2 ...
where $CLASSPATH expands to the current setting of the CLASSPATH environment variable.

On Windows:
javac -classpath %CLASSPATH%;dir1;dir2 ...

Finally, please note that if directories in your class search path have spaces in their names, you may have to use double-quotes on the command line to prevent the CLASSPATH being split up. For example:
javac -classpath "%CLASSPATH%";dir1;dir2 ...

Comments

Popular posts from this blog

Advantages & Disadvantages of Synchronous / Asynchronous Communications?

  Asynchronous Communication Advantages: Requests need not be targeted to specific server. Service need not be available when request is made. No blocking, so resources could be freed.  Could use connectionless protocol Disadvantages: Response times are unpredictable. Error handling usually more complex.  Usually requires connection-oriented protocol.  Harder to design apps Synchronous Communication Advantages: Easy to program Outcome is known immediately  Error recovery easier (usually)  Better real-time response (usually) Disadvantages: Service must be up and ready. Requestor blocks, held resources are “tied up”.  Usually requires connection-oriented protocol

WebSphere MQ Interview Questions

What is MQ and what does it do? Ans. MQ stands for MESSAGE QUEUEING. WebSphere MQ allows application programs to use message queuing to participate in message-driven processing. Application programs can communicate across different platforms by using the appropriate message queuing software products. What is Message driven process? Ans . When messages arrive on a queue, they can automatically start an application using triggering. If necessary, the applications can be stopped when the message (or messages) have been processed. What are advantages of the MQ? Ans. 1. Integration. 2. Asynchrony 3. Assured Delivery 4. Scalability. How does it support the Integration? Ans. Because the MQ is independent of the Operating System you use i.e. it may be Windows, Solaris,AIX.It is independent of the protocol (i.e. TCP/IP, LU6.2, SNA, NetBIOS, UDP).It is not required that both the sender and receiver should be running on the same platform What is Asynchrony? Ans. With messag

XML Binding with JAXB 2.0 - Tutorial

Java Architecture for XML Binding (JAXB) is an API/framework that binds XML schema to Java representations. Java objects may then subsequently be used to marshal or unmarshal XML documents. Marshalling an XML document means creating an XML document from Java objects. Unmarshalling means creating creating a Java representation of an XML document (or, in effect, the reverse of marshaling). You retrieve the element and attribute values of the XML document from the Java representation. The JAXB 2.0 specification is implemented in JWSDP 2.0. JAXB 2.0 has some new features, which facilitate the marshalling and unmarshalling of an XML document. JAXB 2.0 also allows you to map a Java object to an XML document or an XML Schema. Some of the new features in JAXB 2.0 include: Smaller runtime libraries are required for JAXB 2.0, which require lesser runtime memory. Significantly, fewer Java classes are generated from a schema, compared to JAXB 1.0. For each top-level complexType, 2.0 generates a v