Alan
Alan

Reputation:

Java cannot find symbol compile error

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

Upvotes: 0

Views: 23358

Answers (8)

Krushna
Krushna

Reputation: 11

If you are using ant script to build your code in eclipse, check if the classpath is also mentioned in the build.xml file. You can use something like this under javac tag in your build file. Where pathelement here is the dependent library:

<target name="compile" depends="init" description="compile the source " >
    <javac srcdir="${src}" destdir="${classsfiles}" >
        <classpath>
            <pathelement path="${libdir}/common-io.jar"/>
            <pathelement path="${libdir}/xerces.jar"/>
        </classpath>
    </javac>
</target>

Upvotes: 1

Alan
Alan

Reputation:

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.

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;

    }
}

Thanks for the help!

Upvotes: 1

Alan
Alan

Reputation:

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!

Upvotes: 1

Suppressingfire
Suppressingfire

Reputation: 3286

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.

Upvotes: 1

duffymo
duffymo

Reputation: 308743

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.

Upvotes: 0

duffymo
duffymo

Reputation: 308743

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.

Upvotes: 0

codeLes
codeLes

Reputation: 3069

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.

Upvotes: 1

Steve B.
Steve B.

Reputation: 57274

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.

Upvotes: 4

Related Questions