minyatur
minyatur

Reputation: 5

Class.forname Exception

all I'am trying to run a junit tests in this way:

I've these files:

./tested/Resta.java
./tested/Suma.class
./tested/Suma.java
./tested/Resta.class
./test/TestResta.class
./test/TestSuma.java
./test/TestSuma.class
./test/TestResta.java
./Spumevaluador.class
./Spumevaluador.java

When I run Spumevaluador (java -cp /home/minyatur/.netbeans/7.2/modules/ext/junit-4.10.jar:. Spumevaluador) I get this error:

minyatur@orome:~/codigo$ ./ejecuta.sh Hola Exception in thread "main" java.lang.NoClassDefFoundError: test/TestSuma (wrong name: TestSuma) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:787) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:447) at java.net.URLClassLoader.access$100(URLClassLoader.java:71) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:423) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:356) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:188) at Spumevaluador.main(Spumevaluador.java:45)

The Spumevaluador.java code:

import org.junit.runner.JUnitCore;
import java.util.Iterator;
import org.junit.internal.TextListener;
import org.junit.runner.Computer;
import org.junit.runner.Request;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import java.util.ArrayList;

public class Spumevaluador {

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

    System.out.println("Hola");
        ArrayList clases = new ArrayList();
        clases.add("test.TestSuma");
        clases.add("test.TestResta");        
        Class<?>[] cClases = new Class<?>[clases.size()];
        for (int i = 0; i < clases.size(); i++) {
            cClases[i] = Class.forName(clases.get(i).toString());
        }
        Request req = Request.classes(cClases);
        JUnitCore jUnitCore = new JUnitCore();
        jUnitCore.addListener(new TextListener(System.out));
        jUnitCore.run(req);
    }
}

It seems the invocation to Class.forname is not finding test.TestSuma, if I change Class.forname("TestSuma"); and include ../test in classpath it works fine.

Why? Any ideas?

Upvotes: 0

Views: 4889

Answers (2)

Eric Galluzzo
Eric Galluzzo

Reputation: 3241

It sounds like your TestSuma class is defined in the default package instead of the test package. Try adding:

package test;

to the top of the TestSuma.java file.

The Java classpath contains the locations where Java can find the root of the package hierarchy. If you have a class in the test package, you need to place the directory containing a "test" subdirectory in the classpath, not the test subdirectory itself.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1503649

Basically when you run Class.forName("test.TestSuma") it's trying to load a class called TestSuma in a package called test. It's looking for that in a class file called TestSuma.class in a directory called test.

It finds the file - but the class that it loads is actually called TestSuma in the default package... so it fails.

It's not really clear what you're trying to achieve, but you should keep the class files in a structure which matches the package structure, and only try to load files using their proper fully-qualified class name.

Upvotes: 2

Related Questions