3

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>

6
  • What is your module-inf.java content and could you update with minimal code to reproduce this as well? Please update with pom.xml as well. Commented Nov 16, 2017 at 5:52
  • I've updated example code and Pom.xml but I'm not sure how to get module-inf.java. Commented Nov 16, 2017 at 5:59
  • 2
    What maven-compiler-plugin version are you(or the build) using, would also be good to know what mvn command did you execute such that the failure occurs. Specific logs are always good to point to specific cause. In addition to which could you try maven-surefire-plugin version 2.19.1?(for Java9 compatibility) Commented Nov 16, 2017 at 6:02
  • 3
    Which Version of IntelliJ are you using? For Java 9 best use the newest one (2017.2.6). Commented Nov 16, 2017 at 7:32
  • 2
    @Nicolai Good point and on that note, anything above 2017.2.3 has been compatible with Java9. Commented Nov 16, 2017 at 8:48

1 Answer 1

3

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)

Sign up to request clarification or add additional context in comments.

1 Comment

I could not afford any more delays so I had to resort to downgrading to Java 8 which worked. I promise to revisit this later after my project, thank you for your help & including everyone else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.