views:

2234

answers:

7

Hi, new to Java. I want to use the org.apache.commons.lang.WordUtils package. Tried importing it. Tried downloading it and adding it to my build path. When I use intellisense for the word WordUtils Eclipse auto puts the import statement at the top of the file.

I get the following error when compiling:

ConvertToUrl.java:3: package org.apache.commons.lang
import org.apache.commons.lang.WordUtils;
                              ^

ConvertToUrl.java:36: cannot find symbol
symbol  : variable WordUtils
location: class d6.common.ConvertToUrl
                url = WordUtils.capitalizeFully(url);
                      ^
2 errors

And when I remove the import statement at the top I get:

ConvertToUrl.java:34: cannot find symbol
symbol  : variable WordUtils
location: class d6.common.ConvertToUrl
                url = WordUtils.capitalizeFully(url);
                      ^
1 error

Thanks

+2  A: 

That is correct java, and correct use of the WordUtils function. This is likely an eclipse issue. I'd check that the jar is imported and is on the compile path.

Steve B.
A: 

Go into your project properties and under Java Build Path (I think) you need to add the commons jar as an External Jar, this is pretty straight forward once you find the right tab in the properties, once there you'll be able to browse to where you downloaded the jar and add it, that should clear up your problem.

codeLes
A: 

This is classpath 101. The JAR containing that class isn't in the build path. Easy enough to add in Eclipse: right click on project properties and it should be in the list. of choices.

duffymo
+1  A: 

Thanks everyone. The class is in the build path.

I've pasted the class I'm using below (minus some unnecessary bits).

As I said Eclipse adds in this import statement.

import org.apache.commons.lang.WordUtils;

Do I still need this if I have downloaded the JAR? btw I haven't taken classpath 101, I'm just figuring it out as I go along, completely new to Java. Thanks for the help!

package d6.common;

public class ConvertToUrl {

    private static String url;

    public String getUrl() {
     return url;
    }

    public void setUrl(String url) {

     url = WordUtils.capitalizeFully(url);
     this.url = url;

    }

}
I cleaned this up for you, but really it's not an answer. You should add any extra information to the question instead.
Michael Myers
A: 

You don't ever "need" to use a single import. All it does is save you typing: you can type "WordUtils" instead of "org.apache.commons.lang.WordUtils" whenever you want to use the class.

If you're still having the issue, it says the class is NOT in the build path and you still have to figure out how to get it there.

duffymo
+1  A: 

When you get the compiler error, where are you getting this? Is this when you try to compile your source on the command line? If so, then you need to make sure that jar is either set in your CLASSPATH variable or used on the commandline or added to your build.xml or set for whatever mechanism you're using to compile the class.

Suppressingfire
+1  A: 

Ah fantastic, that was the one Suppressingfire, thanks.

I was compiling via command line. I still need to learn about ant. Am used to php, its a lot simpler to start off with!