无法解析导入 java.awt
The import java.awt cannot be resolved
我已经在装有 macOS 10.13.5 的 MacBookPro 上安装了 Eclipse [版本:Photon Release (4.8.0)] 和 JDK 10
当我写我的代码时:import java.awt.*;
我收到错误:
The import java.awt cannot be resolved
java.awt
是否包含在 JDK 10 中?如果是,我在哪里以及如何让 Eclipse 可见?如果没有,我该如何添加 java.awt?
Is the java.awt
included in JDK 10?
是的,包确实存在。 The Java10 API docs 也确认一下。
If yes where is and how can I make visible to Eclipse?
在模块化代码中,您需要做的就是 resolve the java.desktop
module 通过在模块描述符文件 (module-info.java
) 中声明对它的依赖。即
requires java.desktop;
基本上,只需将此 maven 构建编译器插件 部分添加到您的主项目 Maven POM.xml
以指定哪些 JDK9+ 模块 必须 添加用于 javac 编译目的。
<?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>
如此处解释:
https://www.baeldung.com/java-9-modularity
其他常见缺失的JDK10+模块请参考:
http://cr.openjdk.java.net/~iris/se/10/pfd/java-se-10-pfd-spec-01/api/overview-summary.html
我已经在装有 macOS 10.13.5 的 MacBookPro 上安装了 Eclipse [版本:Photon Release (4.8.0)] 和 JDK 10
当我写我的代码时:import java.awt.*;
我收到错误:
The import java.awt cannot be resolved
java.awt
是否包含在 JDK 10 中?如果是,我在哪里以及如何让 Eclipse 可见?如果没有,我该如何添加 java.awt?
Is the
java.awt
included in JDK 10?
是的,包确实存在。 The Java10 API docs 也确认一下。
If yes where is and how can I make visible to Eclipse?
在模块化代码中,您需要做的就是 resolve the java.desktop
module 通过在模块描述符文件 (module-info.java
) 中声明对它的依赖。即
requires java.desktop;
基本上,只需将此 maven 构建编译器插件 部分添加到您的主项目 Maven POM.xml
以指定哪些 JDK9+ 模块 必须 添加用于 javac 编译目的。
<?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>
如此处解释:
https://www.baeldung.com/java-9-modularity
其他常见缺失的JDK10+模块请参考:
http://cr.openjdk.java.net/~iris/se/10/pfd/java-se-10-pfd-spec-01/api/overview-summary.html