简体   繁体   English

Java导入错误

[英]Java Imports Error

I'm trying to import org.apache.commons.fileupload.* but I am being told that it does not exist. 我正在尝试import org.apache.commons.fileupload.*但有人告诉我它不存在。

I am downloading this JAR: http://commons.apache.org/fileupload/ 我正在下载此JAR: http : //commons.apache.org/fileupload/

And placing it on the classpath. 并将其放在类路径上。 So what am I doing wrong here? 那我在做什么错呢?

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: 若要确定您的类是否存在于jar中,请执行以下操作:

# 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

Most likely you're thinking of %CLASSPATH% environment variable. 您很可能在考虑%CLASSPATH%环境变量。 You shouldn't do that. 你不应该那样做。 The JAR file has to go in /WEB-INF/lib folder of the dynamic web application project. JAR文件必须放在动态Web应用程序项目的/WEB-INF/lib文件夹中。 That folder is by default covered by the webapp's classpath. 默认情况下,该文件夹由webapp的类路径覆盖。 A bit decent IDE (Eclipse, Netbeans, etc) will automagically add it to the Build Path whenever you drop a JAR file in that folder. 每当您在该文件夹中放置一个JAR文件时,一点点不错的IDE(Eclipse,Netbeans等)都会自动将其添加到构建路径中。

When you're compiling using plain vanilla javac.exe in command console, then you have to specify it in the -cp argument. 在命令控制台中使用普通香草javac.exe进行编译时,必须在-cp参数中指定它。


Update : Assuming that you're using Windows and are sitting in source root folder, here's how javac.exe should look like: 更新 :假设您正在使用Windows,并且位于源根文件夹中,则javac.exe外观应如下所示:

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. 注意:通配符仅适用于JDK 1.6或更高版本。 Otherwise you've to specify all libraries separately. 否则,您必须单独指定所有库。

Since you're writing a servlet, you've probably created a web project. 由于您正在编写servlet,因此可能已经创建了一个Web项目。 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. 将您的fileupload jar放在WEB-INF/lib文件夹中,然后在IDE中刷新您的项目,以自动将其放置在项目构建路径中。


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). 编辑看到您正在使用命令行,请确保提供了CLASSPATH的完整jar文件路径(包括jar名称及其扩展名,以分号分隔)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM