Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is what I have in PATH:


There is a compiler.jar in /opt/ClosureCompiler, but when I call

java -jar compiler.jar

I get

Unable to access jarfile compiler.jar

Though java -jar /opt/ClosureCompiler/compiler.jar works just fine.

So, can java follow the PATH for filenames resolving? If so - what have I done wrong?

share|improve this question
up vote 2 down vote accepted

There is no provision in the java executable for searching a list of directories for the .jar file. Write a wrapper script that executes java with the full path to the .jar.

exec java -jar /opt/ClosureCompiler/compiler.jar
share|improve this answer

The easiest option is to make the jar excutable using chmod +x compiler.jar. Then you can run it from your PATH as follows:

compiler.jar --help

These are a couple of other options to use the java command to run the file.

Java uses the CLASSPATH to find classes and other resources. The classpath can contain jars and directories. Java will search the classpath for resources. Define an environment variable containing the path(s) you want searched. The path seperator is ; on Windows and : on most other systems. This will work for classes on the command line, but not jars. If you read the manifest of compiler.jar you can write a short Compiler class which invokes the main class. Alternatively, just invoke the main class directly as specifed in the manifest instead of Compiler (as specified below).

You may already have a classpath defined. Try echo $CLASSPATH to see if it is. Try

export CLASSPATH=$CLASSPATH:/opt/ClosureCompiler:/opt/ClosureCompiler/compiler.jar
java Compiler

or for closure (other jars will have a different main class).

export CLASSPATH=$CLASSPATH:/opt/ClosureCompiler/compiler.jar

You should define your CLASSPATH where you added /opt/ClosureCompiler to your path. You don't need it /opt/ClosureCompiler in your path.

This is a sample file.

public class Compiler {
    public static void main (String[] args) {
        CommandLineRunner.main( args );

It can be compiled with the command javac -cp compiler.jar

share|improve this answer
In order to get this to work you'd need to add the .jar itself to $CLASSPATH and then specify the main class yourself, thereby eliminating one of the main (no pun intended) advantages of creating a .jar file in the first place. – Ignacio Vazquez-Abrams Jan 19 '12 at 4:28
@IgnacioVazquez-Abrams Simplified that option as suggested. – BillThor Jan 19 '12 at 5:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.