我是Java EE 6的新手,正在学习Java EE 6教程。我创建了一个Java EE 6项目,该项目启用了上下文和DI。
我得到以下错误:
java.lang.NoClassDefFoundError:Lboundaries/UsersFacade
环境:Netbeans 7.3、JDK 1.6和Glassfish 3.1.2。请就解决这个问题向我提出建议。
(a) web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
(b) beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>
(c) 数据库中的实体Bean,用户(包名:用户ejb中的实体)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package entities;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author abc
*/
@Entity
@Table(catalog = "", schema = "USER")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"),
@NamedQuery(name = "Users.findByUserid", query = "SELECT u FROM Users u WHERE u.userid = :userid"),
@NamedQuery(name = "Users.findByUseridPassword", query = "SELECT u FROM Users u WHERE u.userid = :userid AND u.password=:password"),
@NamedQuery(name = "Users.findByUsername", query = "SELECT u FROM Users u WHERE u.username = :username"),
@NamedQuery(name = "Users.findByPassword", query = "SELECT u FROM Users u WHERE u.password = :password"),
@NamedQuery(name = "Users.findByEnabled", query = "SELECT u FROM Users u WHERE u.enabled = :enabled")})
public class Users implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(nullable = false)
private Integer userid;
@Size(max = 30)
@Column(length = 30)
private String username;
@Size(max = 30)
@Column(length = 30)
private String password;
@Size(max = 1)
@Column(length = 1)
private String enabled;
public Users() {
}
public Users(Integer userid) {
this.userid = userid;
}
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEnabled() {
return enabled;
}
public void setEnabled(String enabled) {
this.enabled = enabled;
}
@Override
public int hashCode() {
int hash = 0;
hash += (userid != null ? userid.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Users)) {
return false;
}
Users other = (Users) object;
if ((this.userid == null && other.userid != null) || (this.userid != null && !this.userid.equals(other.userid))) {
return false;
}
return true;
}
@Override
public String toString() {
return "entities.Users[ userid=" + userid + " ]";
}
}
(d) 实体Bean的会话Bean,UsersFacade(包名称:users-ejb中的边界)有方法:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package boundaries;
import entities.Users;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
/**
*
* @author abc
*/
@Stateless
public class UsersFacade extends AbstractFacade<Users> {
@PersistenceContext(unitName = "sbg-ejbPU")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
public UsersFacade() {
super(Users.class);
}
public Users validUser(String username, String password) {
Query query = getEntityManager().createNamedQuery("Users.findByUseridPassword").setParameter("username", username).setParameter("password", password);
Users user = (Users) query.getSingleResult();
return user;
}
}
(e) 创建JSF托管Bean、LoginBean(包名:users-war中的com.users)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.xyz;
import boundaries.UsersFacade;
import entities.Users;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
/**
*
* @author abc
*/
@Named(value = "loginBean")
@RequestScoped
public class LoginBean {
@EJB
private UsersFacade usersFacade;
private Users currentUser;
/**
* Creates a new instance of LoginBean
*/
public LoginBean() {
}
/**
* @param currentUser the currentUser to set
*/
public void setCurrentUser(Users currentUser) {
this.currentUser = currentUser;
}
/**
* @return the currentUser
*/
public Users getCurrentUser() {
if(currentUser==null){
currentUser= new Users();
}
return currentUser;
}
public String validUser(){
String outcome="index?faces-redirect=true";
Users user=usersFacade.validUser(getCurrentUser().getUsername(), getCurrentUser().getPassword());
if(user!=null){
outcome="/view/home?faces-redirect=true";
}else{
System.out.println("Unable to find User with User Id:"+getCurrentUser().getUsername()+"!");
}
return outcome;
}
}
也检查类路径
Issue-java.lang.NoClassDefFoundError
根本原因:环境变量部分中设置的Java路径不正确
解决方案:设置正确的JAVA_HOME路径
步骤->环境变量设置(My Comp右键单击->属性->环境变量->高级选项卡->变量)
创建新的JAVA_HOME环境变量。
JAVA_HOME。;C: \Program Files(x86)\Java\jdk1.6.0_14
在PATH变量部分设置JAVA_HOME变量。
路径%JAVA_HOME%\bin
在CLASSPATH变量中设置JAVA_HOME变量
CLASSPATH%JAVA_HOME%\jre\lib
重新启动系统
验证所有可变
echo%CLASSPATH%
echo%JAVA_HOME%
echo%PATH%
编译&运行程序