views:

52

answers:

4
+1  Q: 

Java Imports Error

I'm trying to import org.apache.commons.fileupload.* 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?

A: 

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

Adam
I said I added the JAR to the classpath.
Glenn Nelson
I'm saying check to make sure that the jvm is infact using the classpath you think it is.
Adam
A: 

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
Synesso
+1  A: 

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.

BalusC
When I include the servlet-api.jar I include it via 'set classpath' so how would this be any different?
Glenn Nelson
I now see in the comments that you're doing this in command console -which I honestly didn't expect since that's pretty cumbersome. In that case, you need to add the full path to the `commons-fileupload.jar` to the `-cp` argument, separated by a (semi)colon (depending on OS). Don't forget to put quotes around the individual path whenever it contains spaces. You can of course use the `%CLASSPATH%` environment variable instead, but that's considered poor practice. Better write a `.bat` or `.cmd` file which has the correct `-cp` argument in the command.
BalusC
Alright, I got it working now (I know command line is cumbersome but I like what I like. After all, no one said 6502 asm is streamlined). I need to do a little more tinkering to figure out what was going wrong but doing it using javac -cp worked just fine.
Glenn Nelson
You're welcome.
BalusC
@BalusC - using a bat file would be better. Using a cross-platform build tool like Ant or Maven would be *much* better.
Mike Baranczak
@Mike: I wholeheartedly agree this. An IDE would be the cream on the top. But maybe it's just OP's first steps in Java. It's then better to let him fiddle that way so that he'll end up having a better grasp on the phenomenon called "classpath" and inherently also understand better what build tools are doing under the covers :)
BalusC
A: 

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

The Elite Gentleman
I'm not using an IDE, I do things via command line.
Glenn Nelson