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();
}
}
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 )
Related
Note: I can successfully compile everything else besides RegisterTest.java
I have the following project structure. I want to compile this using javac only, no build tools, no IDEs.
Each file is in a package corresponding to their directory structure, for example Drink.java is in Kassensystem/AbstractProducts which translates to package Kassensystem.AbstractProducts;
Project Structure
All abstract products implement the common interface Product which is in the Interfaces folder, so they naturally import Kassensystem.Interfaces.*. Finally the Products are the concrete classes derived from the abstract product classes, so they in turn import Kassensystem.AbstractProducts.*. Finally the Register class just calculates some prices and only depends on the Product interface. RegisterTest should test the whole system and instantiates some concrete classes and passes them to Register.
So the dependency hierarchy is as follows:
Dependency Hierarchy
I'm having trouble compiling this, how would I need to set the classpath to get the right dependency order (note I'm not using jar files)?
I tried setting the classpath such that the most abstract classes come first (Interfaces -> Abstract Classes -> Concrete Products -> Register) but it's always complaining at the import statements, that the package does not exist.
$ javac -cp "Interfaces/*.class:AbstractProducts/*.class:Products/*.class" RegisterTest.java
RegisterTest.java:3: error: package Kassensystem.Products does not exist
import Kassensystem.Products.*;
^
RegisterTest.java:4: error: package Kassensystem.Interfaces does not exist
import Kassensystem.Interfaces.*;
^
RegisterTest.java:8: error: cannot find symbol
var products = new Product[] { new Tomato(), new TomatoJuice() };
^
symbol: class Product
location: class RegisterTest
RegisterTest.java:8: error: cannot find symbol
var products = new Product[] { new Tomato(), new TomatoJuice() };
^
symbol: class Tomato
location: class RegisterTest
RegisterTest.java:8: error: cannot find symbol
var products = new Product[] { new Tomato(), new TomatoJuice() };
^
symbol: class TomatoJuice
location: class RegisterTest
RegisterTest.java:10: error: cannot find symbol
double total = Register.scan(products);
^
symbol: variable Register
location: class RegisterTest
6 errors
I have a simple problem I don't know how to solve!
I have a single Java file User.java:
import java.util.Vector;
public class User {
private String name;
private Vector<User> friends;
public User(String name) {
this.name = name;
this.friends = new Vector<>();
}
public void addFriend(User newfriend) {
friends.add(newfriend);
}
public boolean isFriendsWith(User friend) {
return friends.indexOf(friend) != -1;
}
}
and I have a simple test class UserTest.java beside this class:
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class UserTest {
#Test
public void evaluatesExpression() {
User user = new User("foo");
User user2 = new User("bar");
user.addFriend(user2);
assertEquals(true, user.isFriendsWith(user2));
}
}
I want to run this test class for the User class.
I'm not using IDEs like IntelliJ or Eclipse, so I want to compile the test from linux command line, but this command:
javac -cp .:"/usr/share/java/junit.jar" UserTest.java
gives me the following errors:
UserTest.java:1: error: package org.junit does not exist
import static org.junit.Assert.assertEquals;
^
UserTest.java:1: error: static import only from classes and interfaces
import static org.junit.Assert.assertEquals;
^
UserTest.java:2: error: package org.junit does not exist
import org.junit.Test;
^
UserTest.java:6: error: cannot find symbol
#Test
^
symbol: class Test
location: class UserTest
UserTest.java:11: error: cannot find symbol
assertEquals(true, user.isFriendsWith(user2));
^
symbol: method assertEquals(boolean,boolean)
location: class UserTest
5 errors
Note: everything I have seen on Stackoverflow is about testing a single file in a project or building and testing with gradle and ..., but I don't know much about Java and I don't need to know much, I just need to know the simplest way to create and run a test for a single Java class.
Note2: I have installed junit with apt install junit and it installed junit-3-8-2 version.
Note3: I have problems when trying to compile my test class, I haven't even reached the stage where I can run the tests!
After quite a lot of trial and error in the comments section, the root cause was an old JUnit 3.8.2 dependency. The 3.x releases used a different namespace that was changed in 4.x to org.junit.
Therefore the classes where not found while compiling the test.
To debug such issues unzipping the jar with unzip on Linux can be helpful.
When I run my project in Netbeans 8.1 nothing goes wrong. However, when I build it to a .jar file, there are 34 errors of missing packages and symbols all referring to JFreeChart. Couple of these errors:
C:NetBeansProjects\Program\src\org\jfree\chart\servlet\ChartDeleter.java:51: error: package javax.servlet.http does not exist
import javax.servlet.http.HttpSessionBindingEvent;
C:NetBeansProjects\Program\src\org\jfree\chart\servlet\ChartDeleter.java:52: error: package javax.servlet.http does not exist
import javax.servlet.http.HttpSessionBindingListener;
C:\NetBeansProjects\Program\src\org\jfree\chart\servlet\ChartDeleter.java:58: error: cannot find symbol
public class ChartDeleter implements HttpSessionBindingListener, Serializable {
symbol: class HttpSessionBindingListener
C:\NetBeansProjects\Program\src\org\jfree\chart\servlet\ChartDeleter.java:98: error: cannot find symbol
public void valueBound(HttpSessionBindingEvent event) {
symbol: class HttpSessionBindingEvent
location: class ChartDeleter
etc.....
My code is too long to post here (6000+ lines) and contains Java swing and some charts. Everything worked fine, but the charts made these errors appear. What's the reason for this?
"javax.servlet.http does not exist", add servletapi.jar to your classpath
I'm not very familiar with Andorid and Gradle. I'm trying to run this unit test from the same project after removing the #Ignore annotation. But I'm getting the following error
java.lang.AssertionError: Compilation produced the following errors:
/SOURCE_OUTPUT/com/example/MainActivity$$Aftermath.java:6: error: cannot find symbol
import org.michaelevans.aftermath.IAftermathDelegate;
^
symbol: class IAftermathDelegate
location: package org.michaelevans.aftermath
/SOURCE_OUTPUT/com/example/MainActivity$$Aftermath.java:8: error: cannot find symbol
public class MainActivity$$Aftermath<T extends com.example.MainActivity> implements IAftermathDelegate<T> {
^
symbol: class IAftermathDelegate
can someone please point out to me how to solve the problem?
Need Some help. Though there are lot of different answers available and also I tried them but couldn't make it work. I intsalled hadoop locally in my mac os and when I tried compiling the java programs I got the following errors. I know the problem is with the setting up the correct class path, but in may case providing the class path didn't make it work. I have installed hadoop under /usr/local/Cellar/hadoop/1.2.1/libexec
I have my java home set to export JAVA_HOME="$(/usr/libexec/java_home)"
and class path set to export HADOOP_CLASSPATH=${HADOOP_HOME}/bin:${JAVA_HOME}/bin:${PATH}
but still getting the below errors. Any suggestions for the setting up the correct class path would be appreciated.
LineIndexer.java:6: package org.apache.hadoop.io does not exist
import org.apache.hadoop.io.LongWritable;
^
LineIndexer.java:7: package org.apache.hadoop.io does not exist
import org.apache.hadoop.io.Text;
^
LineIndexer.java:8: package org.apache.hadoop.mapred does not exist
import org.apache.hadoop.mapred.FileInputFormat;
^
LineIndexer.java:9: package org.apache.hadoop.mapred does not exist
import org.apache.hadoop.mapred.FileOutputFormat;
^
LineIndexer.java:10: package org.apache.hadoop.mapred does not exist
import org.apache.hadoop.mapred.FileSplit;
^
LineIndexer.java:11: package org.apache.hadoop.mapred does not exist
import org.apache.hadoop.mapred.JobClient;
^
LineIndexer.java:12: package org.apache.hadoop.mapred does not exist
import org.apache.hadoop.mapred.JobConf;
^
LineIndexer.java:13: package org.apache.hadoop.mapred does not exist
import org.apache.hadoop.mapred.MapReduceBase;
^
LineIndexer.java:14: package org.apache.hadoop.mapred does not exist
import org.apache.hadoop.mapred.Mapper;
^
LineIndexer.java:15: package org.apache.hadoop.mapred does not exist
import org.apache.hadoop.mapred.OutputCollector;
^
LineIndexer.java:16: package org.apache.hadoop.mapred does not exist
import org.apache.hadoop.mapred.Reducer;
^
LineIndexer.java:17: package org.apache.hadoop.mapred does not exist
import org.apache.hadoop.mapred.Reporter;
^
LineIndexer.java:21: cannot find symbol
symbol : class MapReduceBase
location: class LineIndexer
public static class LineIndexMapper extends MapReduceBase
^
LineIndexer.java:22: cannot find symbol
symbol : class Mapper
location: class LineIndexer
implements Mapper {
^
LineIndexer.java:22: cannot find symbol
symbol : class LongWritable
location: class LineIndexer
implements Mapper {
^
LineIndexer.java:22: cannot find symbol
symbol : class Text
location: class LineIndexer
implements Mapper {
^
Looks like your classpath is wrong, try this instead:
javac -classpath /usr/local/cellar/hadoop-1.2.1/hadoop-core-1.2.1.jar
Or redefine your HADOOP_HOME env variable to be /usr/local/cellar/hadoop-1.2.1