0

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?

zerkms
  • 431
  • 2
  • 5
  • 17

2 Answers2

2

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
Ignacio Vazquez-Abrams
  • 45,939
  • 6
  • 79
  • 84
2

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.

BillThor
  • 27,737
  • 3
  • 37
  • 69
  • 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 04:28
  • @IgnacioVazquez-Abrams Simplified that option as suggested. – BillThor Jan 19 '12 at 05:30