How set CLASSPATH variable for a folder in Ubuntu
First, in general, setting the env var CLASSPATH usually causes more problems than it solves — since not all app’s want/need the same classpath, & often break when undesired or even unneeded jars are included in the classpath. A java app should only include the minimum number of jars it requires, no more, no less.
When you have specific, individual apps that do require that the classpath be set, then usually the command-line option is preferred: java -cp path1:path2. . Desktop icons can have their command altered to include these options, or shell scripts can be modified to include these options.
That being said (and since there are always exceptions to the rule), then depending on the version of java (this requres java 6 or later), you can specify that a whole directory of jars be added to the classpath by adding a » * » at the end of a directory; e.g, the following:
/dir1/foo.jar:/dir2/dir3:/dir5/dir6/*:etc.
- /dir1/foo.jar — (the single jar) will be added to the classpath;
- /dir2/dir3 — all un-jar’d classes in this directory will be added to the classpath (must be in proper package structure; e.g, com.my.Foo.class must be in /dir2/dir3/com/my/Foo.class )
- /dir5/dir6/* — all jars in this directory (i.e., /dir5/dir6/*.jar ) will be added to the classpath. Note that this » * » isn’t a wildcard (you can’t use f*.jar or even *.jar ); it’s a special character indicating «add all jars»
In general, if you have to add a whole directory of jars to the application’s classpath, the app was not bundled correctly. Rather, the app should have a manifest containing the list of jars it depends on. Or at the very least, only one jar should be added to your classpath, and that jar can have in its manifest the whole list of jars in some subdirectory.
PATH and CLASSPATH
This section explains how to use the PATH and CLASSPATH environment variables on Microsoft Windows, Solaris, and Linux. Consult the installation instructions included with your installation of the Java Development Kit (JDK) software bundle for current information.
After installing the software, the JDK directory will have the structure shown below.
The bin directory contains both the compiler and the launcher.
Update the PATH Environment Variable (Microsoft Windows)
You can run Java applications just fine without setting the PATH environment variable. Or, you can optionally set it as a convenience.
Set the PATH environment variable if you want to be able to conveniently run the executables ( javac.exe , java.exe , javadoc.exe , and so on) from any directory without having to type the full path of the command. If you do not set the PATH variable, you need to specify the full path to the executable every time you run it, such as:
C:\Java\jdk1.7.0\bin\javac MyClass.java
The PATH environment variable is a series of directories separated by semicolons ( ; ). Microsoft Windows looks for programs in the PATH directories in order, from left to right. You should have only one bin directory for the JDK in the path at a time (those following the first are ignored), so if one is already present, you can update that particular entry.
The following is an example of a PATH environment variable:
C:\Java\jdk1.7.0\bin;C:\Windows\System32\;C:\Windows\;C:\Windows\System32\Wbem
It is useful to set the PATH environment variable permanently so it will persist after rebooting. To make a permanent change to the PATH variable, use the System icon in the Control Panel. The precise procedure varies depending on the version of Windows:
- Select Start, select Control Panel. double click System, and select the Advanced tab.
- Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New .
- In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.
- From the desktop, right click the My Computer icon.
- Choose Properties from the context menu.
- Click the Advanced tab (Advanced system settings link in Vista).
- Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New .
- In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.
- From the desktop, right click the Computer icon.
- Choose Properties from the context menu.
- Click the Advanced system settings link.
- Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New .
- In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.
Note: You may see a PATH environment variable similar to the following when editing it from the Control Panel:
%JAVA_HOME%\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem
Variables enclosed in percentage signs ( % ) are existing environment variables. If one of these variables is listed in the Environment Variables window from the Control Panel (such as JAVA_HOME ), then you can edit its value. If it does not appear, then it is a special environment variable that the operating system has defined. For example, SystemRoot is the location of the Microsoft Windows system folder. To obtain the value of a environment variable, enter the following at a command prompt. (This example obtains the value of the SystemRoot environment variable):
Update the PATH Variable (Solaris and Linux)
You can run the JDK just fine without setting the PATH variable, or you can optionally set it as a convenience. However, you should set the path variable if you want to be able to run the executables ( javac , java , javadoc , and so on) from any directory without having to type the full path of the command. If you do not set the PATH variable, you need to specify the full path to the executable every time you run it, such as:
% /usr/local/jdk1.7.0/bin/javac MyClass.java
To find out if the path is properly set, execute:
This will print the version of the java tool, if it can find it. If the version is old or you get the error java: Command not found, then the path is not properly set.
To set the path permanently, set the path in your startup file.
For C shell ( csh ), edit the startup file (~/.cshrc ):
set path=(/usr/local/jdk1.7.0/bin $path)
For bash , edit the startup file ( ~/.bashrc ):
PATH=/usr/local/jdk1.7.0/bin:$PATH export PATH
For ksh , the startup file is named by the environment variable, ENV . To set the path:
PATH=/usr/local/jdk1.7.0/bin:$PATH export PATH
For sh , edit the profile file ( ~/.profile ):
PATH=/usr/local/jdk1.7.0/bin:$PATH export PATH
Then load the startup file and verify that the path is set by repeating the java command:
% source ~/.cshrc % java -version
Checking the CLASSPATH variable (All platforms)
The CLASSPATH variable is one way to tell applications, including the JDK tools, where to look for user classes. (Classes that are part of the JRE, JDK platform, and extensions should be defined through other means, such as the bootstrap class path or the extensions directory.)
The preferred way to specify the class path is by using the -cp command line switch. This allows the CLASSPATH to be set individually for each application without affecting other applications. Setting the CLASSPATH can be tricky and should be performed with care.
The default value of the class path is «.», meaning that only the current directory is searched. Specifying either the CLASSPATH variable or the -cp command line switch overrides this value.
To check whether CLASSPATH is set on Microsoft Windows NT/2000/XP, execute the following:
On Solaris or Linux, execute the following:
If CLASSPATH is not set you will get a CLASSPATH: Undefined variable error (Solaris or Linux) or simply %CLASSPATH% (Microsoft Windows NT/2000/XP).
To modify the CLASSPATH , use the same procedure you used for the PATH variable.
Class path wildcards allow you to include an entire directory of .jar files in the class path without explicitly naming them individually. For more information, including an explanation of class path wildcards, and a detailed description on how to clean up the CLASSPATH environment variable, see the Setting the Class Path technical note.
Previous page: Miscellaneous Methods in System
Next page: Questions and Exercises: The Platform Environment
adding classpath in linux
I don’t like setting CLASSPATH. CLASSPATH is a global variable and as such it is evil:
- If you modify it in one script, suddenly some java programs will stop working.
- If you put there the libraries for all the things which you run, and it gets cluttered.
- You get conflicts if two different applications use different versions of the same library.
- There is no performance gain as libraries in the CLASSPATH are not shared — just their name is shared.
- If you put the dot (.) or any other relative path in the CLASSPATH that means a different thing in each place — that will cause confusion, for sure.
Therefore the preferred way is to set the classpath per each run of the jvm, for example:
java -Xmx500m -cp ". /somejar.jar. /mysql-connector-java-5.1.6-bin.jar" "folder.subfolder../dit1/some.xml
If it gets long the standard procedure is to wrap it in a bash or batch script to save typing.
the cmd that i have used export classpath= java -Xmx500m -cp «. /somejar.jar. /mysql-connector-java-5.1.6-bin.jar» «folder.subfolder../dit1/some.xml
Throw the export part away! Just: java -Xmx500m -cp «. /somejar.jar. /mysql-connector-java-5.1.6-bin.jar» folder.subfolder ../dit1/some.xml
«global variables» are not evil. They are there to be inherited. You can override the setting in your command line «local» version with no problems. More to the point this «global variable» as you term it is important for running java apps you might have installed via synaptic/aptitude/apt-get. Want localised versions? Use them.
It’s always advised to never destructively destroy an existing classpath unless you have a good reason.
The following line preserves the existing classpath and adds onto it.
export CLASSPATH="$CLASSPATH:foo.jar. /bar.jar"
Better advice would be not to use the CLASSPATH environment variable at all. Use «-cp» instead, and write wrapper scripts or shell aliases to save typing.
Important difference between setting Classpath in Windows and Linux is path separator which is «;» (semi-colon) in Windows and «:» (colon) in Linux. Also %PATH% is used to represent value of existing path variable in Windows while $ is used for same purpose in Linux (in the bash shell). Here is the way to setup classpath in Linux:
but as such Classpath is very tricky and you may wonder why your program is not working even after setting correct Classpath. Things to note:
- -cp options overrides CLASSPATH environment variable.
- Classpath defined in Manifest file overrides both -cp and CLASSPATH envorinment variable.
Paths under linux are separated by colons ( : ), not semi-colons ( ; ), as theatrus correctly used it in his example. I believe Java respects this convention.
Edit
Alternatively to what andy suggested, you may use the following form (which sets CLASSPATH for the duration of the command):
CLASSPATH=". /somejar.jar. /mysql-connector-java-5.1.6-bin.jar" java -Xmx500m .
whichever is more convenient to you.
For linux users, and to sum up and add to what others have said here, you should know the following:
- Global variables are not evil. $CLASSPATH is specifically what Java uses to look through multiple directories to find all the different classes it needs for your script (unless you explicitly tell it otherwise with the -cp override).
- The colon («:») character separates the different directories. There is only one $CLASSPATH and it has all the directories in it. So, when you run «export CLASSPATH=. » you want to include the current value «$CLASSPATH» in order to append to it. For example:
export CLASSPATH=. export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java-5.1.12.jar