Adding a jar file to CLASSPATH is still not executable

0

Perhaps I just don't understand how the whole CLASSPATH environment variable works when trying to find .jar files on your system. I thought if you specified it, you could launch .jar files with java in much the same way that you can launch executables that are on your path.

I have an executable java archive (.jar file) on my system, that I stuck in /usr/local/bin/gatk/. I added this to my CLASSPATH via:

export CLASSPATH=/usr/local/bin/gatk/GenomeAnalysisTK.jar

I thought this would make the .jar file visible to my JVM. When I try to invoke it with

java -jar GenomeAnalysisTK.jar
#Error: Unable to access jarfile .gatk/GenomeAnalysisTK.jar

I can invoke it setting the absolute path, e.g.
java -jar /usr/local/bin/gatk/GenomeAnalysisTK.jar, however I'd rather not type the full path each time. I have read many of the linked tutorials but somehow I don't seem to be getting this right and I can't understand what I am doing wrong.

Simon O'Hanlon

Posted 2014-06-02T16:02:16.780

Reputation: 135

Answers

3

CLASSPATH is not meant to search for jars. It is meant to search for classes. Putting a jar in CLASSPATH means java will see the classes inside the jar, but not the jar itself.

What you want is more likely a shell script wrapper starting the jar.

Create a text file containing the following lines:

#!/bin/sh
java -jar /usr/local/bin/gatk/GenomeAnalysisTK.jar

Make the file executable (suppose you name it GenomeAnalysisTK):

chmod +x GenomeAnalysisTK

Put that file in /usr/local/bin or ~/bin or wherever you want provided the path is in your $PATH.

From now on you can call the shell script and it will start the jar. No need to mess with CLASSPATH anymore.


Alternatively: with the jar in the classpath you can start the main class from inside the jar like this:

java org.broadinstitute.sting.gatk.CommandLineGATK

The main class of the jar is specified in the file META-INF/MANIFEST.MF inside the jar. For more information see the wikipedia article about jar files.

lesmana

Posted 2014-06-02T16:02:16.780

Reputation: 14 930

Many thanks. I was going crazy. I don't have much experience using java based programs so that's a great help, thanks. – Simon O'Hanlon – 2014-06-02T18:28:49.807