Why does my classpath only work with a colon on th

2019-08-12 23:19发布

问题:

My app only starts logging to log4j if I put a : on the end off the classpath. I already have the properties file and the jar in the classpath, so I'm not sure what's going on. What does ending the classpath with a colon even do? The startup command thus becomes something like this:

java -cp path-to-log4j.properties:bunch:of:other:stuff: app

If I take that last colon off it stops logging, as if it can't find log4j. My question is, what is that last colon actually doing, as in what directory is it adding to the classpath?

回答1:

usually you should put the classpath in quotes -cp "path:path2".

EDIT: The colon might add the current directory.

EDIT2: One cannot add files (except jar files) directly to the classpath. Only directories. Read that today^^



回答2:

Was surprised with similar situation:

Not working:

java -classpath swt.jar org.ivan.swt.SWTHelloWorld

Working:

java -classpath swt.jar: org.ivan.swt.SWTHelloWorld

Also working:

java -classpath :swt.jar org.ivan.swt.SWTHelloWorld

(note colon at the beginning and the end of the swt.jar)

Java(TM) SE Runtime Environment (build 1.8.0_51-b16) on Linux