How to Set Java Classpath in Linux

Set classpath permanently in Linux

You could add that export statement to the end of your bash init file ~/.bashrc, therefore it will in effect all the time.

By the way, a better setting of CLASSPATH would be

export CLASSPATH="/path/to/file":"/path/to/file2":"${CLASSPATH}"

this will also preserve the initial value of that environment variable.

How to set CLASSPATH in Linux to let java find jar file?

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.

Java classpath - Linux

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.

How to add multiple jar files in classpath in linux

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

Setting classPath inside Linux Script

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.

Setting CLASSPATH permanently

The variable you set in a terminal, is valid only for that terminal. What you should do is, export the variable in your ~/.bashrc file, which is loaded for each terminal. So, add that statement in the .bashrc file, and you'll be fine.

You can also export the variable from ~/.bash_profile file, which will be loaded for login shells.



Related Topics



Leave a reply



Submit