I have java tools that I need to use. The tools are in a folder full of jar files. I wanted to add this folder to my path, for the obvious reasons, but after I edit my .bash_profile to include the new folder in the $PATH variable, and source it, it doesn't work. I also tried logging out, and logging back in, and that didn't work either. I just keep getting the error message "Unable to access jarfile .jar"
Asked
Active
Viewed 4.7k times
10
-
Have you checked if it's not a permissions issue? – matcheek Jun 21 '12 at 11:11
-
You'll have to excuse my ignorance, but how could I check this? – Davy Kavanagh Jun 21 '12 at 11:20
-
You may want to check this answer:http://stackoverflow.com/questions/5569591/unable-to-access-jarfile-in-linux-land – matcheek Jun 21 '12 at 11:46
2 Answers
5
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.

Huygens
- 9,345
-
I tried export CLASSPATH=$CLASSPATH:<my_new_path>, with the <my_new_path> replaced with the actual path of the folder, sourced and still the same result. – Davy Kavanagh Jun 21 '12 at 11:46
-
Sorry my mistake, it's not the path but the .jar file with full path you put, I'll update the answer – Huygens Jun 21 '12 at 12:11
-
Does this mean I will have to add to the CLASSPATH, a new entry for every jar file. There are about 80 of these jar files. – Davy Kavanagh Jun 21 '12 at 12:26
-
@DavyKavanagh Note: do not add the environment variable to
.bashrc
, add it to.profile
. See Alternative to .bashrc – Gilles 'SO- stop being evil' Jun 21 '12 at 22:44 -
@DavyKavanagh sorry for the long delay in the response. But yes, this would mean adding the 80 jar files to the classpath. – Huygens Feb 28 '15 at 20:16
2
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

Jari Laamanen
- 2,421