简体   繁体   English

导入 java.awt 无法解析

[英]The import java.awt cannot be resolved

I have installed the Eclipse [Version: Photon Release (4.8.0)] and JDK 10 on a MacBookPro with macOS 10.13.5 When I write in my code: import java.awt.*;我已经在装有 macOS 10.13.5 的 MacBookPro 上安装了 Eclipse [版本:Photon Release (4.8.0)] 和 JDK 10 当我在代码中编写时: import java.awt.*;

I get the error:我收到错误:

The import java.awt cannot be resolved导入 java.awt 无法解析

Is the java.awt included in JDK 10 ? java.awt是否包含在 JDK 10 中? If yes where is and how can I make visible to Eclipse?如果是,我在哪里以及如何使 Eclipse 可见? If no how can I add java.awt?如果没有,我该如何添加 java.awt?

Is the java.awt included in JDK 10? java.awt是否包含在 JDK 10 中?

Ye, the package does exist.是的,包确实存在。 The Java10 API docs do confirm the same as well. Java10 API 文档也证实了这一点。

If yes where is and how can I make visible to Eclipse?如果是,我在哪里以及如何使 Eclipse 可见?

In a modular code, all you need to do, is to resolve the java.desktop module by declaring a dependency on it in your module-descriptor file( module-info.java ).在模块化代码中,您需要做的就是通过在模块描述符文件( module-info.java )中声明对它的依赖来解析java.desktop模块 ie IE

requires java.desktop;

Basically, just add this maven build compiler plugin portion to your main project Maven POM.xml基本上,只需将此Maven 构建编译器插件部分添加到您的主项目 Maven POM.xml
to specify which JDK9+ modules MUST BE added for javac compilation purposes.指定必须javac编译目的添加哪些 JDK9+ 模块。

  <?xml version="1.0" encoding="UTF-8"?>
  <project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
    ...
    
    <properties>
        <java.version>11</java.version>
        ...
    </properties>

    ...

    <build>
        <plugins>
             <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>

                    <!-- JDK9+ Add Module MAGIC happens below...          -->
                    <!-- Add implicit default JDK9+ java.se explicitly    -->
                    <!-- Add explicit java.desktop for import java.awt.*; -->
                    <compilerArgs>
                        <arg>--add-modules</arg>
                        <arg>java.se,java.desktop</arg>
                    </compilerArgs>
                </configuration>
            </plugin>
        </plugins>
    </build>

  </project>

as explained here:如此处所述:

https://www.baeldung.com/java-9-modularity https://www.baeldung.com/java-9-modularity

For other common missing JDK10+ modules, please refer to:其他常见缺失的JDK10+模块,请参考:

http://cr.openjdk.java.net/~iris/se/10/pfd/java-se-10-pfd-spec-01/api/overview-summary.html http://cr.openjdk.java.net/~iris/se/10/pfd/java-se-10-pfd-spec-01/api/overview-summary.html

I have tried this, and in fact the error dissapears, but the program is unstable. 我已经尝试过了,实际上错误消失了,但是程序不稳定。 I will try to do anything and it will randomly crash. 我将尝试做任何事情,它将随机崩溃。 I don't know what to do, but it just won't work, it's unusable. 我不知道该怎么办,但那根本行不通,无法使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM