当前位置: 代码迷 >> J2SE >> 为什么小弟我用Class.forName()会报java.lang.ClassNotFoundException
  详细解决方案

为什么小弟我用Class.forName()会报java.lang.ClassNotFoundException

热度:109   发布时间:2016-04-24 14:57:16.0
为什么我用Class.forName()会报java.lang.ClassNotFoundException
package   services;

import   org.apache.commons.logging.Log;
import   org.apache.commons.logging.LogFactory;

public   class   BusinessService   {
private   final   static   String   DEFUL_IMP   =   "HibernateFactory ";

private   static   Log   log   =   LogFactory.getLog(BusinessService.class);

private   static   JliveBBS   service   =   null;

public   static   JliveBBS   instance()   {
if   (service   ==   null)   {
//   load   service   implement   class   from   config   file
String   classStr   =   null;
if   (classStr   ==   null   ||   classStr.length()   <   1)   {
if   (log.isDebugEnabled())
log.debug( "load   implement   Factory   for   defullclass: "
+   DEFUL_IMP);
classStr   =   DEFUL_IMP;
}
try   {
Class   serviceImp   =   Class.forName(classStr);
service   =   (JliveBBS)   serviceImp.newInstance();
}   catch   (ClassNotFoundException   e)   {
log.info( "don 't   instantiate   Factory   class: "   +   classStr);
}   catch   (InstantiationException   e)   {
//   TODO   Auto-generated   catch   block
e.printStackTrace();
}   catch   (IllegalAccessException   e)   {
//   TODO   Auto-generated   catch   block
e.printStackTrace();

}
}

return   service;
}
}
********************************************************
这个是测试的类:
package   HibernateTestGroup;

import   java.util.*;

import   forms.Boards;
import   forms.Groups;
import   forms.Users;

import   DAO.*;
import   services.BusinessService;

public   class   TestGroup     {
public   static   GroupManager   mng   =   BusinessService.instance()
.getGroupManager();


public   void   addgroup(){
Groups   group=new   Groups();
group.setGroupname( "   华灯初上 ");
group.setDesc( "物尽繁华尽先孤独 ");
group.setCreatedate(new   Date());
try{
mng.addGroup(group);
}catch(Exception   e){
e.printStackTrace();
}
}
public   void   loadallgroup()   {

List   list   =   mng.loadAllGroups(new   Users());
System.out.println( "- ");
for   (Iterator   iter   =   list.iterator();   iter.hasNext();)   {
Groups   group   =   (Groups)   iter.next();
System.out.print(group.getGroupname());
List   boards   =   (List)   group.getBoards();
for   (Iterator   iterator   =   boards.iterator();   iterator.hasNext();)   {
Boards   board   =   (Boards)   iterator.next();
System.out.println( "     + "   +   board.getBoardname());
}
System.out.println( "- ");
}

}
public   static   void   main(String   args[]){
TestGroup   test=new   TestGroup();
test.addgroup();
  相关解决方案