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:
Copyjava -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.
Answer from flybywire on Stack OverflowI 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:
Copyjava -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.
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.
Copyexport CLASSPATH="$CLASSPATH:foo.jar:../bar.jar"
export CLASSPATH=/home/appnetix/LOG4J_HOME/log4j-1.2.16.jar
or, if you already have some classpath set
export CLASSPATH=$CLASSPATH:/home/appnetix/LOG4J_HOME/log4j-1.2.16.jar
and, if also you want to include current directory
export CLASSPATH=$CLASSPATH:/home/appnetix/LOG4J_HOME/log4j-1.2.16.jar:.
You have to use ':' colon instead of ';' semicolon.
As it stands now you try to execute the jar file which has not the execute bit set, hence the Permission denied.
And the variable must be CLASSPATH not classpath.
Videos
I'm not a Java programmer, so I don't know the correct value for CLASSPATH. But, you seem to. You can add it to ~/.bashrc like so:
CLASSPATH=/path/to/1:/path/to/2:/etc
The change will take effect globally the next time you log in. However, it will take effect immediately in new shells.
Additionally, if you want to set it for just one particular command, do this:
CLASSPATH=/something command-here arg1 arg2
A third way would be to create a wrapper script, which would be appropriate if you needed to set multiple variables or if you needed to determine appropriate values programmatically:
#!/bin/bash
export CLASSPATH=/something
export ANOTHER_ENV_Variable=foo
exec your_fancy_program "$@"
that's what happened with me.. i left empty spaces while typing.. it fixed when i removed spaces.
1) 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 ":"
You have to supply the complete path after the parameter -jar. So for your example you have to call
java -jar /home/user/plantuml.jar -testdot
The $CLASSPATH is only evaluated to find additional files (classes/resources) but not the jar file defined in the command line.
export CLASSPATH="/path/to/class_or_jar1":"/path/to/class_or_jar2":"${CLASSPATH}"
export CLASSPATH=/your/stuff/
or preserving system wide settings:
export CLASSPATH=$CLASSPATH:/your/addition/
Here are two good tutorials I found via Google:
http://www.linuxheadquarters.com/howto/basic/classpath.shtml
http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/classpath.html
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.
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 ":"
I think you should not put any paths that a local to your home directory in a system wide file. I would leave /etc/environment well alone, unless you provide some changes, that are necessary or beneficial to all users.
Put any changes to the CLASSPATH in your .bashrc in your home directory.
CLASSPATH=$CLASSPATH:/home/foo:/home/foo/Java_code/my_code
export CLASSPATH
This way you can source it and any newly started bash will have the settings right at once.
export CLASSPATH=""
or better
unset CLASSPATH
will delete an existing Classpath. There are multiple locations where you can set or unset the classpath - a missing entry will not unset it.
You need to use:
export CLASSPATH=/home/praveen/lib/commons-logging-1.0.4.jar:/home/praveen/lib/log4j-1.2.8.jar
rather than:
export CLASSPATH = /home/praveen/lib/commons-logging-1.0.4.jar: /home/praveen/lib/log4j-1.2.8.jar
You can't put spaces in between the variable name and the equals sign in shell scripting.
You should not put any spaces in when you are setting variable, not even spaces around '=', it should be:
export CLASSPATH=/home/praveen/lib/commons-logging-1.0.4.jar:/home/praveen/lib/log4j-1.2.8.jar
If you have spaces in one of the elements use single or double quotations e.g.:
MY_VAR1=' variable with spaces'
MY_VAR2=variable_without_spaces
export MY_VAR3="${MY_VAR1}${MY_VAR2}"