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.
Related
Recently While I was doing a Dynamic web project,I forgot to include OJDBC.jar in the WEB-INF folder.But the code ran file without showing any Class Not Found Exception.But when my friend did the same,Class not found exception was shown.Then when he included the OJDBC.jar in the WEB-INF folder,the code ran fine.I am not able to understand why this is the case.Is it not mandatory to include OJDBC file in WEB-INF folder?And we both used Apache Tomcat Server.
As you can see from the above screenshot,I experimented by not including OJDBC.jar in the WEB-INF folder.The code ran fine.Please tell me why this is the case?
Well, Eclipse builds the context's classpath joining libraries from several locations:
Of corse, the <tomcat_home>\lib\*.jar files.
The <project>\WEB-INF\lib\*.jar files.
... and also the deployment assembly entries (within the project's properties).
Check out every one of these and compare with your partner ones.
I'm trying to export my project to a jar file in IntelliJ using these instructions, and failing.
I'm getting a NullPointerException on this line in my application:
ClassLoader.getSystemClassLoader().getResource(".").getPath();
Note that this same project exports fine in Eclipse.
I've tried changing the Class-Path in the manifest to ., as I've noticed this change in the jar exported by Eclipse.
Then, my application gets a bit further, but bails when trying to load any of the resource files included in my jar. This is how:
Assume my jar is placed in
/Users/me/screwed.jar
When my application tries to open resource.file in the root of the jar file, it's searching for it here:
/Users/me/resource.file
This triggers a NullPointerException.
How do I fix this, team?
Use this line to load your path:
this.getClass().getClassLoader().getResource(".").getPath();
This should be the root of your jar file and allow you to open resources within it.
For static context, use
MyStaticClassName.class.getClassLoader().getResource(".").getPath();
where MyStaticClassName is some staic utility class.
I have a jar containing the main class of a project. This depends on several other jars that reside in a lib directory. One class of such a dependend jar loads a ressource "/Data/foo/bar/file.txt". But loading this file as ressource leads to null.
Here is the directory structure:
./main.jar
./lib/lib1.jar
./lib/lib2.jar
./lib/lib3.jar
./lib/runtimedata/Data/foo/bar/file.txt
This is the classpath of the manifest.mf of the main.jar:
lib/lib1.jar lib/lib2.jar lib/lib3.jar lib/runtimedata
I start the application via
java -jar main.jar
The lib2.jar contains a class that tries to load the file with
ThatClass.class.getResource("/Data/foo/bar/file.txt");
But that happens to be null. Why? lib/runtimedata is in the classpath.
I even tried to put the Data directory into lib/lib/runtimedata, in case the path is relative to the jar file containing the loading class. But that doesn't help.
What am I doing wrong here?
EDIT:
Running the application with
java -cp main.jar:lib/*.jar:lib/runtimedata my.package.Main
works correctly.
EDIT 2:
I cannot change that lib that does the resource loading. I am only using that lib. The only things I can change is the main.jar the classpath and the command line.
When you start the path with a "/", it's considered as absolute. Try ThatClass.class.getResource("/runtime/Data/foo/bar/file.txt");
Otherwise, if you cant't change the code, put the file on /Data/foo/bar/file.txt
In a development environment, this can sometimes happen when resources have not been processed during the build. Using Gradle and building your main JAR or main test JAR will depend on the compileJava tasks in your libs, but will not trigger their resources to be processed. You can see if this is happening by looking in your build dir to see if resources for your libs have been copied over. If they haven't been copied the resource loader won't find them at runtime.
If this is the problem, a full build will fix the issue and published JARs should always have their resources. But, it's nice to be able to trigger e.g. the test task for an individual module and know that it will always pull in everything it needs. If you have a library with essential resources that must always be present, you can force them to be processed in partial builds by adding this to the build.gradle of the library:
compileJava.dependsOn(processResources)
I recently used eclipse (myeclipse actually) to create a new Web Project via the new->web project wizard. The default directory structure for the project looks as follows:
src
WebRoot
WebRoot/META-INF
WebRoot/META-INF/MANIFEST.MF
WebRoot/WEB-INF
WebRoot/WEB-INF/lib
WebRoot/WEB-INF/classes (not visible via the Package/Project Explorer)
WebRoot/WEB-INF/web.xml
For jpa I added:
WebRoot/META-INF/persistence.xml
For spring I added:
WebRoot/META-INF/spring/spring-master.xml
When trying to run a very basic junit test to load the spring context and hibernate entity manager I was running into classpath problems. Initally to get around the first class path problem I encountered I added WebRoot/META-INF/spring to the junit->Run Configurations classpath when Spring couldn't find my spring configuration files.
But then I ran into another classpath error when jpa couldn't find my entity bean definition. Reading this article (dated 2007) it states that META-INF/persistence.xml should be contained within WEB-INF/classes because JPA searches for entity bean class files in the parent directory of META-INF/persistence.xml
When I copied META-INF (via Windows Explorer since eclipse is hiding the classes directory) and all its contents into WEB-INF/classes my classpath errors went away (as well as the need to monkey with the junit run configuration to remedy the previous problem of spring not knowing where my spring config file is located turns out I still need to add the spring folder to the junit classpath option).
But now I have configuration files in a folder which does not show in Eclipse's Package/Project Explorer.
What is the best way to setup Eclipse for this situation? Tell it to show the classes folder somehow? Why does Eclipse create META-INF as a sibling to WEB-INF for web projects?
Is there a way to inform JPA to look for entity classes somewhere other than the default?
Thanks for any input!
You should know that eclipse mixes the compiled classes and resource from src (and any other source code directory) in the <WebRoot>/WEB-INF/classes directory.
So one way to access the same file in Test and Webapp (for example META-INF/persistence.xml) is to put it in src/META-INF/persistence.xml. Then it is available in <WebRoot>/WEB-INF/classes/META-INF/persistence.xml for the web app, and in <classpath>/META-INF/persistence.xml for EVERY application or test.
So my recommendation is to put all the stuff that is addressed via classpath (persistence.xml, spring.xml) in a source directory, and let eclipse do the rest. - If you specify the location of a resource via classpath, then it should work the same way for webapp, test, app.
BTW: eclipse can create many source code directories, you can use one for the normal application classes, one for the class path relevant resource and one for the test classes.
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...