0

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;

  • Why javac not imports any classes?
  • How can I correctly compile the test classes from command line via javac and lzunch them via java?
Valentyn Hruzytskyi
  • 1,772
  • 5
  • 27
  • 59

1 Answers1

1

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 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. I am did edit my question – Valentyn Hruzytskyi Sep 19 '19 at 15:18
  • I won't argue. However `-cp .;c:...` and `-cp c:...` do mean different things. The latter won't work if your `ColorTest` class depends on other compiled classes in the directory tree rooted at the current directory (`.`) – Stephen C Sep 19 '19 at 15:32
  • > That does not make sense. You use javac to compile classes. To run them you use java. - How can I use `java` command, but not compile `.java` classes with `javac`? – Valentyn Hruzytskyi Sep 20 '19 at 05:57
  • I read oracle tutorial (https://docs.oracle.com/en/java/javase/13/docs/specs/man/javac.html), so 1) `If --class-path, -classpath, or -cp are not specified, then the user class path is the value of the CLASSPATH environment variable, if that is set, or else the current directory.` - I well set the current dirrectory in command – Valentyn Hruzytskyi Sep 20 '19 at 06:58
  • 2) `Use the --class-path option to specify libraries to be placed on the class path. Locations on the class path should be organized in a package hierarchy. You can also use alternate forms of the option: -classpath or -cp.` - I well set the path to package/packages (`c:\full\path\to\junit-4.12.jar` or `....\repositories\*`) – Valentyn Hruzytskyi Sep 20 '19 at 07:02
  • There's nothing else about `-cp` or `.:` in https:\\docs.oracle.com/en/java/javase/13/docs/specs/man/javac.html . Can I read anymore else? – Valentyn Hruzytskyi Sep 20 '19 at 07:06
  • There won't be anything *specific* about `.:`. The `.` is simply the standard filename for the current directory, relative to itself. (UNIX invented it, MS-DOS borrowed it. Windows inherited it.) – Stephen C Sep 20 '19 at 07:32
  • Read these too: https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html https://docs.oracle.com/javase/8/docs/technotes/tools/windows/findingclasses.html – Stephen C Sep 20 '19 at 07:36
  • "How can I use java command, but not compile .java classes with javac?" I didn't invent the names of the commands! But anyway, you miss the point. You compile XXX.java to XXX.class using `javac`. You run a XXX.class using `java`. The link I gave you explains specifically how to run Junit tests ... using the `java` command. – Stephen C Sep 20 '19 at 07:41