Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[junit] java.lang.ClassNotFoundException: UserActivityTest

 
Haifeng Jiang
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone!
When I 'ant test' in command line to run my Junit test class, I continue getting ClassNotFoundException.

My build.xml is as below:


The output when 'ant test' in command line:


I am sure that the UserActivityTest.class is under ${class.dir} directory. Could anyone help on this? Thanks so much!
 
Haifeng Jiang
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I modified the build.xml file:


But when run 'ant test', I still got error:


This time the error should be caused by my test class. But I don't know what's wrong in my test class, could you please have a look?

BTW, when I use Eclipse to 'Run As -> JUnit Test', it works fine.
 
Haifeng Jiang
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I go further...
but still got problem with the 'file path'

see here:
File file = new File("../config/log4j.properties"); //in this way, ant works, but use eclipse run ant doesn't work
File file = new File("./config/log4j.properties"); //in this way, ant doesn't work, but eclipse works
Anyone who know the solution, please help!!!
 
Haifeng Jiang
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seems I am helping myself to fix the problem.

The error is caused by the path of build.xml. When I put it under <project_dir>/ant/ , Ant didn't work. So I put it under <project_dir>, then Ant works for me.
It caused by the relative path.
<project name="XXX" basedir="." default="run"> -- in build.xml
public static final String LOG4J_CONFIG_FILE_PATH = "./config/log4j.properties"; --in my Java class

build.xml should always put under project directory directly. In this way, it will avoid some unnecessary troubles.
Also http://ant.apache.org/manual/Tasks/java.html, it help me a lot!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic