eclipse Java找不到符号编译错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/403378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 13:27:40  来源:igfitidea点击:

Java cannot find symbol compile error

javaeclipseclasspath

提问by Krushna

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 WordUtilsEclipse auto puts the import statement at the top of the file.

Java 新手。我想使用 org.apache.commons.lang.WordUtils 包。试过导入。尝试下载它并将其添加到我的构建路径中。当我对单词WordUtilsEclipse auto使用智能感知时,会将导入语句放在文件的顶部。

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

谢谢

回答by Steve B.

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.

那是正确的java,正确使用WordUtils 函数。这很可能是日食问题。我会检查 jar 是否已导入并在编译路径上。

回答by codeLes

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.

进入您的项目属性,在 Java Build Path(我认为)下,您需要将 commons jar 添加为 External Jar,一旦您在属性中找到正确的选项卡,这将非常简单,一旦在那里您就可以浏览到您下载 jar 的位置并添加它,这应该可以解决您的问题。

回答by codeLes

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.

正如我所说,Eclipse 添加了这个导入语句。

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.

如果我已经下载了 JAR,我还需要这个吗?
顺便说一句:我没有使用类路径 101,我只是在进行过程中弄清楚,对 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!

谢谢您的帮助!

回答by Suppressingfire

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.

当您收到编译器错误时,您是从哪里得到的?这是当您尝试在命令行上编译源代码时吗?如果是这样,那么您需要确保 jar 设置在您的 CLASSPATH 变量中或在命令行上使用或添加到您的 build.xml 或设置为您用于编译类的任何机制。

回答by Suppressingfire

Ah fantastic, that was the one Suppressingfire, thanks.

啊太棒了,那就是 Suppressingfire,谢谢。

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!

我是通过命令行编译的。我仍然需要学习蚂蚁。我习惯了 php,开始使用它要简单得多!

回答by Krushna

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:

如果您使用 ant 脚本在 eclipse 中构建代码,请检查 build.xml 文件中是否也提到了类路径。您可以在构建文件的 javac 标记下使用类似的内容。这里的 pathelement 是依赖库:

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

回答by duffymo

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.

您永远“不需要”使用单个导入。它所做的只是节省您的输入:只要您想使用该类,您就可以输入“WordUtils”而不是“org.apache.commons.lang.WordUtils”。

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.

如果您仍然遇到问题,它会说该类不在构建路径中,您仍然需要弄清楚如何将它放在那里。

回答by duffymo

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.

这是类路径 101。包含该类的 JAR 不在构建路径中。在 Eclipse 中添加很容易:右键单击项目属性,它应该在列表中。的选择。