在 java 应用程序中实现自定义 jar 时出错
Getting error for implementing custom jar in java application
我为我的自定义编码创建了新的 jar 文件,当我将这个 jar 文件添加到我的新项目时它抛出了 "package does not exist" 和 "cannot find symbol" 错误。
jar 文件有一个 class,
AbstractDao.class
package kar;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.Query;
import java.util.List;
public abstract class AbstractDao {
private Session session;
private Transaction tx;
public AbstractDao() {
HibernateFactory.buildIfNeeded();
}
protected void saveOrUpdate(Object obj) {
try {
startOperation();
session.saveOrUpdate(obj);
tx.commit();
} catch (HibernateException e) {
handleException(e);
} finally {
HibernateFactory.close(session);
}
}
protected void delete(Object obj) {
try {
startOperation();
session.delete(obj);
tx.commit();
} catch (HibernateException e) {
handleException(e);
} finally {
HibernateFactory.close(session);
}
}
protected Object find(Class clazz, Long id) {
Object obj = null;
try {
startOperation();
obj = session.load(clazz, id);
tx.commit();
} catch (HibernateException e) {
handleException(e);
} finally {
HibernateFactory.close(session);
}
return obj;
}
protected List findAll(Class clazz) {
List objects = null;
try {
startOperation();
Query query = session.createQuery("from " + clazz.getName());
objects = query.list();
tx.commit();
} catch (HibernateException e) {
handleException(e);
} finally {
HibernateFactory.close(session);
}
return objects;
}
protected void handleException(HibernateException e) throws DataAccessLayerException {
HibernateFactory.rollback(tx);
throw new DataAccessLayerException(e);
}
protected void startOperation() throws HibernateException {
session = HibernateFactory.openSession();
tx = session.beginTransaction();
}
}
我的实现 class 是,
UserService.java
package obs.service;
import kar.AbstractDao;
import kar.DataAccessLayerException;
import obs.domain.User;
import org.springframework.stereotype.Service;
@Service("IUserService")
public class UserService extends AbstractDao {
public UserService() {
super();
}
public void create(User event) throws DataAccessLayerException {
super.saveOrUpdate(event);
}
}
这里我将 AbstractDao.class 文件放入 jar 中,在 UserService.java 中我实现了 AbstractDao class.
这是我得到的错误,
您出错的可能原因是您没有将自定义 jar 正确安装到本地 maven 存储库。
按如下方式将 jar 安装到本地存储库。
mvn install:install-file
-Dfile=<path-to-file>
-DgroupId=<group-id>
-DartifactId=<artifact-id>
-Dversion=<version>
-Dpackaging=<packaging>
-DgeneratePom=true
Where: <path-to-file> the path to the file to load
<group-id> the group that the file should be registered under
<artifact-id> the artifact name for the file
<version> the version of the file
<packaging> the packaging of the file e.g. jar
也看到这个问题How to add local jar files in maven project?
我为我的自定义编码创建了新的 jar 文件,当我将这个 jar 文件添加到我的新项目时它抛出了 "package does not exist" 和 "cannot find symbol" 错误。
jar 文件有一个 class, AbstractDao.class
package kar;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.Query;
import java.util.List;
public abstract class AbstractDao {
private Session session;
private Transaction tx;
public AbstractDao() {
HibernateFactory.buildIfNeeded();
}
protected void saveOrUpdate(Object obj) {
try {
startOperation();
session.saveOrUpdate(obj);
tx.commit();
} catch (HibernateException e) {
handleException(e);
} finally {
HibernateFactory.close(session);
}
}
protected void delete(Object obj) {
try {
startOperation();
session.delete(obj);
tx.commit();
} catch (HibernateException e) {
handleException(e);
} finally {
HibernateFactory.close(session);
}
}
protected Object find(Class clazz, Long id) {
Object obj = null;
try {
startOperation();
obj = session.load(clazz, id);
tx.commit();
} catch (HibernateException e) {
handleException(e);
} finally {
HibernateFactory.close(session);
}
return obj;
}
protected List findAll(Class clazz) {
List objects = null;
try {
startOperation();
Query query = session.createQuery("from " + clazz.getName());
objects = query.list();
tx.commit();
} catch (HibernateException e) {
handleException(e);
} finally {
HibernateFactory.close(session);
}
return objects;
}
protected void handleException(HibernateException e) throws DataAccessLayerException {
HibernateFactory.rollback(tx);
throw new DataAccessLayerException(e);
}
protected void startOperation() throws HibernateException {
session = HibernateFactory.openSession();
tx = session.beginTransaction();
}
}
我的实现 class 是,
UserService.java
package obs.service;
import kar.AbstractDao;
import kar.DataAccessLayerException;
import obs.domain.User;
import org.springframework.stereotype.Service;
@Service("IUserService")
public class UserService extends AbstractDao {
public UserService() {
super();
}
public void create(User event) throws DataAccessLayerException {
super.saveOrUpdate(event);
}
}
这里我将 AbstractDao.class 文件放入 jar 中,在 UserService.java 中我实现了 AbstractDao class.
这是我得到的错误,
您出错的可能原因是您没有将自定义 jar 正确安装到本地 maven 存储库。
按如下方式将 jar 安装到本地存储库。
mvn install:install-file
-Dfile=<path-to-file>
-DgroupId=<group-id>
-DartifactId=<artifact-id>
-Dversion=<version>
-Dpackaging=<packaging>
-DgeneratePom=true
Where: <path-to-file> the path to the file to load
<group-id> the group that the file should be registered under
<artifact-id> the artifact name for the file
<version> the version of the file
<packaging> the packaging of the file e.g. jar
也看到这个问题How to add local jar files in maven project?