Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default problem with Persistence.createEntityManagerFactory

    Hi all,
    I have an application that runs fine in the netbeans IDE. I have compiled it and run it from the command line (java -jar jarfile.jar), but it dies at Persistence.createEntityManagerFactory(). When I say dies, I mean nothing happens - no exceptions are being thrown that I can see and the method returns null. I have set up a logger that prints to a file and the last thing in the file is "INFO Getting an EntityManagerFactory"

    Here is part of the code:
    Java Code:
    myLogger.log(Level.INFO, "Getting an EntityManagerFactory");  
            EntityManagerFactory entityManagerFactory =  
                                 Persistence.createEntityManagerFactory(myPersistenceUnit,  
                    new HashMap<String, String>() {  
                        private static final long serialVersionUID = 1L;  
                        {  
                            put("hibernate.show_sql", "false");  
                            put("hibernate.connection.url", myUrl);  
                            put("hibernate.connection.username", myUsername);  
                            put("hibernate.connection.password", myPassword);  
                        }  
                    });  
            myLogger.log(Level.INFO, "Setting myEntityManager");
    This code is never called when running from a jar (java -jar jarfile.jar):
    Java Code:
    myLogger.log(Level.INFO, "Setting myEntityManager");
    Its almost like it cannot find the persistence.xml file (at least that is what I am assuming from my trolling on the web.) One clue may be that it was working fine (from the jar file) until I upgraded my netbeans to 6.8 recently (it only just now occurred to me that this may be the root of the problem - I had made many code changes (none of them relating to the entities) and I assumed that I had broken something. Now I don't know where to start.

    Any suggestions would be most welcome

    thanks in advance,
    Jordan

  2. #2
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    364
    Rep Power
    5

    Default

    It is always rocket science when it comes to *.jar packing i hate it myself.

    It is strange that you don't get any NoClassDef error.

    Few things to check:

    - place of src\META-INF\persistence.xml file

    - do you have to manuallly add values in
    "Class-Path" in your \src\META-INF\MANIFEST.MF file
    to all *.jar files needed

    -because of possible ClassLoader problems
    it is usefull to add all needed *.jar files in your
    ...jre\lib\ext
    folder

    - write some System.out.println("...") and run your *.jar in
    command prompt to see where is it stopping

    - - -

    I had problem with using application's *.properties files with *.jar.
    You can solve this by editing META-INF\MANIFEST.MF

    If *.properties is next to *.jar in your file-system use, do this in META-INF\MANIFEST.MF:
    Class-Path: .
    (That is Class-Path:blanc.)
    it means: look for property file in SAME folder where my app jar file is

    If *.properties is in some folder do this in META-INF\MANIFEST.MF:

    Class-Path: . folder_name\myproperties.properties

    Now, when u use export tools to create app JAR
    use this one you just created, dont let wizard do it for u

    i hope u will solve this
    it's real pain int the *

    regards ;)

  3. #3
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default

    Thanks for the response...
    I am actually including a library jar (of mine) that includes the persistence.xml file (so it exists in lib/myEntitlyLib.jar/META-INF of the dist directory.) This saves me from passing the persistence.xml file around for applications that use the entities defined in myEntitlyLib. This used to work fine until recently, but I don't know what I did to break it. I have subsequently tried moving the META-INF directory to the src directory of the top-level jar, but this did not work :-(

    I do not normally mess with the Class-Path, although since I started having this problem, I did try adding all of the jar files to it. I get the same error.

    I am not familiar with the jre/lib/ext folder. It sounds like this would need to be done for each user of my app? This doesn't sound like a good idea to me, but hey, I'm game for anything at this point!

    I have added a logger that prints to a file in /temp. You can see the log commands in my original post - it never gets to the last one.

    The app is reading the properties file - because it is logging what it reads. The properties file is in /src of the top-level app.

    I will try the Class-Path: . in the manifest, hoping it will resolve the persistence.xml problem. Unfortunately, the app seems to pick up all of the other jars and the properties files.

    Here is my manifest from the top-level app (note that netbeans added the Main-Class and Class-Path info):
    Manifest-Version: 1.0
    Ant-Version: Apache Ant 1.7.1
    Created-By: 11.0-b15 (Sun Microsystems Inc.)
    Main-Class: persysapp.PersysApp
    Class-Path: lib/dataLib.jar lib/thompco.jar lib/antlr-2.7.6.jar lib/as
    m.jar lib/asm-attrs.jar lib/cglib-2.1.3.jar lib/commons-collections-2
    .1.1.jar lib/commons-logging-1.1.jar lib/dom4j-1.6.1.jar lib/ehcache-
    1.2.3.jar lib/jdbc2_0-stdext.jar lib/jta.jar lib/hibernate3.jar lib/h
    ibernate-tools.jar lib/hibernate-annotations.jar lib/hibernate-common
    s-annotations.jar lib/hibernate-entitymanager.jar lib/javassist.jar l
    ib/ejb3-persistence.jar lib/mysql-connector-java-5.1.6-bin.jar lib/jc
    ommon-1.0.16.jar lib/jfreechart-1.0.13.jar lib/log4j-1.2.15.jar lib/j
    calendar-1.3.3.jar
    X-COMMENT: Main-Class will be added automatically by build

  4. #4
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    364
    Rep Power
    5

    Default

    ...I don' t see
    myEntitlyLib.jar
    in Manifest file

    in :

    Class-Path: lib/dataLib.jar lib/thompco.jar lib/antlr-2.7.6.jar lib/as
    m.jar lib/asm-attrs.jar lib/cglib-2.1.3.jar lib/commons-collections-2
    .1.1.jar lib/commons-logging-1.1.jar lib/dom4j-1.6.1.jar lib/ehcache-
    1.2.3.jar lib/jdbc2_0-stdext.jar lib/jta.jar lib/hibernate3.jar lib/h
    ibernate-tools.jar lib/hibernate-annotations.jar lib/hibernate-common
    s-annotations.jar lib/hibernate-entitymanager.jar lib/javassist.jar l
    ib/ejb3-persistence.jar lib/mysql-connector-java-5.1.6-bin.jar lib/jc
    ommon-1.0.16.jar lib/jfreechart-1.0.13.jar lib/log4j-1.2.15.jar lib/j
    calendar-1.3.3.jar

    should you add it too?

  5. #5
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default

    Sorry - I was trying to be generic (before I added the manifest file) - the library file is "dataLib.jar" and it is there.

  6. #6
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    364
    Rep Power
    5

    Default

    Something that will not solve your classpath problems but it's good coding practice I think you should easily get used to:

    - You made direct impl. of Map interface by writing that HashMap with <K,V> pairs, and put ALL that in SAME line with creation of manager factory.

    First be sure that you call this code on app. startup
    because creation of manager factory can be long.

    Second, values that you put in map can be moved from java code -
    move them to either persistance.xml (your 'myPersistenceUnit') or some app. properties file and load them on startup too.

    Keep versions of your application - don't let one change ruin all that work
    and don't EVER change anything related to app. development (like version of NetBeans etc..) in the middle of project!

    hope you will kill your problem soon ;)

  7. #7
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default

    this has been resolved - turns out that I had removed the mysql jar and added back the mysql library. Ant did not include the library (but it did include the jar.)

  8. #8
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    364
    Rep Power
    5

Similar Threads

  1. Name persistence is not bound in this Context
    By tascoa in forum Java Servlet
    Replies: 1
    Last Post: 12-15-2009, 10:01 PM
  2. Replies: 0
    Last Post: 03-08-2009, 01:52 PM
  3. Container Managed Persistence
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-27-2007, 10:16 AM
  4. Hibernate Java Persistence
    By JavaBean in forum Java Tutorial
    Replies: 0
    Last Post: 09-22-2007, 10:02 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •