JavaTechie

Its all about Technology

java.lang.OutOfMemoryError: Java heap space January 10, 2008

Filed under: Java — javatechie @ 2:15 pm

Problem:

The error ‘java.lang.OutOfMemoryError: Java heap space’ is caused when the JVM (Java Virtual Machine) runs out of available memory during processing. This can be caused by:

  • a large number of files on your system
  • an extremely large benchmark document
  • an OVAL test that returns a large number of results
  • a system that doesn’t meet the minimum memory requirement of the NG Scoring tool (256MB)

The NG tool by default sets a maximum heap size of 256MB. The directions below describe how to increase this value on UNIX and Windows systems.

Solution:

For UNIX systems:

When running the tool include the heap space argument ‘-Xmx’. For example:

./ng.sh -Xmx512m

Where ‘-Xmx512m’ sets the maximum heap size to 512MB of memory. Higher values may be used where additional memory is available.

For Windows systems:

1. Edit cli.bat located in the directory the tool was installed to. This directory is ‘c:\Program Files\The Center for Internet Security\CIS NG Scoring Tool’ by default on most systems.

2. On line 33 change the text -Xmx256m to a larger value. For example:

java -Xmx512m -Djava.library.path=./lib -classpath ‘%CLASSPATH%’ org.cis.cli.Application %*

Where ‘-Xmx512m’ sets the maximum heap size to 512MB of memory. Higher values may be used where additional memory is available.

3. Repeat step 2 on gui.bat also located in the directory the tool was installed to.

Memory Problems With Mysql Connector

GOOD:
mysql.jar (221 kb, 10/7/2003)
mysql-connector-java-3.0.16-ga-bin.jar (231 kb, 11/16/2004)
mysql-connector-java-3.0.17-ga-bin.jar (241 kb, 6/22/2005)
BAD (memory leak):
mysql-connector-java-3.1.10-bin.jar (409 kb, 6/23/2005)
mysql-connector-java-3.1.14-bin.jar (449 kb, 10/18/2006)
… (other versions)
mysql-connector-java-5.1.6-bin.jar (687 KB, 3/5/2008)

 

Multiple Instances of Tomcat with Apache January 7, 2008

Filed under: Java — javatechie @ 6:28 am
Tags: ,

Learn how to use mod_jk to forward requests to specific hosts when more than one Tomcat instance is running.

Recently, I was asked to reorganize some of our Web applications to improve their stability. The major push was to get each of our applications running in its own instance of Tomcat. These applications all are in various stages of development, and if a single instance allocates all of the memory available, the entire Tomcat server must be restarted. This, in turn, brings down all of the other applications.
(more…)

 

Tomcat 5 on Fedora Core 6: In Five Easy Steps January 7, 2008

Filed under: Java — javatechie @ 6:25 am
Tags: , ,

Install FC6 and Apache Web Server

Stick the Fedora Core 6 DVD in and reboot. Then follow the instructions. I took all defaults where I can, including letting it “delete all Linux partitions” and automatically partition the disk. I choose to install “Web Server” and “Development Tools” when offered the chance. This installs the Apache Web Server 2.2.3 on the box.

Install Tomcat

Tomcat 5 is included in Fedora Core 6, but not installed by default. So I have to bring it in from the repository:

[root@root]# yum install tomcat5 tomcat5-webapps tomcat5-admin-webapps

This installs Tomcat 5.5.17 and a lot of their dependencies onto the system.

Read more

 

Send emails using PHPMailer and GMail January 7, 2008

Filed under: Mail, PHP — javatechie @ 6:21 am
Tags:

Sending emails using PHPMailer_v5.0.2 and GMail

Requirements :

1 : PHPMailer source code
2 : GMail account

Download the PHPMailer source code from http://sourceforge.net/project/showfiles.php?group_id=26031&package_id=252700

Now create a file you call mail.php on your harddisk and open it with your favorite text editor. Paste this code into it :

<?php

//error_reporting(E_ALL);
error_reporting(E_STRICT);

date_default_timezone_set('America/Toronto');

require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail             = new PHPMailer();

$body             = file_get_contents('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
 // 1 = errors and messages
 // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
$mail->Username   = "yourusername@gmail.com";  // GMAIL username
$mail->Password   = "yourpassword";            // GMAIL password

$mail->SetFrom('name@yourdomain.com', 'First Last');

$mail->AddReplyTo("name@yourdomain.com","First Last");

$mail->Subject    = "PHPMailer Test Subject via smtp (Gmail), basic";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");

$mail->AddAttachment("images/phpmailer.gif");      // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send()) {
 echo "Mailer Error: " . $mail->ErrorInfo;
} else {
 echo "Message sent!";
}

?>
 

Virtual Hosting with Tomcat January 7, 2008

Filed under: Apache — javatechie @ 6:04 am
Tags: , , ,

This is a guide on setting up Tomcat to do virtual hosting and make it behave like a simple webserver with jsp and servlet support, for many different sites all hosted on the same IP address. The aim is to have a single directory for each virtual host, which can be manipulated individually without hassles from managing multiple .war files and other configuration difficulties.

To configure Tomcat for a virtual host, you need a directive in the server.xml file, and a ROOT.xml file in the conf/Catalina/$host directory. Here’s the minimal setup required for a copy of Tomcat serving directly on Port 80, using no connectors or other configuration difficulties.

This was written for Tomcat 5 on linux, with Tomcat installed in /usr/local/tomcat

We start with the simplest configuration, of one website, called ‘localhost’ which keeps it’s files in /usr/local/tomcat/webapps/localhost . We’re not using any .war files here – all the files are placed straight into the directory.

Read more

 

PHP/MySQL order by rand() January 7, 2008

Filed under: Uncategorized — javatechie @ 6:01 am
Tags: ,

I m trying to select a random row from a table in a MySQL database using
PHP. The MySQL version is 3.23.53, so I have access to this syntax:

SELECT * FROM $table ORDER BY RAND()

 

Creating Custom Error Messages in Apache January 7, 2008

Filed under: Uncategorized — javatechie @ 5:59 am
Tags:

Below is an example of a httpd.conf file, just copy and paste into your file, but change the paths:

ErrorDocument 404 /path/to/your/error/file/or/script
ErrorDocument 500 /path/to/your/error/file/or/script
ErrorDocument 403 /path/to/your/error/file/or/script

A very important point to note is that the paths should be the relative paths. This means that the paths would be what you would type on the machine to get to the files, not what you would type in the address box in a browser.

Examples:
Full path would be http://www.yoursite.com/path/to/error/page
Absolute path would be /home/yoursite.com/path/to/error/page
Relative path would be /path/to/error/page

 

Redirecting subdomains to directories in apache. January 7, 2008

Filed under: Uncategorized — javatechie @ 5:57 am
Tags:

If you want to be able to redirect blah.domain.com to domain.com/blah/ so that blah.domain.com is shown in the address bar here’s a quick and dirty how to:

First off if you want any subdomain to be able to be redirected first you need to set-up your DNS correctly. In my case this was as simple as going to the people that host my domain and adding an A record with the wildcard * that pointed to my server’s ip address. Bear in mind your DNS settings may take a while to work so be patient. Before you proceed you should check that anything.domain.com resolves to the ip address of your server. You can check this by running:

ping iloveapache.domain.com

The next step is to set-up the virtual host to respond to the wildcard subdomains. In the case of plesk you have to set-up a vhost.conf file but on any other apache installation you can just edit httpd.conf and look for the that corresponds to your site. If you are using Plesk you will need to ssh in to your server and create a vhost.conf file if it doesn’t already exist here where is your domain:

/var/www/vhosts//conf/vhost.conf

Open that file with the command line editor of your choice and add the following directives:

ServerAlias *.domain.com
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.* [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com
RewriteCond /var/www/vhosts/domain.com/httpdocs/%1 -d
RewriteRule ^(.*) /%1/$1 [L]

Substitute domain.com for your domain. Now here’s a quick walkthrough how these rules work. The first line ServerAlias *.domain.com tells apache to accept anysubdomain.domain.com.

Next the rewrite rules work as follows:

1. The first line of mod_rewrite !^www.* only matches urls that don’t begin with www. You don’t want to redirect www.domain.com to www.domain.com/www/ do you? Note: you could add additional lines like these to prevent re-writing other subdomains like secure.domain.com if that should not be redirected to domain.com/secure/
2. The second line ^([^\.]+)\.*$ captures into a backreference anything that doesn’t have a period e.g. the subdomain. The plus makes sure that there is at least one character that matches.
3. The next condition checked is that the directory captured into the backreference does actually exist hence the -d.
4. This last line ^(.*) /%1/$1 [L] takes the requested path and dumps it into a back reference and then rewrites it to point to the a directory (the subdomain backreference captured in the Rewrite conditions above) . In other words %1 is the subdomain backreference and $1 is the path backreference (that captured by .*). The important thing to rember is you can refer to backreferences in rewriteCond lines with % and backreferences in rewriteRules with $. Lastly the [L] (last) means doen’t further rewrite this URL. Note: You might want to leave this out if you are doing any subsequent rewrites, be sure to test what you have set-up thoroughly!!!

And then whether are using Plesk or not you need to restart apache e.g:

service httpd restart

 

Apache HTTPD subdomain configuration with Virtual Hosts on a local machine January 7, 2008

Filed under: Java — javatechie @ 5:51 am
Tags:

Open up your HOSTS file to add the changes to your system (so that Windows knows where to look when you ask for that domain):
Look for:

C:/WINNT/SYSTEM3/DRIVERS/ETC/HOSTS

and open up in your fav text editor.
Then add the new virtual hosts to the file (see my edited file below – note the differences) so that:

127.0.0.1 impistage would now become 127.0.0.1 abc.oneafrikan.com.

And that’s it really – fire up Apache again

Open up your Apache httpd.conf file, then go to:

C:Program Files/Apache Group/Apache/conf

and open up the file in your favourite text editor and go to the (usually) bottom of the file, and look for the bit about Virtual Hosts.

My local server Virtual Host used to have:

ServerName impistage with the DocumentRoot set to “C:/htstage” and the Directory set to “c:/htstage“.

My local Virtual Host now has it looking like:

ServerName staging.oneafrikan.com with the DocumentRoot set to C:/htstage/oneafrikan.com and the Directory set to C:/htstage/oneafrikan.com.

 

Tomcat 5.5.17, with mod_jk on Windows January 7, 2008

Filed under: Uncategorized — javatechie @ 5:43 am
Tags:

Requirements

  • Java 1.5 JDK (SDK)
  • Apache 2.0
  • mod_jk 1.2

Download

Configuration

  • Java JDK path : C:\jdk1.5.0_07
  • Apache path : C:\www\Apache2
  • Tomcat path : C:\www\tomcat5
  • Apache’s webroot : C:\www\webroot
  • JSP/Servlet webroot (via Apache2/mod_jk) : C:\www\tomcat5\webapps
  • JSP/Servlet webroot (via native Tomcat server, port 8080) : C:\www\tomcat5\webapps
  • Access JSP/Servlet pages via Apache2/mod_jk : http://localhost/[jkmount-mapped url of your webapp]
  • Access native Tomcat server : http://localhost:8080/

Unpack and place Tomcat5 and mod_jk

  • Unpack file apache-tomcat-5.5.17.tar.gz under directory C:\www
  • Rename directory C:\www\apache-tomcat-5.5.17 to C:\www\tomcat5
  • Rename file mod_jk-apache-2.x.xx.so to mod_jk.so, and place under directory C:\www\Apache2\modules

Configuration and Setup

  • Set Environmental Variables : JAVA_HOME=C:\jdk1.5.0_07 and CATALINA_HOME=C:\www\tomcat5
    • Set variables under Start » Settings » Control Panel » System » Advanced » Environment Variables » System variables » New…
  • Create empty file C:\www\Apache2\conf\workers.properties, insert code…

workers.tomcat_home=C:/www/tomcat5workers.java_home=C:/jdk1.5.0_07ps=\

# Define worker 'example'worker.list=example

# Set properties for worker 'example' (ajp13)worker.example.type=ajp13worker.example.host=localhostworker.example.port=8009

worker.example.cachesize=10worker.example.cache_timeout=600worker.example.socket_keepalive=1worker.example.recycle_timeout=300

Edit file C:\www\Apache2\conf\httpd.conf, insert code...

LoadModule jk_module modules/mod_jk.so

<ifmodule>

  JkWorkersFile conf/workers.properties  JkLogFile logs/mod_jk.log  JkLogLevel error  JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "  JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories  JkRequestLogFormat "%w %V %T"

  Alias /jsp-examples "C:/www/tomcat5/webapps/jsp-examples/"

  <directory>      Options Indexes +FollowSymLinks      AllowOverride None      Allow from all </directory>

  Alias /servlets-examples "C:/www/tomcat5/webapps/servlets-examples/"

  <directory>      Options Indexes +FollowSymLinks      AllowOverride None      Allow from all  </directory>

 <location>      AllowOverride None      deny from all</location>

  JkMount /jsp-examples/*.jsp example  JkMount /servlets-examples/* example

</ifmodule>

Install and Start Tomcat

Tomcat can be installed as a Service or started as a Standalone Console application. Note to make sure that Apache has been started at this point.