smwikipedia
smwikipedia

Reputation: 64175

How does java locate a JAR file?

The JAR file locates at C:\Workbench\jars\antlr-4.4-complete.jar

The environment variables are:

CLASSPATH=.;C:\Workbench\jars\*
PATH=C:\Workbench\jars;...

I am trying with the following commands:

java -jar "C:\Workbench\jars\antlr-4.4-complete.jar"   <-- OK
java -jar antlr-4.4-complete.jar   <-- FAIL!
java org.antlr.v4.Tool  <-- OK

I am totally confused about the failed one. I am expecting the PATH variable will be looped through to locate the jar file. But it seems not. Why?

My guess is, the implementation of java -jar command line doesn't use the PATH variable for searching jar file. But still, why?

Upvotes: 1

Views: 450

Answers (1)

Tom
Tom

Reputation: 16188

The PATH is for looking up the command to execute, in this case java will be looked up on the PATH.

You will need to supply either an absolute or relative path to java -jar because the terminal (bash/windows/zsh/etc...) will not expand arguments in this way. CLASSPATH is used by Java to look up further jars, but it expects a correct path to the initial jar as the first argument.

Upvotes: 1

Related Questions