How do I load a properties file?
Here are 2 most popular ways:
1) Use a ResourceBundle. See the java docs for the specifics of how the ResourceBundle class works. Using this method, the properties file must go into the WEB-INF/classes directory or in a jar file contained in the WEB-INF/lib directory.
2) Another {{{ // Assuming that you are in a Servlet extending HttpServlet
- // This will look for a file called "/more/cowbell.properties" relative // to your servlet Root Context
InputStream is = getServletContext().getResourceAsStream("/more/cowbell.properties"); Properties p = new Properties(); p.load(is); is.close(); }}}
</PRE>
How do I use log4j for all Tomcat log output?
To have Tomcat use log4j universally, place both log4j.jar and the Jakarta commons-logging.jar into the $TOMCAT_HOME/common/lib directory. Create your log4j properties file as $TOMCAT_HOME/common/classes/log4j.properties and configure the root logger. Here is the basic log4j.properties I used for this myself: <PRE> log4j.rootLogger=info, R log4j.appender.R=org.apache.log4j.ConsoleAppender log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%-5p %-30.30c{1} %x - %m%n </PRE> If you only need to use log4j in your own web app, just include log4j (and any log4j properties file) in your WAR.
(I could not find this in any FAQ or documentation as of Feb 11 04, but it has been discussed several times on the mailing list.)
How do I configure Tomcat for a remote debugging or Hot-Swaping?
To connect your debugger to the port 5005, set Java options like this:
<code> set JAVA_OPTS=-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005 </code>
How do I configure the Connectors?
On the FAQ there are links to Other Resources which should have a relevant link to your config. If not, there are the JK docs. The JK docs for 3, 4, and 5 are all virtually the same. JK2 is not quite ready for production. (YMMV) Lucky - there is a new committer which has been committing a lot of good jk2 patches so this statement may be outdated. (As of Dec 1, 2003 its not)
Tomcat Crashed What do I do?
These steps are in no particular order ...
- 0. Read the FAQ
- 1. Read the RELEASE NOTES - there is something about Linux in it
- 2. First look at the stack traces. I hope a stack trace was produced before the failure aborted the JVM process. After you get a few stack traces, see if a pattern appears. Trace back to source code if needed.
- 3. Patch the OS. (Or in some cases - UN-patch the OS)
- 4. Patch the JVM. (Or in some cases - UN-patch the JVM)
- 5. Linux Problem? - Read the RELEASE-NOTES
- 6. Look at commercial vendor support for other servlet engines. Sometimes the problem is Universal regardless of servlet engine and maybe a jvm/os/application code issue
- 7. Google - Maybe someone had this problem. I bet they did.
- 8. Google groups (I love Google newsgroups)
- 9. If the JVM is from a commercial vendor, (eg: IBM, HP) check their release notes and news groups
- 10. Using a database? Make sure jdbc type4 drivers are used. Check their release notes and news groups.
- 11. Tweak JVM memory params. Setting memory too high can be as bad as having memory too low. (If your memory settings are set too high, 1.3 JVMs may freeze while waiting for the entire gc to finish. Also if the JVM has too much memory, if may be starving other resources on the machine which are needed which may be causing unforeseen exceptions. In a nutshell, throwing more memory doesn't always solve the problem. YMMV)
- 12. Turn off the JIT compiler. See the Java Docs on how to do this.
TomcatWeb also has some short step-by-step Tomcat - Webserver interfacing documentation.
How do I share sessions across webapps?
You cannot share sessions directly across webapps, as that would be a violation of the Servlet Specification. There are workarounds, including using a singleton class loaded from the common classloader repository to hold shared information, or putting some of this shared information in a database or another data store. Some of these approaches have been discussed on the tomcat-user mailing list, whose archives you should search for more information.
Sharing sessions across contains for clustering or replication is a different matter altogether.
Why doesn't the netbeans example build for me?
I have found 2 issues with the build.xml provided here: http://jakarta.apache.org/struts/faqs/netbeans.html
First, add this to the top of your properties: <property environment="env" />
Next find this line in the "war" target: <classes dir="${build.dir}" includes="**/*.properties" />
Change it so it reads like this: <classes dir="${src.dir}" includes="**/*.properties" />
Now it will work!
I'm encountering classloader problems when using JNI under Tomcat
The important thing to know about using JNI under Tomcat is that one cannot place the native libraries OR their JNI interfaces under the WEB-INF/lib or WEB-INF/classes directories of a web application and expect to be able to reload the webapp without restarting the server. The class that calls System.loadLibrary(String) must be loaded by a classloader that is not affected by reloading the web application itself.
Thus, if you have JNI code that follows the convention of including a static initilaizer like this:
{{{ class FooWrapper {
- static {
- System.loadLibrary("foo");
- } }}}
then both this class and the shared library should be placed in the $CATALINA_HOME/shared/lib directory.
The symptom of this problem that I encountered looked something like this - {{{ java.lang.UnsatisfiedLinkError: Native Library WEB-INF/lib/libfoo.so already loaded in another classloader
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1525) }}}
I'm encountering exceptions when using RMI in Tomcat webapp under Windows
If you have Tomcat installed in a path with emebedded spaces - i.e. C:\Program Files\Apache Group\... and try to use RMI You may receive the exception:
<code> {{{ ERROR: The following exception occured - RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: java.net.MalformedURLException: no protocol: Files/Apache }}}
</code>
The RMI classes try to use the Directory path as a URL without encoding the spaces. I resolved this problem, after quite some googling, by editing catalina.bat and placing the following property in the CATALINA_OPTS env var.
<code> -Djava.rmi.server.useCodebaseOnly=true </code>
i.e.:
<code> set CATALINA_OPTS=%CATALINA_OPTS% -Djava.rmi.server.useCodebaseOnly=true </code>
If you still have the problem (although this worked fine for me) you may also add the encoded codebase as well. i.e.:
<code> -Djava.rmi.server.useCodebaseOnly=true -Djava.rmi.server.codebase=file:///Program+Files/Apache+Group/tomcat/webapps/cocoon/WEB-INF/classes/ </code>
Setting up SSL - from the tomcat user list archives with Verisign: http://marc.theaimsgroup.com/?l=tomcat-user&m=106285452711698&w=2 and : http://marc.theaimsgroup.com/?l=tomcat-user&m=107584265122914&w=2 with ["OpenSSL"]?: http://marc.theaimsgroup.com/?l=tomcat-user&m=106293430225790&w=2 or this: http://marc.theaimsgroup.com/?l=tomcat-user&m=106453566416102&w=2 and this: http://marc.theaimsgroup.com/?l=tomcat-user&m=106621232531781&w=2 Plus, "what SSL is all about anyway": http://marc.theaimsgroup.com/?l=tomcat-user&m=106692394104667&w=2 If you're looking for a certicate provider, check here: http://www.sslreview.com/
How do I load webapps in Embedded Tomcat?