使用Java 9导入java.util.function的问题

时间:2017-11-16 05:44:33

标签: 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文件中更新我的项目,以反映我正在使用的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>

1 个答案:

答案 0 :(得分:3)

在您的pom.xml配置中,几乎可以为Java9兼容性改进的内容包括: -

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

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

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