MyBatis looking for xml files in Tomcat directory - eclipse

I'm testing a web application with Eclipse + Tomcat, Eclipse deploys the web application files and launches Tomcat, and the application runs fine. But when MyBatis is trying to open it's XML configuration files, it looks for them in
C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\persistence\db\oracle.xml
instead of the correct place:
C:\workspace\mywebapp\src\persistence\db\oracle.xml
Where is MyBatis supposed to look for XML files?
EDIT:
This is where I specify the relative path:
String cfgFile = "persistence/db/oracle.xml";
Reader reader = Resources.getResourceAsReader(cfgFile);
session.put(db, new SqlSessionFactoryBuilder().build(reader));

Resources.getResourceAsReader looks files in classpath. For web application running in tomcat classpath consist of WEB-INF/classes and all jars from WEB-INF/lib and tomcat folders like $TOMCAT_HOME\lib.
The issue you encounter most probably is caused by the fact that oracle.xml file is not added to deployment. It looks like c:\workspace\myweapp\src is not among source folder of eclipse project so eclipse doesn't copy files from it to the folder which is deployed to tomcat. Depending on your existing project structure you may need to create subfolder in src and add persistence with all subfolders there. This will allow you to avoid clash if some subfolder of src is already a source folder in eclipse. I would recommend to use maven project structure:
src
main
* java
you java source code here organized by package
* resources
persistence
I marked folders which should be added as source folder to eclipse with *.
Please note that it is not correct to say that C:\workspace\mywebapp\src\persistence\db\oracle.xml is a correct place to search for it. After you create a war to deploy it on production this path most probably will not be available at your production server. What you really need is to include persistence\db\oracle.xml to the war in appropriate place (under WEB-INF/classes).

Maybe you need another class loader 1. Try this:
String cfgFile = "persistence/db/oracle.xml";
ClassLoader classloader = Thread.currentThread().getContextClassLoader()
Reader reader = Resources.getResourceAsReader(classloader, cfgFile);
Notes
See Difference between thread's context class loader and normal classloader and you may want to see the code of org.apache.ibatis.io.Resources. You find it here.

Related

Eclipse RAP or RWT Launch Configuration accessing WEB-INF resources?

I am developing an Eclipse RAP web application and would like to keep resource files (configuration etc.) in WEB-INF and load them with something like this:
Properties properties = new Properties();
properties.load(getServletContext().getResourceAsStream("/WEB-INF/foo.properties"));
The WAR is created with a Gradle build and that looks OK when deploying to a standalone Tomcat.
What I would like to do is getting this to work in my development cycle inside Eclipse. I.e. launch the project with a RAP or RWT launch configuration and have all contents in WEB-INF deployed to Jetty, too.
The following instructions have helped me to at least have my own web.xml in the runtime directory used by Jetty:
http://wiki.eclipse.org/RAP/FAQ#How_do_I_develop_an_RWT_standalone_application_with_RAP_.3E.3D_1.5
..\my_workspace\.metadata\.plugins\org.eclipse.rap.tools.launch.rwt\my.rap.app\web-app\WEB-INF\web.xml
But I cannot find a way for any other files under WEB-INF to be deployed when launching inside Eclipse. What I am hoping to achieve is this:
..\my_workspace\.metadata\.plugins\org.eclipse.rap.tools.launch.rwt\my.rap.app\web-app\WEB-INF\foo.properties
Does anybody know whether this is possible with an RWT or RAP launch configuration in Eclipse?
Is there a better approach to comfortably develop and debug a RAP application that loads resources from WEB-INF in Eclipse?
Unfortunately, a feature to copy resources to the WEB-INF directory never made it into the RWT launcher code base.
Since this would be a useful extension of the RWT launcher you may want to file an enhancement request.
I cannot think of a workaround other than putting the properties file on the class path and reading from there:
Properties properties = new Properties();
properties.load( getClass().getResourceAsStream( "foo.properties" ) );
If the resources meant to be located in the WEB-INF directory aren't changing much you could also try to copy them manually to my_workspace\.metadata\.plugins\org.eclipse.rap.tools.launch.rwt\my.rap.app\web-app\WEB-INF

JBoss 6.1.1.EAP Module Deployment: Class not found

The structure is as follows:
Actually deployed module is EAR.
EAR contains WAR module, and WAR module contains another JAR module.
In this last JAR, there are some generated classes and their parent folder is also used as source folder. Its path is "target/generated-sources/java".
The problem that is killing me, is that the deployed application throws ClassNotFoundException on server start, and the classes in question are the generated ones.
Now the trick:
if I explicitly change the Deployment Assembly in WAR project in Eclipse not to contain JAR project as "project", but as an archive from the JAR's project "target" folder, JBoss sees the generated classes and starts.
This solution however works only until next eclipse maven project update, so manual edit of deployment assembly is not really a solution.
Any ideas how to deploy or reorganise packages correctly?
OK, seems I have found the solution.
I looked through the build-path of the mentioned JAR file and saw that the entry Output folder pointed to target/test-classes.
After changing this entry to target/classes the application deploys and starts without any missing generated classes.
This is what worked for me:
Expand the 'target' folder of your maven project inside Eclipse (Project Explorer View);
Refresh it (F5);
Right click on your project on 'Servers' tab, then select "Full Publish";
Start your JBoss.
I had to enforce the JBoss Tool "Full Publish" to get an updated version of my target folder by Refreshing it manually on eclipse.
I don't know why but sometimes the Publishing from maven projects (even Full Publishes) do not copy classes from the target Project as it is in the file system. Maybe it's using some outdated memory info or some cache...
Anyway, this is what works for me.
After an update to Eclipse 4.15 and JBoss tools I got this problem too.
My solution: project -> properties -> Java Build Path --> Source
There my Output folder from my source was linked to project/target/classes, I changed this to project/target/project-projectversion/WEB-INF/classes
When I looked into the standalone JBoss folder I saw the folder structure of my source, but the classes where missing, when I changed the output folder the classes pop up and everything worked like before.
I am pretty sure the update made the problems.

deploy jar Java Desktop Application in jdeveloper12c

I have always developed my java NetBeans projects. When I finish my project, and automatically build "dist" folder where the main jar and a "lib" folder containing all the external jar was created. This is done automatically with the NetBeans IDE
Now I'm trying to do a project with jdeveloper 12c Java Desktop Application and try to deploy my project but I have problems with java.lang.NoClassDefFoundError. I meet several questions:
Can you add the same structure as I said before? That is, a file jar and inside a folder "lib" place the jars using How to do it?
I've seen examples that by New File Group adds refencias, but it really adds to the main jar file (as I see in the size of the file) but not saved in a separate folder.
I do not know if you can help me do this in jdeveloper 12c: Generate a jar file and folder lib with references.
Thank you for your help.
Go into the deployment profile for your project and add all the libraries and files that you need to be included to the JAR you are generating.

Spring can't find a lib and webapp doesn't start up in tomcat 6

I've this problem using STS: I'm building a simple Spring app, just to try out features like MVC and persistence. Now I've created something very simple, out of a bunch of tutorials for Spring 3, that I'm using. The application fails with this, during server startup:
Code:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean] for bean with name 'mySessionFactory' defined in ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
but I've org.springframework.orm in web-inf/classes folder (I even tried putting it in web-inf/lib). As I copied these libs there, the came out in Web App Libraries folder. Building this project in STS works fine as this dependency is set up in build path throught project properties, but how do I transfer the libs to the web app? (I'm using Tomcat 6 as it is the server I'm going to use sometime in the future for production). Is this a config problem of my XML? Or am I just missing the right way to put this lib? (I encountered the same problem before, but adding the needed lib in classes worked it out). More than this I that if I browse inside my workspace to the folder where the working folder of tomcat should be, I can't find any work directory and any commo
Go to the Servers view and double click on Tomcat Server which will open up a Tomcat Overview page. This will show you the server path and deploy path which Eclipse is using to publish your webapps to.
For example, mine are:
Server path: .metadata\.plugins\org.eclipse.wst.server.core\tmp0
Deploy path: wtpwebapps
Now in a Command Prompt (or Explorer) browse to %ECLIPSE_HOME%\workspaces\default\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps and you should see your webapp. Go into the WEB-INF\lib directory and check if your jar is there.
A few things to try:
If the jar is not in WEB-INF\lib, you might need to republish it. Right-click on the Tomcat in the Servers view and click Publish. Does it appear? If not, Right-click on the Tomcat again and this time select Clean... which will clean out everything and republish. If this doesn't work, the brute force approach would be to delete the files manually and then tell Eclipse to republish them.
Clean your work directory, by right-clicking the webapp under Tomcat in the Servers view and selecting Clean module work directory. Or do it manually by deleting from %ECLIPSE_HOME%\workspaces\default\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost
You should also try closing-reopening/cleaning/rebuilding/refreshing your webapp project just in case Eclipse hasn't picked up the changes.
You need to put the various Spring JARs into your WEB-INF/lib directory (WEB-INF/classes is no use for JAR files).
You don't just need org.springframework.orm, you need most of them (see here for dependency diagram).
At least the libary jar files must be located at WEB-INF/lib within your war file. And you only need to deploy the war file to your productiv web server.
If you use STS but your Project is not a Maven project: then you need to put the files in <projectRoot>/WebContent/WEB-INF/libs .(to build the war with Eclipse/STS you need to execute Export/Web/WAR file.)
If you use Maven, then you need the specify your dependencies in the pom.xml, and use maven to package the war (run as/Maven package).

In eclipse, is there a way to specify a location other than the WEB-INF, for web.xml

I want to place development web.xml in another folder in eclipse.
This can be done using the Deployment Assembly properties. Right click on the web project, choose Properties, and then navigate to the Deployment Assembly panel. Remove the /WebContent entry (pointing to /) and then add another entry, of type Folder. It should be rooted where you want to keep the web.xml, and mapped to /.
This technique can be extended to make Eclipse work with arbitrary build tool source layouts.
You might want to use Apache Ant
for that purpose. You may define a "conf" directory and place all you config files there, or a resource directory for the same reason.
If haven't use ant for that purpose, I strongly recommend you to do so.
Here is a sample ant build.xml for a web app Sample Build.XML
I'll use Maven myself, but since you're just beginning, Ant would be just fine.
I am using the eclipse builder and not ant.
Actually what i did was that i defined web.xml in another folder. So now I have two web.xml, one for welogic with a lot weblogic specific stuff like filters. And i have another for development which i placed in another folder, in the web project and added this folder to the deployment descriptor for tomcat in eclipse. So now when i deploy the application, the web.xml for tomcat which is in my specified folder owerwrites the web.xml present in web-INF folder(in the deployment directory).
Looks to me a nice workaround.