Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

This is what I have in PATH:

PATH=/opt/ClosureCompiler:/home/vmadmin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

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

2 Answers 2

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.

#!/bin/bash
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
java com.google.javascript.jscomp.CommandLineRunner

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 Compiler.java file.

import com.google.javascript.jscomp.CommandLineRunner;
public class Compiler {
    public static void main (String[] args) {
        CommandLineRunner.main( args );
    }
}

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

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

 
discard

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.