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...
Means:
/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.classmust 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 usef*.jaror 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.
Answer from michael on askubuntu.comFirst, 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...
Means:
/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.classmust 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 usef*.jaror 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.
if you want to set classpath permanently then 1) find out where java is installed.. you may use "whereis java" openjdk-7/6 is in /usr/lib/jvm/.....
2) we need to set up CLASSPATH in /etc/environment
sudo gedit /etc/environment
3) add the following likes .. ( DONT LEAVE ANY SPACES WHILE TYPING)(customize according to your java version and installation) (this home path is for open jdk 7)
JAVA_HOME="/usr/lib/jvm/java-7-openjdk-i386/bin"
export JAVA_HOME
CLASSPATH=".:/usr/lib/jvm/java-7-openjdk-i386/lib:/home/laptop/Desktop/a2"
export CLASSPATH
separate directory by ":"
Videos
For JAR files, you have to set the CLASSPATH and not the PATH environment variable.
If you're using BASH, it is: export CLASSPATH="$CLASSPATH:<full_path_to_each_jar_files>"
You better add it in the file .bashrc unless you know what you are doing.
Example:
export CLASSPATH="$CLASSPATH:$HOME/java/lib/foebar.jar:$HOME/extra/lib/another.jar"
But of course, if you are still invoking the jar file with the Main class you have to use the full path for it:
java -jar $HOME/java/lib/main-prog.jar
However, you can set its execution right and run it:
chmod u+x $HOME/java/lib/main-prog.jar
export PATH=$PATH:$HOME/java/lib
main-prog.jar
But you have to take care that your classpath is correct and list all required jar.
If your tools are scripts, which contain commands like
java -jar somejafile.jar
then you should edit them to contain the correct path
java -jar /full/path/to/somefile.jar
First thing to note - you're not using "Putty" to set the CLASSPATH, you're using the 'shell' that you login to, using Putty as your SSH client.
Depending on your shell, there are different ways to set your classpath -
Bash (or compatible shells
export CLASSPATH=/full/path/to/xalan.jar:/full/path/to/serializer.jar:/full/path/to/xml-apis.jar:/full/path/to/xercesImpl.jar:$CLASSPATH
C Shell (or compatible shells)
setenv CLASSPATH /full/path/to/xalan.jar:/full/path/to/serializer.jar:/full/path/to/xml-apis.jar:/full/path/to/xercesImpl.jar:$CLASSPATH
Define an environment variable in your putty session for CLASSPATH with the path of required jars. If you are using bash shell in putty then export CLASSPATH=<Jar Path>:$CLASSPATH will help. Use java -cp .:$CLASSPATH ... for including your CLASSPATH.
You should add your jar files to your CLASSPATH instead:
export CLASSPATH=/home/somebody/lib/java/a.jar:/home/somebody/lib/java/b.jar
The classpath supports directories (with *.class files) or individual jar files. You can also add wildcards (or use java -classpath option) but I will leave that off for now to don't further complicate things ;)
By the way, you don't need to change your PATH environment variable at all. The CLASSPATH is more than enough.
As per the documentation:
It's better to use the classpath option (-classpath or -cp) to a command than the global CLASSPATH environment variable.
Say you have multiple jar files a.jar,b.jar and c.jar. To add them to classpath while compiling you need to do
$javac -cp .:a.jar:b.jar:c.jar HelloWorld.java
To run do
$java -cp .:a.jar:b.jar:c.jar HelloWorld
You use the -classpath argument. You can use either a relative or absolute path. What that means is you can use a path relative to your current directory, OR you can use an absolute path that starts at the root /.
Example:
bash$ java -classpath path/to/jar/file MyMainClass
In this example the main function is located in MyMainClass and would be included somewhere in the jar file.
For compiling you need to use javac
Example:
bash$ javac -classpath path/to/jar/file MyMainClass.java
You can also specify the classpath via the environment variable, follow this example:
bash$ export CLASSPATH="path/to/jar/file:path/tojar/file2"
bash$ javac MyMainClass.java
For any normally complex java project you should look for the ant script named build.xml
Typically you specify the classpath when you start your java program with the switch
java -cp your.jar xxxx.java
But you can also permanently add it to your java installation by copying the jar to the default-java/jre/lib/ext folder.
Finally take a look at this question: Setting multiple jars in java classpath
The environment variable CLASSPATH contains a colon-separated list of locations Java should search for classes. Try
Copyexport CLASSPATH=$CLASSPATH:/path/to/comm.jar
Your problem is min separator you are using. Separator ; is for windows. On Unix systems you should use : instead:
java -classpath "bin:mysql-connector-java-5.1.19-bin.jar" MyProgram
Use a ':' to separate your entries on Unix systems:
java -classpath "bin:mysql-connector-java-5.1.19-bin.jar" MyProgram
java -cp bin:mysql-connector-java-5.1.19-bin.jar MyProgram
Eclipse converts it automatically.