2016-04-26 32 views
0

我爲自定義編碼創建了新的jar文件,而我將這個jar文件添加到我的新項目中,它拋出「包不存在」和「無法找到符號」錯誤。在java應用程序中實現自定義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 { 
 

 

 
\t public UserService() { 
 
\t \t super(); 
 
\t } 
 

 
\t public void create(User event) throws DataAccessLayerException { 
 
\t \t super.saveOrUpdate(event); 
 
\t } 
 
}

這裏我已經把AbstractDao.class文件放在jar和UserService.java中,我已經實現了AbstractDao類。

這是錯誤我得到了,enter image description here

+1

添加什麼步驟你一些描述了包括該項目的新的jar – gba

+0

分享你的pom.xml與調試完整的堆棧跟蹤啓用 – mirmdasif

+0

Hi @gba我已更新我的問題。 – Karthikeyan

回答

4

其原因可能爲你的錯誤是你沒有正確安裝您的自定義罐子你的本地倉庫。

按照以下步驟將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?

相關問題