Showing posts with label Tomcat. Show all posts
Showing posts with label Tomcat. Show all posts

Thursday, 12 January 2012

Tomcat+Spring: how to load a resource (file) from the application path

Method #1

The following code shows how to read a file called src/a/b/file.txt from src/a/b/Foo.java:
package a.b;

public class Foo {
 ...

  // reads src/a/b/file.txt
  InputStream is = getClass().getResourceAsStream("file.txt");
 
 ...

}

Method #2

InputStream is = Foo.class
                .getClassLoader()
                .getResourceAsStream("a/b/file.txt");

Wednesday, 11 January 2012

Tomcat needs excessive memory!

I get all the time PermGen and other memory errors while I start/run tomcat on a VPS (Virtual Private Server).

Solution: At least this has worked for me... Edit catalina.sh:

export CATALINA_OPTS="-XX:-DoEscapeAnalysis -XX:MaxPermSize=192m"

Sunday, 8 January 2012

Tomcat: change the default ROOT "/" web application

I have copied foo.war into webapps. I want to access it when I request the URL http://localhost:8080/ (not I can access it on http://localhost:8080/foo/) Edit /etc/tomcat6/server.xml
<Host ...>
   <Context path="" docBase="foo" debug="0" reloadable="true">
   </Context>
</Host>

How it should be..not working!

OK..I don't like this invasive way of editing server.xml. There is another way that didn't work for me (why?anybody?)

Edit /etc/tomcat6/Catalina/localhost/ROOT.xml:
<Context path="/"                                                                                                                                                              
         docBase="/foo.war"                                                                                                                                                 
         reloadable="true"                                                                                                                                                     
         antiResourceLocking="false" />

References

http://techlightening.wordpress.com/2009/05/13/tomcat-change-default-web-application/

How to deploy a war

Manually

Copy foo.war into $CATALINA_BASE/webapps/. Then access the app on http://localhost:8080/foo/

With Tomcat Manager

Make sure you enable the user of the Tomcat Manager...see relative post here

Friday, 6 January 2012

Tomcat error: "java.lang.OutOfMemoryError: PermGen space"

Edit $CATALINA_HOME/bin/catalina.sh and add the comments in the beginning:
export CATALINA_OPTS="-XX:MaxPermSize=216m"
Comment: default PermGen size for tomcat is 128MB.

Howto: Tomcat 6 installation in Ubuntu

Install:
$ sudo apt-get install tomcat6 tomcat6-admin tomcat6-user
Start/stop:
$ sudo service tomcat6 stop
$ sudo service tomcat6 start
Handy paths:
  • /usr/share/tomcat6/bin/{startup.sh,shutdown.sh}
  • /usr/share/tomcat6/webapps
  • /usr/share/tomcat6-admin
  • tail -f /var/log/tomcat6/*
  • /etc/default/tomcat6

Tomcat install in ubuntu: error "java.net.BindException: Permission denied :80"

catalina.out error:
Jan 7, 2012 12:52:59 AM org.apache.catalina.startup.Catalina start
SEVERE: Catalina.start: 
LifecycleException:  service.getName(): "Catalina";  Protocol handler start failed: java.net.BindException: Permission denied :80
 at org.apache.catalina.connector.Connector.start(Connector.java:1087)
 at org.apache.catalina.core.StandardService.start(StandardService.java:531)
 at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
 at org.apache.catalina.startup.Catalina.start(Catalina.java:593)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
 at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)

Solution

Edit /etc/default/tomcat6 and set last line equal to:
# If you run Tomcat on port numbers that are all higher than 1023, then you                                                                                                    
# do not need authbind.  It is used for binding Tomcat to lower port numbers.                                                                                                  
# NOTE: authbind works only with IPv4.  Do not enable it when using IPv6.                                                                                                      
# (yes/no, default: no)                                                                                                                                                        
AUTHBIND=yes

What is authbind?

$ apt-cache show authbind
 Allows non-root programs to bind() to low ports
 This package allows a package to be started as non-root but
 still bind to low ports, without any changes to the application.

References

http://thelowedown.wordpress.com/2010/08/17/tomcat-6-binding-to-a-privileged-port-on-debianubuntu/

Tomcat: install APR

$ sudo apt-get install libtcnative-1

Verify installation: when Tomcat start in catalina.sh should exist the line "INFO: Loaded APR based Apache Tomcat Native library 1.1.19."

But for Tomcat v.7.0.23 (still not solved): "INFO: An older version 1.1.19 of the APR based Apache Tomcat Native library is installed, while Tomcat recommends a minimum version of 1.1.22"

Comment: what's the difference with the package libapr1?

Tomcat: enable Tomcat Manager

Tomcat 7.0.23

edit $TOMCAT_HOME/conf/tomcat-users.xml and add a user:
<tomcat-users>
  <role rolename="manager-gui" />
  <user username="myusername" password="mypassword" roles="manager-gui" />
</tomcat-users>
Access Tomcat Manager: http://localhost:8080/manager/html

Tomcat 6

edit $TOMCAT_HOME/conf/tomcat-users.xml and add a user:
<tomcat-users>
  <role rolename="manager" />
  <user username="myusername" password="mypassword" roles="manager" />
</tomcat-users>
Access Tomcat Manager: http://localhost:8080/manager/html

Tomcat start error: "Error occurred during initialization of VM"

Edit $CATALINA_HOME/bin/catalina.sh and add the comments in the beginning:
export CATALINA_OPTS="-Xms512m -Xmx512m"

Tomcat: change default port 8080 into 80

Edit tomcat/conf/server.xml and change:

into:

And not restart tomcat:
$ sudo $TOMCAT_HOME/bin/startup.sh
$ sudo $TOMCAT_HOME/bin/shutdown.sh

Sunday, 11 December 2011

Tomcat 7 says that it supports EL 2.2 but methods calling with arguments doesn't work (solved)

Summary

Tomcat provides EL specification ($TOMCAT_HOME/lib/el-api.jar) and Jasper implementation (jasper.jar,jasper-el.jar). On Tomcat Homepage it says that it supports EL 2.2 specification which allows us to call from facelet methods of beans with arguments, eg mybean.mymethod(3). Unfortunatelly for me methods with arguments are recognized as properties from Tomcat. Obviously EL spec 2.2 doesn't work with Tomcat 7 (tested with apache-tomcat-7.0.23).

Solution

We will use the implementation of EL 2.2 from GlassFish. web.xml Override the expression factory implementation using org.apache.myfaces.EXPRESSION_FACTORY on MyFaces or com.sun.faces.expressionFactory on Mojarra:
        
        
 
  org.apache.myfaces.EXPRESSION_FACTORY
  com.sun.el.ExpressionFactoryImpl
  
 
  com.sun.faces.expressionFactory
  com.sun.el.ExpressionFactoryImpl
 
Note: another more violent solution would be deleting Jasper from Tomcat. pom.xml
  
   org.glassfish.web
   el-impl
   2.2.1-b05
  

Wednesday, 9 November 2011