Tuesday, December 15, 2009

How to increase the heap size available to a JUnit Test Class in Eclipse?

If your running a JUnit Test on Eclipse, do the following to increase the heap size available for it:
1. Click on 'Run' and 'Open Debug Dialog'
2. Click on 'JUnit' on the left pane of the window
3. Enter a name for the JUnit test
4. Fill up all the relevant details in the 'Test' tab
5. Click on the 'Arguments' tab
6. In 'VM arguments', type the following:
-Xmx1024M

Monday, November 2, 2009

Getting the attributes of an OpenLayer.Feature.Point that has been clustered in a OpenLayers.Layer.Vector layer

I encountered this problem and spent a day trying to understand how it worked. I have figured it out and am sharing my understanding with everyone.

Issue
------------
I had a GeoJSON parser that parsed the following geojson file:

{
"type": "FeatureCollection",
"features": [
{"type":"Feature", "id":"computer1", "properties":{"trial":"test"}, "geometry":{"type":"Point", "coordinates":[143, -35.91]}},
{"type":"Feature", "id":"computer2", "properties":{"trial":"test"}, "geometry":{"type":"Point", "coordinates":[144, -37.81667]}}
]
}

Once the GeoJSON parser parsed the file and created 2 OpenLayer.Feature.Points, these points were parsed to the OpenLayers.Layer.Vector object that implemented the clustering strategy.

Now, since the two points were within a close distance (specified by me), they were clustered and only one top level feature point was displayed on the map.

I wanted to get the 'id' and 'properties' (as in the GeoJSON file above) of each of the two points (when I zoomed into the map). However, I could not get it. It seemed to me, back then, that the GeoJSON parser had somehow overriden the properties I added into the "properties":{} field (in the above geojson file) with the 'count' property of the clustering Vector layer.

Hence, the question was, how do I get the attributes and ID of each point in the map (as in the geojson file).


Fix
----
feature.cluster solved the problem.

For other users who come across this problem, try to get the attributes of a point (specified in the geojson file in the 'properties' list) by using
feature.cluster[0].attributes.

Basically, this is what happens when the geojson fileabove is parsed by OpenLayers.Format.GeoJSON parser and added as OpenLayers.Vector.Feature objects into a OpenLayers Vector layer (that performs the clustering strategy):
1. GeoJSON file is read by the GeoJSON parser.
2. Parser creates OpenLayers.Vector.Feature objects for each string.
3. These OpenLayers.Vector.Feature objects are then added to the OpenLayers Vector layer
4. OpenLayers Vector layer performs clustering strategy
5. Each feature point that appears on the map is the top-level feature point (for a group of clusters). The attributes for this feature point is:

undefinedlayer value :[object Object]
lonlat value :null
data value :[object Object]
id value :OpenLayers.Feature.Vector_201
geometry value :POINT(15918687.18122222 -4288244.5663272375)
state value :null
attributes value :[object Object]
style value :null
cluster value :[object Object],[object Object],[object Object] //this indicates that this cluster point has three feature objects underneath it
renderIntent value :select
marker value :null

6. As it can be seen above, there is a 'cluster' property that holds 3 objects (being the three points). Hence, if you drill down to the 'cluster' property (i.e. feature.cluster[0]), it will give you the attributes of the first object in the cluster (being the point):

undefined0 value :[object Object]
layer value :null
lonlat value :null
data value :[object Object]
id value :OpenLayers.Feature.Vector_197
geometry value :POINT(8218510.393273033 -2699238.272071229)
state value :null
attributes value :[object Object]
style value :null
fid value :computer14
marker value :null

7. From the above, it can be seen that the 'id' property from the geojson file has been mapped to the 'fid' key of the feature point object. You can also see that the 'attributes' key holds an [object Object]. If you do feature.cluster[0].attributes.trial , it will display the 'trial' property and value that you specified in the geojson file (i.e. 'test').

Hence, adding as many property-value pairs in the GeoJSON file is easy as the GeoJSON parser just parsers the property-value pairs and enters the data into feature point object attributes that are beneath the top-level cluster point (it does not override the properties as I initially thought).

Friday, October 30, 2009

Printing Javascript object properties

If you would like to print out all the properties of an Javascript object and their associated values, do this:

Note: Replace < objName > below with the name of your instantiated object.

var str;

for(prop in < objName >)
{
str += prop + ' value :' + < objName >[prop]+ '\n ';
}

alert(str); //Show all properties and its value

Friday, October 16, 2009

Resolving the Exception in thread "main" java.lang.UnsupportedClassVersionError: com/google/gwt/dev/GWTCompiler(Unsupported major.minor version 48.0)

When I tried compiling my GWT project using TestProject-compile.cmd, I got the following exception:
Exception in thread "main" java.lang.UnsupportedClassVersionError:
com/google/gwt/dev/GWTCompiler (Unsupported major.minor version 48.0)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)



How To Resolve This?

Ensure that your JRE version in your environment variable in anything from Version 1.4 and above.
To check your Java version, pull up comman prompt and type in the following on the command line to check your JRE version:
java -version

Since I had Oracle installed, my "Path" environment variable was referencing 2 Oracle JVM's that were versions 1.1.3 and 1.1.8. I deleted these two paths and ensured that JVM path in the environment variable was pointing to the folder where I had installed Java 1.6.

This worked and did not cause the UnsupportedClassVersionError exception to be thrown anymore.

Tuesday, October 13, 2009

How to print output on the same line on the console in Java?

Trying to continuously print output onto the same line in a console using Java?

Try this:
System.out.print("TEST TEXT\r");

The "\r" (carriage return) character returns the cursor to the beginning of the output line.

Just remember that if you do System.out.print("TESTTEXT\r"); and then System.out.print("BLAH\r");, your final output will be
BLAHTEXT

This is because the remaining characters from the longest string are not deleted after the shorter string overwrites it. Hence try to do something like this to delete of all characters from the longest string:
System.out.print("TESTTEXT\r"); //write text and go back to beginning of line
System.out.print(" \r"); //have many empty spaces to overwrite the chars from the longest string and then go back to the beginning of the line
System.out.print("BLAH\r"); //write text and go back to the beginning of the line

This will output:
BLAH

Monday, October 12, 2009

Internet Information Services - Setting Up Access Permissions for Specific Windows Users to Access Specific FTP Virtual Directories

To set up the Windows IIS FTP Server to allow certains Windows users to access certain virtual directories, ensure that you have done the following:
1. Install an IIS FTP Server
2. Ensure that you have a Windows User account set up for the user you want to log on as, onto your FTP Server.
3. In the 'Default FTP Site Properties', uncheck the 'Allow Anonymous Connections' checkbox which can be found under the 'Security Accounts' tab. This will allow specific users that have the right username and password of one of the Windows user accounts on the FTP Server machine to access the FTP Virtual Directories.

Once Step 1 and 2 above is set up, permissions to specific FTP Virtual Directories can be set up as follows:
3. Right click on the virtual directory you have created (eg. CHITRATEST). Below this Local Path textbox, there are three checkboxes (Read, Write and Log Visits). You can uncheck/check these boxes. Think of these boxes as three different pipes. Clogging a pipe (i.e. unchecking a say, 'Write' checkbox) will cause all Windows users who log on to the FTP Server to not have Write permissions to CHITRATEST, regardless of whether the folder on the local path of the virtual directory (e.g. c:\TEST) has 'Write' permission enabled for the currently logged in Windows Users.
4. Once the FTP Server evaluates the super access priviledges it can give to a currently logged Windows user (by evaluating the checkboxes in Step 3 above), it will then evaluate the next level folder properties of the virtual directory. If say, C:\TEST is mapped to a virtual directory called CHITRATEST, the Security permissions set on C:\TEST and C:\Inetpub\ftproot\CHITRATEST will both be evaluated to give the currently logged Windows user specific permissions. Both C:\TEST and C:\Inetpub\ftproot\CHITRATEST must have the exact permissions set/unset for folder level permissions to be evaluated by the FTP Server.

Summary
-------

To allow users with the username and password of a specific Window User account to access the FTP server, follow Steps 1 and 2.

To allow specific logged on Windows users (with a corresponding Windows User account) to gain access permissions to specific virtual directories, follow Step 3.

To allow a lower level folder access permission for logged on Windows users, follow Step 4.

Why does a file on a Windows machine have different timestamps on different Windows machines?

I have a program (which runs on a Windows machine) that gets a list of files and their corresponding timestamps from a remote Windows machine.

On the 4th of Oct (the day when Daylight savings triggered of a 1.59am to 3am time change in Melbourne, Australia), my program returned a null value when retrieving the last modified timestamp of a file that was created at 1.21am that day.

What puzzled me was that although the file was created at 1.21am (which was recorded within the contents of the file), the last modified timestamp was 2.21am. When I uploaded this file across to a machine that had the Windows option "Automatically Adjust for Daylight Saving Time" turned off, it had a last modified time of 1.21am.

After reading up on http://www.peterdonis.net/computers/computersarticle3.html and http://support.microsoft.com/kb/129574 , I finally realised why the last modified timestamp of the file on a Windows machine was different to the timestamp when it was actually created:

On Windows, if you are in a location that observes DST (Daylight Savings Time) and the DST time change kicks in, the timestamps on all files on the Windows NT file system (NTFS) are shifted by an hour. Even the files that were created BEFORE the daylight saving time change are shifted. So basically, the timestamps of ALL files created ANYTIME before 4/10/2009 1.59am will have one hour added to it.

As stated on the Windows Help and Support page, "All times displayed in Windows NT for event log events and files on NTFS partitions are computed as offsets to Greenwich Mean Time (GMT). When you set the time on your system, you are setting the value for GMT. When you select your local time zone for the system, the appropriate number of hours are added or subtracted to the stored GMT value. This adjusted time is displayed. When "Automatically Adjust for Daylight Saving Time" is selected, an additional hour is added to GMT during daylight savings time (the first Sunday in April through the last Sunday in October)." In other words, manually changing the system time does not affect file timestamps but when the local time zone is changed, all file timestamps will be adjusted to display the new zone time.

Friday, September 25, 2009

Postgres 8.3 error: Internal account lookup failure: no mapping between account names and security IDs was done

If you have encountered this error on a Windows XP machine, check to see if your machine name in the 'Account Domain' field (in the PostgreSQL installer GUI) is correct.

If you are pulling up the GUI through the command line which has the servicedomain='' parameter, your 'Account Domain' field will be populated to have the value ''. This is obviously erroneous because of the single quotes in the value.

It would be best to not specify the servicedomain='' parameter. Postgres usually pre-fills the 'Account Domain' field correctly.

Monday, September 21, 2009

SimpleDateFormat parse error - from 12pm to 12am

I got this error today, only to find out that it was a very simple code error that I overlooked.

The Error
-----------

I have a SimpleDataFormat parser to parse a date string variable to a required format:

String time = "Mon Sep 21 2009 12:56:02";
SimpleDateFormat df= new SimpleDateFormat("EEE MMM dd yyyy hh:mm:ss");
Date parsedDate = sdf.parse(time);

However, a System.out.println("parsedDate = " + df.format(parsedDate )); gives me a 00:56:02 time and not a 12:56:02 time.


Reason for error
-----------------
In the SimpleDateFormat class, formatting hour in HH will return a hour between 00-23 (i.e. in 24 hour format) while formatting hour in hh will return a hour between 01-12 (12 hour format). - a good source code to look at is http://www.java-examples.com/formatting-hour-using-simpledateformat

Ensure that your SimpleDateFormat class has been instantiated this way:
SimpleDateFormat df= new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss");

Wednesday, September 16, 2009

What is a Windows lmhosts file?

lmhosts stands for LAN Manager Hosts file. This host file is called 'hosts' and can be found in C:\WINDOWS\system32\drivers\etc

To put it very simply, it is a file that maps IP addresses to the NetBIOS host names of the machines. For example, write this into your hosts file:
192.168.1.1 Test-Machine

Hence, when 'Test-Machine' is pinged, it will be translated to '192.168.1.1' without needing to check your DNS.

Friday, July 24, 2009

Windows SYSTEM user

I have a program that copies files from one machine to another over a network. It uses port 445 (called the SMB port - SMB is a protocol for sharing files between computers in a network).

When I ran this program in MS DOS, the remote folder in which the remote files are in was recognised as a directory (i.e. dir.isDirectory() = true). However, when I ran this program from Windows Services, the remote folder was not recognised as a directory.

The reason for this was because of the user under which the program is ran. When it was run in MS DOS, the current Windows user (Administrator) was running the program. Hence, it had access priviledges to read/write the remote folder.

However, as the Windows Services process is controlled by System User, when the program was run in Windows Services, the program was run under the SYSTEM user, disallowing it to have access priviledges over the remote folder.

Basically, although a SYSTEM user is higher than an administrator and has full control of the OS and kernel, it cannot access remote systems. It is just for a local system.

To ensure that the program is run under the current Windows user (Administrator), you will need to set up the program's properties in services.msc. Click on the Properties of the program, then on the 'Log On' tab. Here, ensure that you log on as 'This Account' (type in your current Windows Administrator username and password) and NOT as 'Local System Account'.

This will allow the program (running as a Windows Service) to copy files over a network as it will be running under the current user. You can check the username it is running under by going to your Task Manager.

Thursday, July 23, 2009

Basic PostgreSQL Must-Know's

1. String concatenation using the PL/pgSQL language is done like this:

declare
split_table_name text[];
v_PK text;
v_Query text;

v_Query := 'select ' || v_PK || ' from ' || split_table_name[1];


2. To run the above query string variable (i.e v_Query), use the EXECUTE statement like this:

declare
result text;

EXECUTE v_Query into max_num_str ;

or

EXECUTE 'select ' || v_PK || ' from ' || split_table_name[1] into max_num_str ;

Advanced PostgreSQL Queries

All Postgres users, like it or not, will need to retrieve information about the tables, triggers, sequences etc that they have in their databases. The following are some of the things you might want to query:

1. What constraints does my table have?


select pgc.relname as "TableName", pc.conname as "ConstraintName",
contype as "ConstraintType", conkey as "KeyCols",
confkey as "ForeignCols", consrc as "Src"
from pg_class pgc, pg_constraint pc
where pgc.oid = pc.conrelid
and relname = '< tablename >';


2. How to get the column name of the primary key of a table?

select column_name from information_schema.key_column_usage where constraint_name IN
(
select c.conname as "ConstraintName"
from pg_class r, pg_constraint c
where r.oid = c.conrelid
and contype = 'p'
and relname = '< tablename >' );


3. What triggers are on my table?

select pc.relname as "Table", pt.tgname as "TriggerName",
pt.tgconstrname as "ConstraintName", pt.tgenabled as "Enabled",
pt.tgisconstraint as "IsConstraint", pcc.relname as "ReferencedTable",
pp.proname as "FunctionName"
from pg_trigger pt, pg_class pc, pg_class pcc, pg_proc pp
where pt.tgfoid = pp.oid and pt.tgrelid = pc.oid
and pt.tgconstrrelid = pcc.oid
and pc.relname = '< tablename >';


4. What indexes are on my table?


select * from pg_indexes where tablename = '< tablename >';


More Advanced PostgreSQL Queries can be found here:
http://www.thegeekstuff.com/2009/05/15-advanced-postgresql-commands-with-examples/

Monday, July 13, 2009

Setting the 'Show Desktop' keyboard shortcut in Ubuntu

Having just moved from a Windows environment to Linux, I have been trying to find my way around Ubuntu 9. Everytime I have something that I want to do in the OS, I have used the Windows way of doing it, without any success :p

For those of you trying to set up your Linux OS to 'Show Desktop' when the Windows + D key is pressed, this is what I found out on how to do it:
1. Go to System -> Preferences -> Keyboard Shortcuts

2. By default, Ubuntu responds to Ctrl-Alt-D to 'Show Desktop'. You can change this to Windows + D by firstly pressing the Windows key (or called Super in Linux which I havent quite figured out why), then simultaneously clicking on the keyboard shortcut for the "Hide all normal windows and set focus to the desktop background" option and hitting the 'D' key.

This will map a Windows-D keypress to 'Show Desktop'!

Friday, May 22, 2009

Firefox Sucking Up Your Memory?

THis is a good article to read on how to tune your Firefox: http://stayupdate.com/tips-tricks/increase-firefox-speed-and-decrease-firefox-memory-usage-20-tips/567

Wednesday, May 20, 2009

Divide 0 by 0

Here's a simple algo to divide 0.0 by 0.0:

double a = 0.0 / 0.0;
double b = 1.0 / 0.0;
double c = 0.0 / 1.0;
double d = 1.0 / 1.0;
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);

Here are the results:

NaN
Infinity
0.0
1.0


Good reference: http://www.math.utah.edu/~pa/math/0by0.html

Monday, May 4, 2009

Getting the rendered height of a html body tag

I came across this problem the other day.

I have a html page with a couple of divs in the body tag. My divs had lengthy text and images. Hence, when I made the border of page visible (by setting the border style of the body tag), the border encaptulated all the divs and was visible when i scrolled down.

When I then included a div that had a background image that I wanted to repeat vertically, this image only got repeated to the height of my screen (i.e. when I scrolled to the bottom of the page, I could see that the image did not get rendered all the way to the bottom). I used height = 100% for the style of this div.

After poking around, I realised that the height=100% means that the div (containing the vertical image) only had a height that was equivalent to the height of the visible screen. To get the FULL height of the body, I had to use this function, called from the body's onLoad event:

function borderheight()
{
var leftborder = parent.document.getElementById('leftborder');
leftborder.style.height = document.body.clientHeight + "px";

var rightborder = parent.document.getElementById('rightborder');
rightborder.style.height = document.body.clientHeight + "px";
}


Hope using clientHeight helps all of you who come across this problem.

GWT-EXT error: com.google.gwt.core.client.JavaScriptException: (TypeError): '$wnd.Ext.StatusBar' is null or not an object

The reason why you are getting this error is most probably because you have not installed ext and included the ext.jar file in your build path. Hence, you have a ext wrapper classes (eg. Gwt-Ext) but not EXT itself.

First of all, download EXT 2.0.2 from http://yogurtearl.com/ext-2.0.2.zip (link can be seen in http://code.google.com/p/gwt-ext/)

Then, add the ext-base.js, ext-all.js and ext-core.js files into the public folder of you GWT project. (eg. in com.redflex.gwt.public.js)

Then, define these in your project's html file:
< !--include the Ext CSS-- >
< link rel="stylesheet" type="text/css" href="js/ext/ext-all.css" / >

< !--include the Ext Core API-- >
< script type="text/javascript" src="js/ext/adapter/ext/ext-base.js" >< / script >


< !--include Ext -- >
< script type="text/javascript" src="js/ext/ext-all.js" > < / script >


This provides access to Ext's Javascript functions which resolves the " '$wnd.Ext.StatusBar' is null or not an object" error.


Make sure your paths to the ext-base.js, ext-all.js and ext-core.js files are correct into you < script > tags.

You can also copy all the other css files in the resources folder of Ext 2.0.2. This will allow your widgets to look like the ones shown in http://code.google.com/p/gwt-ext/

GWT-EXT Error: Scanning source for uses of the deprecated gwt.typeArgs javadoc annotation; please use Java parameterized types instead

So I downloaded GWT-EXT 2.0.6 and placed it in my Eclipse project's build path. Then I wrote some simple code:
MapPanel osm = new OpenStreetMap();

Then i placed this in my project's the gwt.xml file



When I ran this code in hosted mode, I got this error:

[DEBUG] Scanning source for uses of the deprecated gwt.typeArgs javadoc annotation; please use Java parameterized types instead
[DEBUG] Type com.gwtext.client.data.NodeModel
[TRACE] Field attributesAllowed
[WARN] Deprecated use of gwt.typeArgs for field attributesAllowed; Please use java.util.ArrayList as the field's type
[TRACE] Field children
[WARN] Deprecated use of gwt.typeArgs for field children; Please use java.util.ArrayList as the field's type
[TRACE] Field properties
[WARN] Deprecated use of gwt.typeArgs for field properties; Please use java.util.HashMap as the field's type

I have GWT 1.5.3.

I scanned forums which all said that the reason for this is because GWT 1.5 uses generics rather than the @gwt.typeArgs annotation. (eg: http://groups.google.com/group/Google-Web-Toolkit/browse_thread/thread/74037cd1bc9c85d0)

However, I did not use any annotations. Hence, this did not resolve my problem.

I then found out that this was a bug and it was fixed here:
http://code.google.com/p/gwt-ext/issues/detail?id=464

Download the jar file in the above URL. Use that jar file instead. You won't get the error anymore.

Version of Ext in use not compatible with GWT-Ext.

When I ran my GWT application in hosted mode, I kept getting the following error:

Version of Ext in use not compatible with GWT-Ext.
GWT-Ext only supports Ext v.2.0.2.


I was already using EXT 2.0.2 (from http://yogurtearl.com/ext-2.0.2.zip) on GWT-EXT 2.0.6 (http://gwt-ext.com/download/) and I put the ext-all.css, ext-all.js and ext-base.js files into a local js folder so that my html file could access them using this:

< ! --include the Ext CSS -- >
< link rel="stylesheet" type="text/css" href="js/ext/ext-all.css"/ >

< !--include the Ext Core API-- >
< script type="text/javascript" src="js/ext/adapter/ext/ext-base.js" >< / script >


< !--include Ext -- >
< script type="text/javascript" src="js/ext/ext-all.js" >< / script >


The way I solved this was to clear my IE browser cache. In Windows, the hosted mode uses IE.

Friday, May 1, 2009

Changing Subclipse's Username and Password

This blog provides indepth information on how to change your subclipse username and password:
http://shahjapan.blogspot.com/2008/08/how-to-clear-subeclipse-password-svn.html

Tuesday, April 21, 2009

Running an Inno Setup script using Ant

To use ant to run an .iss script, do this:
< target name="createExecutable" >
< exec dir="." executable="C:/Program Files/Inno Setup 5/Compil32.exe" >
< arg line="/cc 'Setup.iss'" / >
< / exec >
< / target >

Monday, April 20, 2009

Running Maven goals in Ant

To run maven goals using Ant, use this:

< target name="mavenCleanBuild" >

< exec dir="." executable="C:/Program Files/Apache Software Foundation/ApacheMaven2.0.8/bin/mvn.bat" >
< arg line="clean" / >
< arg line="install" / >
< / exec >

< / target >

Thursday, April 16, 2009

Bits for bites...


(Src: http://www.html4.com/mime/jpg/Will_code_HTML_for_food.jpg)

Creating and Calling a PL/SQL Function that sets the currval of a sequence

I came across this problem recently. I needed to update the current value of a sequence in existing databases. This is due to an erroneous current value set in the initial install.

My requirements were:
1. The patch should be able to detect whether or the current value of the sequence is the erroneous value. If it is, it should be updated to the new value.
2. Additions to the DB that incremented the current value should not be overriden with the new value.

Using the following function, this can be accomplished:

CREATE OR REPLACE FUNCTION schema_name.checkSeq() RETURNS integer AS
$BODY$
declare
v_CurrVal int4;
begin
select nextval('schema_name.sequence_name') into v_CurrVal;

IF v_CurrVal < 10 then
PERFORM setval('schema_name.sequence_name', 32, true);
v_CurrVal = 32;
RETURN v_CurrVal;
END IF;

PERFORM setval('redflex_express.seq_incidentproperties_id', v_CurrVal - 1, true);
v_CurrVal = v_CurrVal - 1;
RETURN v_CurrVal;

RETURN currval('redflex_express.seq_incidentproperties_id');
end;
$BODY$
LANGUAGE 'plpgsql' VOLATILE
COST 100;


This checks to gets the nextVal of the schema_name.sequence_name sequence and sets it to 32 if it is less than 10. Using currVal did not yield the required results. Hence, the nextVal is used and decremented by 1.

To call this function, use this:

select * from schema_name.checkSeq();

Wednesday, April 15, 2009

The Classic Problem of Java Ping

In previous versions of Java (before version 1.5), Java only supported the TCP and UDP protocols. It did not support ICMP protocol. Hence, there was no way of running the ping application from Java other than using native methods (JNI) or the Runtime.exec() to execute shell commands.

In Version 1.5, the InetAddress class had a method called isReachable() which tests whether the address is reachable (also see http://www.rgagnon.com/javadetails/java-0093.html for a good explanation):

try
{
InetAddress addr = InetAddress.getByName(hostName);
pingable = addr.isReachable(timeOut);
}
catch (Exception e)
{
System.out.println("Host " + hostName + " cannot be ping'ed. " +
"Please check to see if this computer is up and running.");
e.printStackTrace();
}

This method sends ICMP “echo request” packets and listens for ICMP “echo response” replies (essentially what the ping network tool does: http://en.wikipedia.org/wiki/Ping). However, as the API says, "firewalls and server configuration may block requests resulting in a unreachable status while some specific ports may be accessible". If this is the case, the method will then establish a TCP connection to Port 7 (which is an echo port on the server that echos whatever to write to that port). THis could also be blocked depending on your firewall settings. Worst comes to worst, you can still use the Runtime.exec() method to execute the ping shell command:

Boolean pingable;
try
{
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("ping.exe " + _hostNameOrIpAddress);

int exitVal = p.waitFor();
System.out.println("Process exitVal " + exitVal);
}
catch (Throwable t)
{
t.getMessage();
pingable = false;
}

This should do the trick. Make sure that if you use a _hostName, there is a mapping of that hostname and IP in your C:\Windows\system32\drivers\hosts file.

Wednesday, April 1, 2009

Setting Tomcat's JVM path in the registry

If you have, like me, installed Tomcat using its .exe installer (which displays a set of dialog boxes) rather than running the bin\service.bat install in a pre-configured Tomcat, there is a way to change Tomcat's JVM path.

As you all probably already know, Tomcat's GUI has a Java tab
Tomcat's JVM settings are stored in the registry. You can change the JVM path to the jvm.dll file in your newly installed JRE directory.

In my case, I needed to configure it using regedit. To do this, go to the following subkey in HKLM:
SOFTWARE\Apache Software Foundation\Procrun 2.0\Tomcat6\Parameters\Java\

Here, you can change the value data for the 'Jvm' value name.

Hope this helps!

Monday, March 30, 2009

Getting a machine's HostName from an IP Address

If you ever need to find the host name for an IP address, use this:


nbtstat -a address


That will give you the host name of an IP Address.

Directory Mapping in Tomcat 6 / 5.5

For those of u who have always wanted to map a directory and not a servlet in Tomcat, here is how to do it:
1. First in your web.xml - edit the < listings > tag as follows:
< init-param >
< param-name > listings < / param-name >
< param-value > true < / param-value >
< / init-param >


2. Then ensure that under the conf folder in your Tomcat directory, you have the following virtual directory (for Tomcat 6):
conf -> Catalina -> localhost

The above directories will already exist in Tocat 5.5, so all you need to do i carry on Step 3.

3. create a file called media.xml and save it in the localhost folder. Also, put the following content into this folder:

< !--
Context configuration file for the Tomcat Manager Web App
$Id: resources.xml 303123 2004-08-26 17:03:35z remm $
-- >

< Context path="/media" docBase="D:/Importer/Media" debug="0" privileged="true" >
< / Context >


With this media.xml file, if you type in http://localhost/media, it will get mapped to the D:/Importer/Media
Setting the < listings > tag causes the directory structure within D:/Importer/Media to be displayed.

JavascriptException with Gwt Maps 1.0.3 (using GWT 1.5.3)

I had a google MapWidget that has several marker overlays. When run
in hosted mode, these markers can be seen. However, once compiled into
Javascript, the following error is thrown in Firefox:

Error: [Exception... "'com.google.gwt.core.client.JavaScriptException:
(TypeError): a is null
fileName: http://maps.google.com/intl/en_ALL/mapfiles/150c/maps2.api/main.js
lineNumber: 532
stack: (null,"C:\\myIcon.png",[object Object])@http://maps.google.com/
intl/en_ALL/mapfiles/150c/maps2.api/main.js:532
("C:\\mtIcon.png")@http://maps.google.com/intl/en_ALL/mapfiles/150c/
maps2.api/main.js:1262
l2([object Object])@http://localhost:8080/MyProject/
ACADCA4DA4C0B465E1EA03342F4CF5CC.cache.html:726
C1([object Object],[object Object])@http://localhost:8080/MyProject/
ACADCA4DA4C0B465E1EA03342F4CF5CC.cache.html:710
mx([object Object],[object Object])@http://localhost:8080/MyProject/
ACADCA4DA4C0B465E1EA03342F4CF5CC.cache.html:154
px([object Object])@http://localhost:8080/MyProject/
ACADCA4DA4C0B465E1EA03342F4CF5CC.cache.html:156
([object Event])@http://localhost:8080/MyProject/
ACADCA4DA4C0B465E1EA03342F4CF5CC.cache.html:204
' when calling method: [nsIDOMEventListener::handleEvent]" nsresult:
"0x8057001c (NS_ERROR_XPC_JS_THREW_JS_OBJECT)" location: ""
data: no]

Hence, in both Firefox and IE, I couldn't seem to see the markers.

The reason for this is my use of absolute paths rather than relative paths. Ensure that the marker's icon is not on an external absolute URL. The markers I was using used an icon from C:\MyIcon.png. When this is deployed as a war file in Firefox and IE, the marker's icon src was null as it could not point to that URL in web mode. Solution: Ensure that the icon is
in the 'public' folder in your project. Eg. My images are in the 'public/images' folder and hence the icon's url is:
Icon icon = Icon.newInstance("images/MyIcon.png");

This will be visible in webmode when deployed using a war file.