How to make javac find JAR files? (Eclipse can see them)

拟墨画扇 提交于 2019-11-30 08:11:19

-classpath lib/ will cause javac to look for a tree of class files in lib. If you have JAR archives there, you have to use -classpath lib/*.jar - and probably use whatever escaping mechanism your CLI has on the * to make sure it reaches javac rather than being expanded by the CLI

See the javac command reference (windows).

Existing answer, as informative as it is, still left me wondering, having to go over the javac -classpath docs. Here's a revision of what you might want to know:

javac -cp "lib/" Example.java will load all .class files in lib/.
javac -cp "lib/\*" Example.java will load all .jar files in lib/, but not the .class files.
javac -cp "lib/;lib/\*" Example.java will load all .jar and .class files in lib/.

Notes:

  • The * (wildcard) character as used in above examples is escaped (note the \ before *). \ is for bash, if you're using something else it might be different. You want to escape it because the java sdk utilities, like javac, their own way of interpreting the wildcard. As far as I can tell (correct me if I'm wrong), Windows command line doesn't expand * so you don't need to escape it: -cp lib\* should be fine.
  • ; separates paths, as in javac -cp ".;a/;b/;c/" Example.java – this would load .class files in the following directories: current directory, a, b and c.
  • Surround the classpath with quotes (like in examples) when using separators (;).
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!