10

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"

Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
Davy Kavanagh
  • 253
  • 2
  • 3
  • 7

2 Answers2

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
  • 8,985
  • 3
  • 31
  • 36
  • I tried export CLASSPATH=$CLASSPATH:, with the 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](http://unix.stackexchange.com/questions/3052/3085#3085) – 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,371
  • 16
  • 13