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();
}
}
***********************************************************
HibernateFactory类路径:
eclipse -src/services/HibernateFactory.java
------解决方法--------------------------------------------------------
源文件放类路径里有啥用?不会编译?现在的人基础越来越差了
------解决方法--------------------------------------------------------
在eclipse中,把HibernateFactory.java所在的package的父目录加到classpath中就可以了.例如:假设你的HibernateFactory.java所在的package为:services,则你只要把src目录的路径加到classpath中就可以了.Hope this help.