Expand WAR or EAR files in jBoss AS 7 - deployment

I'm deploying a WebService project into a jBoss AS7, and everything goes OK except that jBoss doesn't expand my WAR or EAR file.
Already tried copying the file to the "jboss-as-7.1.0.Final\standalone\deployments" folder and using the WebConsole, but in both cases the result was the same.
If I deploy from within Eclipse everything runs OK.
I need it to expand my file because in the application initialization I scan the class
directories looking for the correct class to instantiate using reflection.
EDIT : Don't know if this a particular situation with jBoss AS 7 or with the jBoss AS family, because I already used WebSphere and jBoss Web and both of them expanded the files.
EDIT2 : Added a System.out with the execution path
MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
and it returns the following path
C:\jboss-as-7.1.0.Final\bin\content\ServerEAR.ear\Server.war\WEB-INF\classes
witch doesn't exist. So I did a search for the class name and fount it at
C:\jboss-as-7.1.0.Final\standalone\tmp\vfs\deployment5a9e98d5c43716c3\Server.war-e31a657d2bc3bd0f\WEB-INF\classes\r30
Isn't it possible to force JBoss to extract the files to the deployment folder? Or how can I get the previous path at run time.

Had forgotten about this question, so this is the way that I solved it:
public String getRealFilePath(String aFilePath) throws Exception
{
org.jboss.vfs.VirtualFile vFile = org.jboss.vfs.VFS.getChild(aFilePath);
URI fileNameDecodedTmp = org.jboss.vfs.VFSUtils.getPhysicalURI(vFile);
path = fileNameDecodedTmp.getPath();
System.out.println(path);
return path;
}
So at runtime I just need to call getRealFilePath() with the original question values, and problem solved ;)

I believe that you cannot force Jboss7 to auto expand the war file when deploying.
You can, however, copy the already expanded war and you shouldn't have any problems.
Actually, if you look closely at the Eclipse deployment, you will notice that it copies the whole directory to the deployment directory, not the war file itself.
Why do you need to deploy exploded, by the way? If you need to access and read some files as resources, you can have a look at classpath resources

Please check you classpath setting. I faced the same problem recently, and solved it by added system environment variables:
CLASSPATH=.;
C:\Program Files\Java\jdk1.6.0_10\lib;
C:\Program Files\Java\jdk1.6.0_10\lib\tools.jar;
C:\Program Files\Java\jdk1.6.0_10\lib\dt.jar; `

Related

Make per-context JNDI variable available to Tomcat in Eclipse

I'm using Tomcat 8.5.6 inside Eclipse 4.6.1. I have my web-app project/context foo, which has a JAX-RS (using RESTEasy 3.1.0.CR3) endpoint of bar, so I can fire up Tomcat inside Eclipse and access:
http://localhost:8080/foo/bar
I have a variable named foobar which I want to access inside my JAX-RS implementation using JNDI:
final String foobar = (String) new InitialContext().lookup("java:comp/env/foobar");
I plan on deploying the produced WAR in production using Tomcat autodeploy. I want to configure the foobar variable for Tomcat externally to the WAR. How can I do that so that I can test it in Eclipse?
After a lot of reading, I found what I thought to be the $CATALINA_HOME of Eclipse: …\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\. So I created a context file for foo at …\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf\Catalina\localhost\foo.xml to correspond to my project/context, and put the following inside it:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Environment name="foobar" type="java.lang.String" value="123"/>
</Context>
Yes, I know that Eclipse erases this directory whenever I rebuild. But after building, I saved to file at least want to see if it works. It doesn't. I get an error:
javax.naming.NameNotFoundException: Name [foobar] is not bound in this Context. Unable to find [foobar].
I want to at least get it working so I can know how to do this in production, and worry later about the context file deletion thing in Eclipse. So what did I do wrong? Why can't Tomcat in Eclipse find this JNDI variable?
Note: I am not using a web.xml file and have no desire to do so; besides, this variable should be defined outside the WAR in the production deployment.
Update: The good news is that (on Windows 10 Professional Anniversary Edition 64-bit) using the same Tomcat but in standalone mode, I put the same foobar.xml file inside the standalone Tomcat's conf\Catalina\localhost\foo.xml, and my JAX-RS application picked it up just fine. So how can I define a JNDI variable in Tomcat inside Eclipse for testing?
It appears that in order to get Eclipse+Tomcat to recognize the per-module context files, you have to go into the server configuration (double-click on the server) and turn on the Publish module contexts to separate XML files. This way Tomcat will use the specific context XML file you created. Otherwise it apparently puts them in conf/server.xml and ignores the context-specific file you created.
There is still the problem that Eclipse regenerates this file each time you do a rebuild, destroying whatever JNDI variables you placed there. I'm trying to get the workaround in https://stackoverflow.com/a/22380248/421049 to work, but not yet succeeding. Anyone have any better ideas?
At least I'm able to reproduce a production environment now --- albeit temporarily, until the next rebuild.
Your link to Markus' answer on https://stackoverflow.com/a/22380248/1794485 allowed me to get this working, or at least as described in his workaround. But the remaining problem to solve was ordering.
As he said, you can workaround this by having a local copy of the META-INF/context.xml somewhere else, and adding this folder to the Deployment Assembly in the project properties of the Eclipse project.
This didn't pick up for me initially though. It looks like that while the Deployment Assembly in the properties shows as sorted by name, in fact it has an order like any other path. When I then removed the src/main/webapp entry (so the one containing the normal META-INF/context.xml) and added it back in, this effectively moved it down the pecking order. The next Tomcat deploy and startup in Eclipse finally put my preferred copy of META-INF/context.xml in .metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\myapp\META-INF
If in doubt about the true sequence of that Deployment Assembly path, have a look under your Eclipse project on the file system - at .settings\org.eclipse.wst.common.component.

weblogic 12c not loading application properties file

We're moving from OC4J to WebLogic and have some properties files that reside outside of the EAR file. In OC4J we simply put them in the "applib" directory. Everything I've read so far about WebLogic says to put the file in the user_projects\domains\mydomain directory, but that's not working.
Is there another directory that I need to use or how do I force WebLogic to look in user_projects\domains\mydomain for the application properties files?
Maybe not the best solution, but at least a workaround. I edited wls12130\oracle_common\common\bin\commEnv.cmd and appended the domain home dir to this line:
set WEBLOGIC_CLASSPATH=%JAVA_HOME%\lib\tools.jar;%PROFILE_CLASSPATH%;%ANT_CONTRIB%\lib\ant-contrib.jar;%CAM_NODEMANAGER_JAR_PATH%;%DOMAIN_HOME%

MyBatis looking for xml files in Tomcat directory

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.

Problems deploying WAR file from Netbeans to Tomcat

I'm trying to configure build.xml files for build forge, but it seems like I am having trouble with the libraries required for the java servlet pages. I am actually trying to use OWASP's AntiSamy library but I keep getting a Policy Exception followed by a file not found
org.owasp.validator.html.PolicyException: java.io.FileNotFoundException: C:\Program%20Files \Apache%20Software%20Foundation\apache-tomcat-6.0.29\webapps\XSSDemo\WEB-INF\classes\...\antisamy.xml (The system cannot find the path specified)
The antisamy.xml file is definitely in this location, however.
So I tried using Netbeans to clean and build a WAR file that I would have expected to work, since the web app works fine when I run it through Netbeans. However, I get the same problem. Is there possible some reference that Netbeans is creating when I run the app through the ide that is not occurring in the WAR deployment? Could the URL Encoding in the antisamy.xml location be causing problems?
Thanks for the help.
Edit: I compressed the long class path into the three dots here. The actual response has the actual class path. I am not getting any kind of security errors.
The problem I had was not actually with the deployment--it was the file reference itself in my java servlet. The encoding in the path '%20' in the Program Files folder caused a FileNotFoundException. The reason this did not occur in netbeans is because the deployed files are in a c:\users... folder and there are no spaces to be encoded. The also explained why the application ran well on other computers with their tomcat folders directly on the root directory. This is the actual solution to my problem. Thanks for all your help.

How to install JDBC driver in Eclipse web project without facing java.lang.ClassNotFoundexception

There is a VERY similar question to mine but in my case I don't have any duplicate jars in my build path, so the solution does not work for me. I've searched google for a couple of hours now, but none of the solutions I've found there actually resolve my issue. I'm creating a web site with some database connectivity for a homework. I'm using a MySQL database, developing in Eclipse and running on Windows.
I keep getting java.lang.ClassNotFoundException: com.mysql.jdbc.Driver with the following code:
import java.sql.*;
//...
public void someMethodInMyServlet(PrintWriter out)
{
Connection connection = null;
PreparedStatement query = null;
try {
out.println("Create the driver instance.<br>");
Class.forName("com.mysql.jdbc.Driver").newInstance();
out.println("Get the connection.<br>");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "secret");
query = connection.prepareStatement( "SELECT * FROM customers");
//...
} catch (Exception e)
{
out.println(e.toString()+"<br>");
}
}
//...
When I run the above code I get the following output:
Create the driver instance.
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
It doesn't get past the Class.forName... line and I can't figure out why! Here is what I did:
Download mysql-connector.
Put it in my MySQL folder C:\Program Files\MySQL\mysql-connector-java-5.1.12\mysql-connector-java-5.1.12-bin.jar.
Opened the project properties in Eclipse.
Add External Jar to my Build Path and I selected mysql-connector-java-5.1.12-bin.jar.
Every time I attempt to use the servlet I get the same error regardless if I have the jar in there or if I don't. Could you help me figure this out?
As for every "3rd-party" library in flavor of a JAR file which is to be used by the webapp, just copy/drop the physical JAR file in webapp's /WEB-INF/lib. It will then be available in webapp's default classpath. Also, Eclipse is smart enough to notice that. No need to hassle with buildpath. However, make sure to remove all unnecessary references you added before, else it might collide.
An alternative is to install it in the server itself by dropping the physical JAR file in server's own /lib folder. This is required when you're using server-provided JDBC connection pool data source which in turn needs the MySQL JDBC driver.
See also:
How to add JAR libraries to WAR project without facing java.lang.ClassNotFoundException? Classpath vs Build Path vs /WEB-INF/lib
How should I connect to JDBC database / datasource in a servlet based application?
Where do I have to place the JDBC driver for Tomcat's connection pool?
JDBC CLASSPATH Not Working
Since you are running it in servlet, you need to have the jar accessible by the servlet container. You either include the connector as part of your application war or put it as part of the servlet container's extended library and datasource management stuff, if it has one. The second part is totally depend on the container that you have.
The others are right about making the driver JAR available to your servlet container. My comment was meant to suggest that you verify from the command line whether the driver itself is intact.
Rather than an empty main(), try something like this, adapted from the included documentation:
public class LoadDriver {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
}
}
On my platform, I'd do this:
$ ls mysql-connector-java-5.1.12-bin.jar
mysql-connector-java-5.1.12-bin.jar
$ javac LoadDriver.java
$ java -cp mysql-connector-java-5.1.12-bin.jar:. LoadDriver
On your platform, you need to use ; as the path separator, as discussed here and here.
Place mysql-connector-java-5.1.6-bin.jar to the \Apache Tomcat 6.0.18\lib folder. Your problem will be solved.
What you should not do do (especially when working on a shared project)
Ok, after had the same issue and after reading some answers here and other places. it seems that putting external lib into WEB-INF/lib is not that good idea as it pollute webapp/JRE libs with server-specific libraries - for more information check this answer"
Another solution that i do NOT recommend is: to copy it into tomcat/lib folder. although this may work, it will be hard to manage dependency for a shared(git for example) project.
Good solution 1
Create vendor folder. put there all your external lib. then, map this folder as dependency to your project. in eclipse you need to
add your folder to the build path
Project Properties -> Java build path
Libraries -> add external lib or any other solution to add your files/folder
add your build path to deployment Assembly (reference)
Project Properties -> Deployment Assembly
Add -> Java Build Path Entries
You should now see the list of libraries on your build path that you can specify for inclusion into your finished WAR.
Select the ones you want and hit Finish.
Good solution 2
Use maven (or any alternative) to manage project dependency
Just follow these steps:
1) Install eclipse
2) Import Apache to eclipse
3) Install mysql
4) Download mysqlconnector/J
5) Unzip the zipped file navigate through it until you get the bin file in it. Then place all files that are present in the folder containing bin to C:\Program Files\mysql\mysql server5.1/
then give the same path as the address while defining the driver in eclipse.
That's all very easy guys.
If the problem still persists,
Put the-
mysql-connector-java-5.0.8-bin jar in a place inside your Tomcat->lib->folder (No matter where you've installed your Tomcat). And change your environmental variable (Done by clicking Properties of Mycomputer -Advanced system settings- Environmental variables-And set a new variable name & variable values as the place where your lib file resides.Dont forget to enter a ; at the end of the path)
If still problem persists
Try downloading commons-collections-2.0.jar (http://www.docjar.com/jar_detail/commons-collections-2.0.jar.html) and paste the jar in the same place where your mysql jar resides (ie) inside Tomcat-lib.
Clean your project-Stop your server- Finally try to run.
Many times I have been facing this problem, I have experienced ClassNotFoundException.
if jar is not at physical location.
So make sure .jar file(mysql connector) in the physical location of WEB-INF lib folder. and
make sure restarting Tomcat by using shutdown command in cmd.
it should work.
The only solution worked for me is putting the .jar file under WEB-INF/lib . Hope this will help.
assuming your project is maven based, add it to your POM:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
Save > Build > and test connection again. It works! Your actual mysql java connector version may vary.
Put mysql-connector-java-5.1.38-bin.jar to the C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib folder.by doing this program with execute
My issue was a little different. Instead of jdbc:oracle:thin:#server:port/service i had it as server:port/service.
Missing was jdbc:oracle:thin:# in url attribute in GlobalNamingResources.Resource. But I overlooked tomcat exception's
java.sql.SQLException: Cannot create JDBC driver of class 'oracle.jdbc.driver.OracleDriver' for connect URL 'server:port/service'
for this error:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
you need to:
Import java.sql.*;
Import com.mysql.jdbc.Driver;
even if its not used till app running.