Question

So I wrote a class called Item and using Junit wrote a test called ItemTest and I want to execute my test from a command line. This is steps that I followed:

  1. set the path, to run java.exe and set the classpath to libraries

    ...> set path="C:\Program Files (x86)\Java\jdk1.7.0_40\bin"
    ...> set classpath=C:\Program Files (x86)\Java\hamcrest-core-1.3.jar; C:\Program Files (x86)\Java\junit-4.11.jar
    
  2. then i simply run org.junit.runner.JUnitCore to see if added libs were in classpath

    ...>java org.junit.runner.JUnitCore
    

    the output was

    JUnit version 4.11
    Time: 0,001
    OK (0 tests)
    
  3. Then I compiled without an error Item and ItemTest

    C:\Users\User\IdeaProjects\My_project\src>javac .\main\java\Item.java .\test\java\ItemTest.java
    
  4. Then try ro run this classes

    C:\Users\User\IdeaProjects\My_project\src>java org.junit.runner.JUnitCore main.java.Item test.java.ItemTest
    
  5. In the end I got this

    JUnit version 4.11
    Exception in thread "main" java.lang.NoClassDefFoundError: main/java/Item (wrong name: Item)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14 2)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    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:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:190)
    at org.junit.runner.JUnitCore.runMain(JUnitCore.java:86)
    at org.junit.runner.JUnitCore.runMainAndExit(JUnitCore.java:47)
    at org.junit.runner.JUnitCore.main(JUnitCore.java:40)
    

    How to fix this ? Should I put Itemin classpath ?

UPD#1

Put this path in my classpath doesn't work (with and without src):

    C:\Users\User\IdeaProjects\My_project\src> 

UPD#2

When I want to run the class I adress to him as he stores in file system or in what packages he lays ? Because I don't have packages and so My_project apart from being project name is a module name.

SOLVED

This is the way - I add to classpath next paths:

  1. The path to class Item:

    C:\Users\User\IdeaProjects\My_project\src\main\java\;
    
  2. The path to test class ItemTest:

    C:\Users\User\IdeaProjects\My_project\src\test\java\;
    
  3. Then simply run:

    ...>java org.junit.runner.JUnitCore ItemTest
    
Was it helpful?

Solution

You have to put the location of the .class files in the classpath. Even if you are in the same folder it won't find them if they are not in the classpath. That comes from DOS x .... anyone else.

In DOS the OS looks first in the current folder. Everywhere else the OS looks first in the path (or classpath for Java).

If Item is in the package com.example.Item and the class is in c:\users\test\myproj\target\com\example\Item.class then you must add c:\users\test\myproj\target\ to the classpath

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top