Search code examples
javajavac

Package not found when running javac


I know that this question was already asked thousand times, but I still can't fully understand the point of the problem, especially in my case. So, I have simple project with dependency of TestNG and Selenium Java libraries, and I have these libraries installed globally, so my project just import them from "global" scope.

So to solve the problem I should add that global folder to my classpath ? Or this in not right from the beginning and I should not use libraries globally in projects ?

C:\Users\Yaroslav\IdeaProjects\GoogleSearchTest\src\main\java>javac GoogleSearchTest.java
GoogleSearchTest.java:1: error: package org.openqa.selenium does not exist
import org.openqa.selenium.By;
                          ^
GoogleSearchTest.java:2: error: package org.openqa.selenium does not exist
import org.openqa.selenium.WebDriver;
                          ^
GoogleSearchTest.java:3: error: package org.openqa.selenium does not exist
import org.openqa.selenium.WebElement;
                          ^
GoogleSearchTest.java:4: error: package org.openqa.selenium.chrome does not exist
import org.openqa.selenium.chrome.ChromeDriver;
                                 ^
GoogleSearchTest.java:5: error: package org.testng.annotations does not exist
import org.testng.annotations.BeforeClass;
                             ^
GoogleSearchTest.java:6: error: package org.testng.annotations does not exist
import org.testng.annotations.Parameters;
                             ^
GoogleSearchTest.java:7: error: package org.testng.annotations does not exist
import org.testng.annotations.Test;
                             ^
GoogleSearchTest.java:12: error: cannot find symbol
    private static WebDriver driver;
                   ^
  symbol:   class WebDriver
  location: class GoogleSearchTest
GoogleSearchTest.java:14: error: cannot find symbol
    @BeforeClass
     ^
  symbol:   class BeforeClass
  location: class GoogleSearchTest
GoogleSearchTest.java:23: error: cannot find symbol
    @Test
     ^
  symbol:   class Test
  location: class GoogleSearchTest
GoogleSearchTest.java:24: error: cannot find symbol
    @Parameters("queryText")
     ^
  symbol:   class Parameters
  location: class GoogleSearchTest
GoogleSearchTest.java:17: error: cannot find symbol
        driver = new ChromeDriver();
                     ^
  symbol:   class ChromeDriver
  location: class GoogleSearchTest
GoogleSearchTest.java:26: error: cannot find symbol
        WebElement searchField = driver.findElement(By.cssSelector("#lst-ib"));
        ^
  symbol:   class WebElement
  location: class GoogleSearchTest
GoogleSearchTest.java:26: error: cannot find symbol
        WebElement searchField = driver.findElement(By.cssSelector("#lst-ib"));
                                                    ^
  symbol:   variable By
  location: class GoogleSearchTest
GoogleSearchTest.java:28: error: cannot find symbol
        WebElement searchButton = driver.findElement(By.name("btnK"));
        ^
  symbol:   class WebElement
  location: class GoogleSearchTest
GoogleSearchTest.java:28: error: cannot find symbol
        WebElement searchButton = driver.findElement(By.name("btnK"));
                                                     ^
  symbol:   variable By
  location: class GoogleSearchTest
16 errors

GoogleSearchTest.java

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

public class GoogleSearchTest {
    private static WebDriver driver;

    @BeforeClass
    public void setup () {
        System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://www.google.com/");
    }

    @Test
    @Parameters("queryText")
    public void doSearch(String queryText) {
        WebElement searchField = driver.findElement(By.cssSelector("#lst-ib"));
        searchField.sendKeys(queryText);
        WebElement searchButton = driver.findElement(By.name("btnK"));
        searchButton.click();
    }
}

Solution

  • I finally understand how it should be done. So, to compile class and then run TestNG test I done this:

    javac -cp C:\Users\Yaroslav\IdeaProjects\GoogleSearchTest\lib\* GoogleSearchTest.java
    
    java -cp C:\Users\Yaroslav\IdeaProjects\GoogleSearchTest\src\main\java\;C:\Users\Yaroslav\IdeaProjects\GoogleSearchTest\lib\* org.testng.TestNG testng.xml
    

    More detailed, in first line to compile class there should be a following command template:

    javac -cp "full path to libs folder, where project libraries located" "name of class to compile"
    

    And for second line, which run TestNG test, template is:

    java -cp "full path to folder where testng.xml file located";"full path to libs folder, where project libraries located" "testNG filename with extension"
    

    And this is very tiresome, as you can see. I should learn proper way to run similar tests without headache ...

    P.S. After all, I just learn Maven and there is no need for these commands now )