aspose file tools *
The moose likes Ant, Maven and Other Build Tools and the fly likes Need a little help : Jar not working when added as dependency in war Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "Need a little help : Jar not working when added as dependency in war" Watch "Need a little help : Jar not working when added as dependency in war" New topic
Author

Need a little help : Jar not working when added as dependency in war

Indrayani Godbole
Greenhorn

Joined: Oct 09, 2012
Posts: 26
Hi All,
I have a jar which contains dao implementation class which uses JPA entity manager for database operations.
Database details are mentioned in profile in Settings.xml in Maven Directory.
When jar is created using maven, test cases calling dao class involving database operations get executed successfully.

There is a war application and i have mentioned the dependency of above jar in the war's pom.xml .
Now the problem is when a request is sent from one of the classes of the war application to the Jar's dao class, i am getting null pointer exception.
this is happening because the EntityManager is null.

Question is, the standalone testcases from the Jar get executed successfully while jar creation.
but when a call is sent from war application to the jar dao, it gets null Entitymanager.

any idea why this is happening, I simply have no clue about it.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2629
    
    9

You got a web app (war file). And you got a jar file (DAO). Is this jar in the war's WEB-INF/lib folder?

I don't think you need to specify the dependency if the jar files are in WEB-INF/lib


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Indrayani Godbole
Greenhorn

Joined: Oct 09, 2012
Posts: 26
K. Tsang wrote:You got a web app (war file). And you got a jar file (DAO). Is this jar in the war's WEB-INF/lib folder?

I don't think you need to specify the dependency if the jar files are in WEB-INF/lib



--------------------
the jar file is not in the lib since its added in pom dependency
when i searched on internet resources, what i found was, i should instantiate the dao using lookup and not by new and this is may be the reason of entitymanager not getting initialized

like currently i am doing

DaoImpl obj = new DaoImpl()

wheres as i should be doing

DaoImpl obj = (DaoImpl)context.lookup("DaoImpl/local")


i tried the same , but now i am getting, Name [DaoImpl/local] is not bound in this Context. Unable to find [DaoImpl].


the DaoImpl class, i have put these annotations
@Transactional
@Repository

do i need to do anything apart from above, what could be the reason of 'name not bound' error.

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16315
    
  23

If you are getting a NullpointerException, then it would appear that Maven is doing what it should. It is more likely that your runtime environment is not set up properly. Maven failures would be more likely to result in a ClassNotFoundException.

I use JPA in webapps running under Tomcat. I use the Spring Framework to maintain the persistence components. To make them work properly, I have to include the Spring tomcat agent and weaver jars in Tomcat's lib directory (NOT the WAR!)




Customer surveys are for companies who didn't pay proper attention to begin with.
Indrayani Godbole
Greenhorn

Joined: Oct 09, 2012
Posts: 26
Tim Holloway wrote:If you are getting a NullpointerException, then it would appear that Maven is doing what it should. It is more likely that your runtime environment is not set up properly. Maven failures would be more likely to result in a ClassNotFoundException.

I use JPA in webapps running under Tomcat. I use the Spring Framework to maintain the persistence components. To make them work properly, I have to include the Spring tomcat agent and weaver jars in Tomcat's lib directory (NOT the WAR!)





------------------------
so does it mean that, i can not use jpa as a part of jar and put the jar's dependency in a war project ?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16315
    
  23

Indrayani Godbole wrote:
Tim Holloway wrote:If you are getting a NullpointerException, then it would appear that Maven is doing what it should. It is more likely that your runtime environment is not set up properly. Maven failures would be more likely to result in a ClassNotFoundException.

I use JPA in webapps running under Tomcat. I use the Spring Framework to maintain the persistence components. To make them work properly, I have to include the Spring tomcat agent and weaver jars in Tomcat's lib directory (NOT the WAR!)





------------------------
so does it mean that, i can not use jpa as a part of jar and put the jar's dependency in a war project ?


No. All of the JPA stuff is part of the WAR. Both my actual user code and the (Hibernate) JPA support librarues. Only the things that enable Spring to weave the annotations are made part of Tomcat.

One thing worth asking, since your EntityManager is null is where and how your webapp constructs the EntityManager. Because Tomcat won't do that for you. Full-stack JEE implementations will, but I think that the JPA has to be full-blown EJB for that to be done.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need a little help : Jar not working when added as dependency in war