hhrzc
hhrzc

Reputation: 2059

javac unable to import classes in project

I am trying to launch a test from the windows command line using the following way described in many tutorials:

javac -cp .;"c:\full\path\to\junit-4.12.jar" test\java\ColorTest.java

but in the result I have compilation errors in each string:

public class ColorTest extends BaseTest{
                               ^
  symbol: class BaseTest
src\test\java\ColorTest.java:14: error: cannot find symbol
        homePage.goToHomePage();
        ^
  symbol:   variable homePage
  location: class ColorTest
src\test\java\ColorTest.java:15: error: cannot find symbol
        homePage.moveToElement(HomePage.letterR, X_OFFSET, Y_OFFSET);
                               ^
  symbol:   variable HomePage
  location: class ColorTest
src\test\java\ColorTest.java:15: error: cannot find symbol
        homePage.moveToElement(HomePage.letterR, X_OFFSET, Y_OFFSET);
        ^

When I add to command all my classes (BaseTest, HomeTest...) or use *.java in command - I am did get errors on each other class (WebDriver, ArrayList, etc).

Also try to joins all libraries from my local repo:

javac -cp .;"c:\full\path\to\.m2\repositories\*" test\java\ColorTest.java

But in this case javac did not see even junit package in first string.

src\test\java\ColorTest.java:1: error: package org.junit does not exist
import org.junit.Test;
                ^
src\test\java\ColorTest.java:2: error: package org.junit does not exist
import org.junit.Assert;

Upvotes: 0

Views: 624

Answers (1)

Stephen C
Stephen C

Reputation: 719576

Since you are compiling on Windows, you need to use a Windows compatible classpath separator:

javac -cp .;c:\full\path\to\junit-4.12.jar test\java\ColorTest.java

or possibly

javac -cp .;"c:\full\path\to\junit-4.12.jar" test\java\ColorTest.java

Note: semicolon not colon.


My better way: javac -cp c:\full\path\to\junit-4.12.jar test\java\ColorTest.java. In this case I not get errors in import .... strings.

But you still got errors for the classes that NOT in the Junit JAR file. Right?

IMO, what you need to do is:

  • read the Oracle manual page for the javac command to understand -cp and possibly -d and/or -sourcepath.

  • read the Oracle manual page for the Classpath to understand what this all actually means.

The classpath is what allows javac to find the compiled versions of the other classes that your code depends on. It is an important concept. It is a good idea to really understand it ... rather than relying on stuff copied from dubious or non-applicable examples.

In your case, your unit test classes will depend on Junit classes AND on the compiled classes that your test code is trying to test. If the directory tree containing the latter is not on the classpath, or they have not been compiled, then javac will complain.

Now I can't work out if are still having "cannot find symbol" compilation errors. But if you are, and the classes that javac can't find really do exist, then either you haven't compiled them or they are not on the classpath ... in the right way.


Finally you ask:

How can I correctly launch the tests from command line via javac?

That does not make sense. You use javac to compile classes. To run them you use java.

This Q&A explains how to run Junit tests:

And note that you need to specify the classpath correctly with java as well.

Upvotes: 1

Related Questions