Hibernate configuration file and Eclipse project export - java

I have a 3 projects under Eclipse : 1-CLIENT, 2-COMMON, 3-SERVER. The server project contains everything related to database managment (i.e. DAO...). I'm using hibernate and annotation for this project to access database. The common project contains also some objects commonly used by both the Server and the Client. Some of these objects are from the database. My Server project contains as well the HibernateUtils class that load the configuration file "hibernate.cfg.xml".
When I run the project under Eclipse, no problem, everything is going smoothly. Now comes the deployment... I'm exporting the server as a runnable jar file under Eclipse and the common project as a lib file. in the server jar file, I have the file "hibernate.cfg.xml".
When I launch the program, I'm ending up with an exception
Caused by: org.hibernate.HibernateException: /hibernate.cfg.xml not found
I've tried to force the filename in the configuration process using new AnnotationConfiguration().configure("XXXX/hibernate.cfg.xml"), but nothing is working !
Even when I copy the debug command line used to launch the program and paste it under a command dos window, it's not working...
What am I missing there ?

Hibernate searches for its configuration in places addressed in CLASS_PATH. i suggest you put your hibernate.cfg.xml in your source folder. this often prevent likely problems.
Another suggestion! don not export your project using eclipse export wizard, try to create an ant file to do so, or maven if you can...

Related

Unexpected classpath behaviour remote deployment of Java application with Netbeans 13

Can anyone help me understand the classpath logic when deploying Java applications to remote hosts?
Netbeans will build, deploy and execute my Java application correctly on a remote Linux (Ubutntu 20.x) host.
Lets say that the executable JAR is deployed and executed in:
/home/user/project/dist
With any supporting library files copied to:
/home/user/project/dist/lib
This all makes sense to me.
However, I wish to read application and log4j2 configuration files. I would think that these should be placed in the same directory as the jar file. However they must be placed in the parent directory:
/home/user/project
Okay... BUT...
If I amend my code to write out a text file (to the executable directory) the resulting text file is written to:
/home/user/project/
<Edited 19Jun22>
I want my deployed application to read the log4j2.xml and configuration files from the same directory that my application writes to. What configuration settings must I change in Netbeans?
Alternatively is there a way to make Netbeans deploy to /home/user/project instead of /home/user/project/dist?

How to connect to database without opening NetBeans?

I have created a Java application that uses a database, and I now want to build it into a jar file and make it so that the database works without NetBeans. I thought NetBeans would automatically include the database, but it doesn't.
The files that NetBeans include in the 'dist' folder when I build it look like this:
dist
lib
derby.jar (Executable Jar File)
derbyclient.jar (Executable Jar File)
README
WordFinder.jar (Executable Jar File)
Everything except the database-related code works when I launch the WordFinder.jar file after closing NetBeans.
I noticed that the database is using 'org.apache.derby.jdbc.ClientDriver', and I'm wondering, does that need to be 'org.apache.derby.jdbc.EmbeddedDriver' in order for it to include the database?
Are there any files that I need to include? I'm new to Java databases, so try to keep the instructions simple.
You need to start the database server in you computer, for doing that you need to go to your derby path (C:\Program Files\Java\jdk1.8.0_45\db\bin) and run the startNetworkServer.bat file
I may be a year late but seeing the comments, I'm assuming the issue hasn't been resolved yet. I had the same problem and would like to show how I worked around it. Note I'm not using an embedded derby database.
1) I changed my connection string. Instead of the usual "jdbc:derby://localhost:1527/myDB;create=true;...." , I put the exact location of the database which can be found usually under C:\Users\myPC\.netbeans-derby\myDB
2) Clean and build the project which should give you the "dist" folder which would include the jar file of the project together with the "lib" folder.
3) Start the StartNetworkServer.bat which is located in the bin folder under derby folder. A cmd would show that will say something "Apache Derby Network Server blah blah blah started and ready to accept connections on port 1527"
4) After doing those, run the jar file and it should run even with the netbeans IDE closed.
That's about it. That's how I got mine to work without opening Netbeans IDE. Hope it helps.

No persistence provider WHEN exporting jar file

I have a maven desktop project that uses JPA as persistent layer; this layer deals with one MySQL database and one SQL Server database.
When I run it inside Eclipse, there is no problema; but when I try to export it outside the output when I run jar the console prints the famous:
javax.persistence.PersistenceException: No persistence provider for
EntityManager named axaptaUnitName
axaptaUnitName is the unit that deals with SQL Server. I have tried all three type of exportation from Eclipse, extract required libraries into JAR, package into JAR, and copy in external folder; none of them works.
All libraries (including the connector with SQL Server) are correctly added to classpath; inside Eclipse all works perfect; so I assume that it's some kind of exportation problem.Any suggestions?
Edit: I've tried to replace Microsoft SQL driver with JTDS driver; but the issue still happening.
Here is Work around for this.
I simply exported it as a runnable Jar with option - "Extract required libraries into generated Jar".
Opened generated Jar with a archiever software.
Then I found there is no "Persistence.xml" in META-INF folder.
I dragged my "Persistence.xml" file in that META-INF folder in achiever's window itself.
Closed archiever program.
After that, the PersistenceException was disappeared.
I'm assuming you use the "uber-jar" method where all dependency jars are exploded into one big jar. The problem with this approach is if the jar has files with same relative path they could override each other. Consider this scneario:
// contents of A.jar
com/foo/Class1.class
com/foo/Class2.class
META-INF/persistence.xml
// contents of B.jar
com/bar/Class1.class
com/bar/Class2.class
META-INF/persistence.xml
When A.jar and B.jar are exploded and re-packaged into Uber.jar, the earlier META-INF/persistence.xml will get overwritten causing errors / unwanted behavior
A better solution to deploy your standalone application is to keep all dependencies in their original jar packaging, place them in a single folder and run using command like this (windows):
java -cp "dependency/;myprog.jar" com.foo.MyMainClass
(all dependency jars are placed on the folder "dependency")
I've found one solution:
Instead of exporting project with Eclipse, I have generated a jar with Maven this way.

How to include sqlite db file into jar in netbeans?

I use sqlite in my java application. When I run it from Netbeans, it works well. The sqlite db file is located in the root project directory (same level as build.xml).
This is the code to access the database file:
DriverManager.getConnection("jdbc:sqlite:database.db");
However, when I build the project, and run the jar file outside netbeans, I always get this error: no such table: table_name
My question: where is the correct location to put the "sqlite db" file? Is it possible to package it inside the JAR file?
I have tried to package my application into a single JAR file (similar to Eclipse's FatJar), but I still get that error. In this case I modify the build.xml as explained in this blog:
Netbeans single JAR
I also tried to create a database folder in the src directory and then put the db file inside the folder (src/database/database.db) and access it using
DriverManager.getConnection("jdbc:sqlite:src/database/database.db");
I can run it inside netbeans. But when I run it outside, I got this error:
path to 'src/database/database.db':C:\Windows\system32\src' does not exist
I know there are similar questions to this in stackoverflow, but most of them don't have a concrete solution. Let me know if someone has found a better one.
Thanks a lot.
Problem solved!
I made a mistake in the way I execute the JAR file. Previously I run the JAR file by right click on it and open it with Java SE binary. I need to do this because in my PC the default program to open JAR file was set to another software.
It turned out that I have to set the default program to Java SE Binary and the double click the JAR file in order to run my application. Using this way I can run and access the sqlite db file without a problem.
I hope this can help others who may get the same problem as I had.
probably you need to point your app to include your app base directory in the classpath.
the command to run your app should be something like:
java -cp . YourAppMainClass
the "-cp . " is the command option that tells java to include current directory in the classpath.

Error while trying to run JAR of project having Hibernate from command prompt

I am novice in Hibernate technology.
I have a Java project which uses Hibernate.
When I run the project from Netbeans IDE the project runs fine without any issues.
However when I run the JAR file of the project from command prompt I get an exception
"org.hibernate.hql.ast.QuerySyntaxException:"
XXXX is not mapped.
I have included all the Jars required for the execution of this project.Also I have imported javax.persistence.Entity.
Appreciate if you can help me out in this issue.
-Adish
You have to have .hbm.xml mapping objects to tables somewhere so Hibernate can read them. It's an ORM tool - JARs aren't sufficient.
Most likely, the mapping files are not included into the generated .jar. Check their presence.
Put the mapping files in a folder included in the class path of the application. I don't use Netbeans, the feature might be called exported entries or build class path in the project settings.
Mapping files can be recognized easily, they end with the hbm.xml extension.
However when I run the Jar file of the project from command prompt I get an exception "org.hibernate.hql.ast.QuerySyntaxException:" XXXX is not mapped.
The message is self explaining, you have somewhere a query that references a class that is considered as a non mapped class. Double check that:
XXXX is on the classpath.
XXXX is properly annotated.
XXXX is listed in the EntityManager configuration (or AnnotationConfiguration, what you're using is unclear).

Categories