简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM