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?
-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, likejavac, 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 injavac -cp ".;a/;b/;c/" Example.java– this would load.classfiles 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