Syed Rashid
Syed Rashid

Reputation: 31

Issue importing java.util.function using Java 9

I'm having an importing issue with:

import com.google.common.base.Function; 

When I run my test I receive an error that states:

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

So far I've learned that Java 8 uses these as replacements. However adding the following imports did not work:

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

They are underscored in red (Intelli-J) and I receive the following errors:

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 

Important things to note is that I'm using Java 9, I do not have any previous Java installed because this a new laptop. Also, this project was created using Java 8. I'm also using maven.

I'm guessing that I probably have to update my project some how maybe in the PomXML file to reflect what JDK I'm using.

I installed java using Homebrew and I'm not sure how to install previous version of java.

Any help is greatly appreciated!

example of specific code that requires this import:

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

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

Upvotes: 3

Views: 3866

Answers (1)

Naman
Naman

Reputation: 32046

Few things that can be improved in your pom.xml configuration definitely for Java9 compatibility includes:-

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

and

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

You can also configure a toolchains.xml with JDK version 9 as well and then run your tests using the custom path to it.

Edit from the comments - Do make sure your IntelliJ version is also compatible with Java9. (if the errors you see are in IntelliJ execution)

Upvotes: 3

Related Questions