Deniz
Deniz

Reputation: 41

Assistance needed getting basic JUnit 4 setup to work using java files along with the corresponding command line tools

I am trying to get a basic JUnit 4 setup working using java files along with the corresponding command line tools (before I move on to eclipse - which I suspect will be easier to get working but I still want to do it this way first) and what follows is self-explanatory input/output that I think will bring out the issue(s):

deniz@debian:~$ cd /tmp/temp2/src/com/example/example
deniz@debian:/tmp/temp2/src/com/example/example$ echo $CLASSPATH
.:/home/deniz/CLASSPATH_DIR:/usr/share/java/jogl.jar:/usr/share/java/gluegen-rt.jar:/usr/share/java/junit4.jar
deniz@debian:/tmp/temp2/src/com/example/example$ ls -l /usr/share/java/junit4.jar
lrwxrwxrwx 1 root root 16 Feb  8  2011 /usr/share/java/junit4.jar -> junit4-4.8.2.jar
deniz@debian:/tmp/temp2/src/com/example/example$ ls
MathUtils.java  MathUtilsTest.java
deniz@debian:/tmp/temp2/src/com/example/example$ javac ./*.java
deniz@debian:/tmp/temp2/src/com/example/example$ ls
MathUtils.class  MathUtils.java  MathUtilsTest.class  MathUtilsTest.java
deniz@debian:/tmp/temp2/src/com/example/example$ java MathUtils
Exception in thread "main" java.lang.NoClassDefFoundError: MathUtils (wrong name: com/example/example/MathUtils)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:334)
Could not find the main class: MathUtils. Program will exit.
deniz@debian:/tmp/temp2/src/com/example/example$ java MathUtilsTest
Exception in thread "main" java.lang.NoClassDefFoundError: MathUtilsTest (wrong name: com/example/example/MathUtilsTest)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:334)
Could not find the main class: MathUtilsTest. Program will exit.
deniz@debian:/tmp/temp2/src/com/example/example$ cat MathUtils.java
package com.example.example;

public class MathUtils
{
    public static double multiply(double a, double b)
    {
        return a * b;
    }

    public static void main(String[] args)
    {
        double num = multiply(4.0,-5.0);
        System.out.println("The number is: " + num);
    }
}
deniz@debian:/tmp/temp2/src/com/example/example$ cat MathUtilsTest.java 
package com.example.example;

import org.junit.Test;

import static org.junit.Assert.*;

public class MathUtilsTest
{
    @Test
    public void testMultiply()
    {
        double a = 5.0;
        double b = -4.0;

        double expected = -20.0;
        double result = MathUtils.multiply(a, b);

        assertEquals(expected, result, 0.00001);
    }
}
deniz@debian:/tmp/temp2/src/com/example/example$

Could someone please tell me what's going on and how to solve this? Even the MathUtils class doesn't work and it seems well done to me; it has the package defined and has a main method.

If more information than what I provided is needed, just ask.

I apologize if I made any stupid mistakes and/or assumptions because I am extremely tired as I type this; please correct me nonetheless.

Any help would be greatly appreciated! Thanks in advance!

Upvotes: 0

Views: 516

Answers (2)

Heinrich
Heinrich

Reputation: 408

First try: Use the complete class name (with package)

java com.example.example.MathUtils

Second try: Add the classpath

java -cp . com.example.example.MathUtils

And mind the directory structure

To run the tests you need to follow the instructions in Running tests

Upvotes: 0

Matthew Farwell
Matthew Farwell

Reputation: 61715

Your MathUtilsTest has a package declaration:

package com.example.example;

For various reasons, this means that your physical file must be in com/example/example/MathsUtilsTest. So you need to run java from /tmp/temp2/src/. java should be able to find the classes correctly then.

It's a good idea to always run javac from that directory as well.

Upvotes: 2

Related Questions