123 123 - 17 days ago 6
Java Question

Maven can't find class even though package is included

I have the following code:

package osu.cs362.URLValidator;

import static org.junit.Assert.*;
import org.junit.Test;
import java.util.*;
import static org.mockito.Mockito.*;

public class DomainValidatorTest {
RegexValidator rev = mock(RegexValidator.class);
}


This includes the package
osu.cs362.URLValidator
which contains
RegexValidator
.



However, when running
mvn test
I get:


cannot find symbol class RegexValidator


Why can't it find this class? Is this a
pom.xml
issue?

Answer

It is not a Maven problem.

If the RegexValidator class had the declaration like that :

package osu.cs362.URLValidator;
public class RegexValidator {
...
}

you would have not the problem. So I suppose it is not the case.
Besides, filesytem folders are not Java packages.

For example, nobody prevents you from declaring your class in the folder : osu/cs362 of your classpath folder and declaring the package of the class like that: fictive.folder.
The class will compile.

It is the case for DomainValidatorTest. The package is not symmetric with the folder layout but the class is found by the compiler and it doesn't cause a compilation error.

But of course, it is a bad practice and it is misleading. That's why packages should always be symmetric to the folders layout.