IntelliJ IDEA - 错误:java: 包 foo 不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19227283/
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-08-12 15:14:33  来源:igfitidea点击:

IntelliJ IDEA - Error: java: package foo does not exist

javagroovyintellij-ideaimporterror

提问by Ben Dohrmann

I have a project in IntelliJ IDEA that consists of both Java and Groovy classes. These classes are contained in folders "groovy" and "java" that I've marked as source folders. I have many Java classes that import classes from the "groovy" source folder, but when I try running them, I consistently get the error "java: package foo does not exist". Package "foo" exists directly under the "groovy" folder, so this should be working. I included a visual below. (I'm trying to avoid any specific details. I may or may not be working on a top secret Area 51 project.)

我在 IntelliJ IDEA 中有一个项目,其中包含 Java 和 Groovy 类。这些类包含在我标记为源文件夹的文件夹“groovy”和“java”中。我有许多 Java 类从“groovy”源文件夹中导入类,但是当我尝试运行它们时,我始终收到错误“java:包 foo 不存在”。包“foo”直接存在于“groovy”文件夹下,所以这应该可以工作。我在下面添加了一个视觉效果。(我试图避免任何具体的细节。我可能在也可能不在一个绝密的 51 区项目中工作。)

Structure visual:

结构视觉:

project-folder
|
-src
 |
 -main
  |
  -groovy (marked as source)
  ||
  |-foo
  | |
  | -bar.groovy
  -java (marked as source)
   |
   - java class that imports "foo.bar"

Error: java: package foo does not exist

错误:java: 包 foo 不存在

Things that don't work:

不起作用的事情:

  • Taking everything under "framework" and placing them directly under "groovy" folder. Results in "Cannot resolve symbol bar"

  • Unmavenizing project and rebuilding

  • 将“框架”下的所有内容直接放在“groovy”文件夹下。结果在“无法解析符号栏”

  • Unmavenizing 项目和重建

回答by Javaru

You should not have to "unmavenize" your project. (Although I understand the troubleshooting reasoning for suggesting you do such.) I suspect the issue is a corrupted cache or index. Go to File > Invalidate Cache. Select to invalidate the cache and then restart IDEA. Let IDEA re-index the project. Things should be fine. If not, check that 1) you are using the latest version of IDEA (12.1.5) and 2) the latest version of the Groovy plug-in (File > Settings > [IDE Settings] > Plugins).

你不应该“unmavenize”你的项目。(虽然我理解建议您这样做的故障排除推理。)我怀疑问题是缓存或索引损坏。转到文件 > 使缓存无效。选择使缓存无效,然后重新启动 IDEA。让 IDEA 重新索引项目。事情应该没问题。如果没有,请检查 1) 您使用的是最新版本的 IDEA (12.1.5) 和 2) 最新版本的 Groovy 插件(文件 > 设置 > [IDE 设置] > 插件)。

When you do use maven, you will need to identify the "groovy" directory as an additional source directory in your POM. If you do not, when IDEA re-imports the project (i.e. re-syncs to the POM), it will drop the groovydirectory as a source since by default maven does not consider it a source. How you do this depends on what plugin you use. Since GMavenis no longer maintained, I've been using the groovy-eclipse-compilerplugin. If you use that plug-in, the plug-in will automatically include src/main/groovyas a source (as long as there is at least one java or groovy file in src/main/java). However, IDEA does not pick that directory up and include it as a source as well. That means if you manually (or IDEA automatically) runs a maven re-import, your src/main/groovydirectory will get unmarked as a source, and IDEA will show compile errors. You need to specify the additional directory. You can use the build-helper-maven-plugin to do this as the groovy-eclipse-compiler documentation recommends.

当您确实使用 maven 时,您需要将“groovy”目录标识为 POM 中的附加源目录。如果不这样做,当 IDEA 重新导入项目(即重新同步到 POM)时,它会将groovy目录作为源删除,因为默认情况下 maven 不将其视为源。您如何执行此操作取决于您使用的插件。由于不再维护GMaven,我一直在使用groovy-eclipse-compiler插件。如果您使用该插件,该插件将自动src/main/groovy作为源包含(只要 中至少有一个 java 或 groovy 文件src/main/java)。但是,IDEA 不会选择该目录并将其作为源包含在内。这意味着如果您手动(或自动 IDEA)运行 Maven 重新导入,您的src/main/groovy目录将被取消标记为源,IDEA 将显示编译错误。您需要指定附加目录。您可以按照 groovy-eclipse-compiler文档的建议使用 build-helper-maven-plugin 来执行此操作。

Here's the meat & potatoes of a POM for a working Java/Groovy project:

以下是用于工作的 Java/Groovy 项目的 POM 的主要内容:

<properties>
    <groovy.version>2.1.5</groovy.version>
    <groovy-eclipse-compiler.version>2.8.0-01</groovy-eclipse-compiler.version>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <compilerId>groovy-eclipse-compiler</compilerId>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.codehaus.groovy</groupId>
                    <artifactId>groovy-eclipse-compiler</artifactId>
                    <version>${groovy-eclipse-compiler.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.codehaus.groovy</groupId>
                    <artifactId>groovy-eclipse-batch</artifactId>
                    <version>2.1.5-03</version>
                    <scope>compile</scope>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>add-source</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/main/groovy</source>
                        </sources>
                    </configuration>
                </execution>
                <execution>
                    <id>add-test-source</id>
                    <phase>generate-test-sources</phase>
                    <goals>
                        <goal>add-test-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/test/groovy</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-all</artifactId>
        <version>${groovy.version}</version>
    </dependency>
</dependencies>

回答by suman j

I had the similar problem. In my case, groovy compilation logged RuntimeException as warning. This is because of No suitable classloader found for grab. After I fixed this issue, groovy sources were successfully compiled and Java classes were able to see them on the classpath.

我有类似的问题。就我而言,groovy 编译将 RuntimeException 记录为警告。这是因为没有找到合适的类加载器用于抓取。在我解决这个问题后,groovy 源代码被成功编译,Java 类能够在类路径上看到它们。

回答by Snekse

Restart IntelliJ :-) Dumb, but that's what worked for me. No idea what was causing the issue, but I'm glad it's fixed. Hopefully that helps someone else too.

重新启动 IntelliJ :-) 愚蠢,但这对我有用。不知道是什么导致了这个问题,但我很高兴它已经解决了。希望这也能帮助别人。