maven - 使用 Java 9 导入 java.util.function 时出现问题

标签 maven intellij-idea import java-9

我遇到导入问题:

import com.google.common.base.Function; 

当我运行测试时,我收到一条错误消息:

Error:(31, 33) java: cannot access java.util.function.Supplier
class file for java.util.function.Supplier not found
Error:(34, 49) java: cannot access java.util.function.Function
class file for java.util.function.Function not found

到目前为止,我了解到 Java 8 使用这些作为替代品。但是添加以下导入不起作用:

import java.util.function.Function;
import java.util.function.Supplier; 
import java.util.function.*; 

它们用红色下划线显示(Intelli-J),我收到以下错误:

Error:(22, 26) java: package java.util.function does not exist
Error:(23, 26) java: package java.util.function does not exist
Error:(21, 1) java: package java.util.function does not exist 

需要注意的重要事项是,我使用的是 Java 9,我没有安装任何以前的 Java,因为这是一台新笔记本电脑。另外,这个项目是使用 Java 8 创建的。我也使用 maven。

我猜我可能需要在 PomXML 文件中更新我的项目,以反射(reflect)我正在使用的 JDK。

我使用 Homebrew 安装了 java,但不确定如何安装以前版本的 java。

非常感谢任何帮助!

需要此导入的特定代码示例:

private static WebElement webAction(final By locator) {
    Wait<WebDriver> wait = new FluentWait<WebDriver>(SharedSD.getDriver())
            .withTimeout(15, TimeUnit.SECONDS)
            .pollingEvery(1, TimeUnit.SECONDS)
            .ignoring(java.util.NoSuchElementException.class)
            .ignoring(StaleElementReferenceException.class)
            .ignoring(ElementNotFoundException.class)
            .withMessage(
                    "Web driver waited for 15 seconds but still could not find the element therefore Timeout Exception has been thrown");

    WebElement element = wait.until(new Function<WebDriver, WebElement>() {
        public WebElement apply(WebDriver driver) {
            return driver.findElement(locator);
        }
    });

pom.xml内容:

<parent>
    <groupId>ru.yandex.qatools.allure</groupId>
    <artifactId>allure-examples-parent</artifactId>
    <version>1.0</version>
</parent>

<groupId>CucumberSample</groupId>
<artifactId>com.cucumber.sample</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <allure.version>1.4.23</allure.version>
    <aspectj.version>1.8.9</aspectj.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.11</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.4.0</version>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-core</artifactId>
        <version>1.2.4</version>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>1.2.4</version>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>1.2.4</version>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-jvm-deps</artifactId>
        <version>1.0.5</version>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-testng</artifactId>
        <version>1.2.4</version>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-picocontainer</artifactId>
        <version>1.2.4</version>
    </dependency>
    <dependency>
        <groupId>net.masterthought</groupId>
        <artifactId>cucumber-reporting</artifactId>
        <version>2.1.0</version>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>gherkin</artifactId>
        <version>2.12.2</version>
    </dependency>
    <dependency>
        <groupId>ru.yandex.qatools.allure</groupId>
        <artifactId>allure-cucumber-jvm-adaptor</artifactId>
        <version>1.5.1</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.18.1</version>
            <configuration>

                <testFailureIgnore>false</testFailureIgnore>
                <argLine>
                    -javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar
                </argLine>
                <properties>
                    <property>
                        <name>listener</name>
                        <value>ru.yandex.qatools.allure.cucumberjvm.AllureRunListener</value>
                    </property>
                </properties>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjweaver</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-junit47</artifactId>
                    <version>2.13</version>
                </dependency>
            </dependencies>
        </plugin>

        <!--Needed only to show reports locally. Run jetty:run and
        open localhost:8080 to show the report-->
        <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>9.2.10.v20150310</version>
            <configuration>
                <webAppSourceDirectory>${project.build.directory}/site/allure-maven-plugin</webAppSourceDirectory>
                <stopKey>stop</stopKey>
                <stopPort>1234</stopPort>
            </configuration>
        </plugin>
    </plugins>
</build>

<reporting>
    <excludeDefaults>true</excludeDefaults>
    <plugins>
        <plugin>
            <groupId>ru.yandex.qatools.allure</groupId>
            <artifactId>allure-maven-plugin</artifactId>
            <version>2.0</version>
        </plugin>
    </plugins>
</reporting>

最佳答案

为了 Java9 兼容性,在 pom.xml 配置中可以改进的一些内容包括:-

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <configuration>
         <!-- specify current java version here: -->
         <source>9</source>
         <target>9</target> <!-- or edit these in your properties -->
    </configuration>
</plugin>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.20.1</version>
    <configuration> ... your configuration </configuration>
</plugin>
<小时/>

您还可以配置toolchains.xml也使用 JDK 版本 9,然后使用其自定义路径运行测试。

根据评论进行编辑 - 请确保您的 IntelliJ 版本也是 compatible with Java9. (如果您看到的错误是在 IntelliJ 执行中)

关于maven - 使用 Java 9 导入 java.util.function 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47322459/

相关文章:

java - Intellij Java Maven 本地主机 :8080 wont excecute method in controller

Maven 发布插件 : Doing the git push via https

java - Eclipse Maven 与 Jenkins 集成

maven-2 - 如何在 IDE 中使用 Grails 依赖项

scala - Travis CI 忽略 MAVEN_OPTS?

java.lang.ClassNotFoundException : text. 默认源

intellij-idea - Intellij 忽略选定的键盘布局

mysql - Sqoop使用Java API导入Hbase表

json - 如何在 React 中导入 json 文件

python - 在 Python 中动态导入模块并实例化类