开发者

Java Imports Error

I'm trying to import org.apache.commons.fi开发者_如何学JAVAleupload.* but I am being told that it does not exist.

I am downloading this JAR: http://commons.apache.org/fileupload/

And placing it on the classpath. So what am I doing wrong here?


Most likely you're thinking of %CLASSPATH% environment variable. You shouldn't do that. The JAR file has to go in /WEB-INF/lib folder of the dynamic web application project. That folder is by default covered by the webapp's classpath. A bit decent IDE (Eclipse, Netbeans, etc) will automagically add it to the Build Path whenever you drop a JAR file in that folder.

When you're compiling using plain vanilla javac.exe in command console, then you have to specify it in the -cp argument.


Update: Assuming that you're using Windows and are sitting in source root folder, here's how javac.exe should look like:

javac -cp .;/path/to/tomcat/lib/*;/path/to/WEB-INF/lib/* com/example/Servlet.java

Note: the wildcard only works on JDK 1.6 or newer. Otherwise you've to specify all libraries separately.


Is the jar in your classpath? Also check out this discussion on the downsides of wildcard imports.


Do you see your class in the jar? To find out if your class exists in a jar, do the following:

# linux
jar tvf jarname.jar | grep classname

# win
jar tvf jarname.jar | findstr classname

To find out if your class exists in any of a number of jars, you can do this:

# linux
for f in `find . -name *.jar`; do echo $f; jar tvf $f | grep classname; done | less


Since you're writing a servlet, you've probably created a web project. Place your fileupload jar inside the WEB-INF/lib folder and let refresh your project in the IDE to be placed automatically in your project build path.


Edit Seeing you're using command-line, make sure that you provide the full jar file path to the CLASSPATH (including the jar name and its extension, separated by semicolon).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜