Home > OS >  How can I solve that type of issue when I compile using `javac`: " error: cannot find symbol [.
How can I solve that type of issue when I compile using `javac`: " error: cannot find symbol [.

Time:07-17

For starters, I have to say that I am using IntelliJ IDEA Community Edition 2020.3.1 and running java 15.0.1 2020-10-20, also when I run my program after enabling assertions and clicking on the run button, it works as expected. That being said here is my file structure:

file structure screenshot

Here is the code in my TestRunner.java file:

import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;

public final class TestRunner {
    private static final List<Class<?>> TESTS = List.of(CalculatorTest.class);


    public static void main(String[] args) throws Exception {

        List<String> passed = new ArrayList<>();
        List<String> failed = new ArrayList<>();

        for (Class<?> klass : TESTS) {

           if(!UnitTest.class.isAssignableFrom(klass)){
               throw new IllegalArgumentException("Class "  klass   " must implement UnitTest");
           }

           for(Method method : klass.getDeclaredMethods()){
               if(method.getAnnotation(Test.class) != null){
                   try{

                       UnitTest test = (UnitTest) klass.getConstructor().newInstance();

                       test.beforeEachTest();
                       method.invoke(test);
                       System.out.println(method.invoke(test));
                       test.afterEachTest();
                       passed.add(getTestName(klass, method));

                   }catch(Throwable throwable){
                       failed.add(getTestName(klass, method));
                   }
               }
           }
        }

        System.out.println("Passed tests: "   passed);
        System.out.println("FAILED tests: "   failed);
    }

    private static String getTestName(Class<?> klass, Method method) {
        return klass.getName()   "#"   method.getName();
    }
}

Here are my issues:

  1. When I compile my main class TestRunner.java using javac TestRunner.java, it fails to find those symbols CalculatorTest.class, UnitTest.class, Test.class, UnitTest. Here is the error message: Error Message

  2. When I use javac *.java though, my files compile and my .class files are generated, here is a screenshot:

Screeshot

but when I try to run my file using java TestRunner it says: "Error could not find or load class TestRunner, here is a screenshot:

screenshot

If anyone can help me solve those issues I'd would be very happy. So far, I have found no solutions when I googled about them. Thank you!

CodePudding user response:

After reading many answers, I found that the solution was simple.

First compiling TestRunner.java:

javac -cp . TestRunner.java

Then running TestRunner (containing my main function):

java -cp . -ea TestRunner

It turns out I was missing on the dot "."!

Here is the final result:

screenshot

  • Related