gpt4 book ai didi

java - 在 java 应用程序中实现自定义 jar 时出现错误

转载 作者:行者123 更新时间:2023-12-01 18:05:24 25 4
gpt4 key购买 nike

我为自定义编码创建了新的 jar 文件,当我将此 jar 文件添加到我的新项目时,它会抛出“包不存在”和“找不到符号”错误。

jar 文件有一个类,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();
}
}

我的实现类是,

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 类。

这是我遇到的错误,enter image description here

最佳答案

错误的可能原因是您没有将自定义 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

Reference

另请参阅此问题 How to add local jar files in maven project?

关于java - 在 java 应用程序中实现自定义 jar 时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36858369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com