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

孤街醉人 提交于 2019-11-29 11:51:05

问题


When I'm in Eclipse my project compiles with no errors, however when I try to compile with javac it says I'm missing some packages...

I copied my compile command and some of the error text below:

javac -classpath lib/ -d bin/ src/*.java
src/Cleaner.java:5: package net.sourceforge.jgeocoder does not exist
src/MyUtilities.java:19: package org.apache.commons.codec.binary does not exist

In Eclipse, I have added all the .JAR files to the build-path, and the program compiles just fine.

Why can it not find the jars when I use javac instead of the Eclipse IDE?


回答1:


-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).




回答2:


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 (;).


来源:https://stackoverflow.com/questions/7182465/how-to-make-javac-find-jar-files-eclipse-can-see-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!