Send As SMS

Thursday, October 12, 2006

HowTo - GCJ: A native Java Compiler

MinGW
MinGW[1] allows you to create native Windows programs. In other words, you can make an exe out of a Java program. The files required for the GCC Java complier are: gcc-java-3.4.2-20040916-1.tar.gz, mingw-runtime-3.9.tar.gz, w32api-3.6.tar.gz, binutils-2.15.91-20040904-1.tar.gz, gcc-core-3.4.2-20040916-1.tar.gz,

These files can be downloaded from the MinGW download page[2] . Additionally, linking requres the libiconv[3] library file libiconv-1.9.1.bin.woe32.zip. This file can be downloaded from its SourceForge.net page[4].

Unzip all the above files in a folder, say D:/mingw. The file gcj.exe will be in D:/mingw/bin. You can add this to the system PATH environment variable.

Compiling a program:
gcj -c -g -O Hello.java

Creating an exe file:
gcj --main=Hello -o Hello Hello.o

This will create a file called Hello.exe

Consider the following program Hello.java
class Hello {
public static void main(String args[]) {
System.out.println("Hello, World!");
}
}


This is a 117 bytes file that will normally require a 15MB JRE from Sun to execute. On the other hand, by using GCJ, the same program compiled into a 3.32MB Hello.exe file.

micro-libgcj
This size can be reduced if we use the minimal micro-libgcj[5] runtime. Note that this runtime supports only the bare minimim features in Java. So, most advanced features including Reflection are not supported.

References
1. http://www.mingw.org/
2. http://www.mingw.org/download.shtml
3. http://www.gnu.org/software/libiconv/
4. http://sourceforge.net/project/showfiles.php?group_id=25167&package_id=51458
5. http://ulibgcj.sourceforge.net/

Sunday, December 04, 2005

Simple Java Annotation example

Here we will declare a simple annotation and use it.
* Create the annotation interface first
@Retention(RetentionPolicy.RUNTIME)
@Target (ElementType.FIELD)
@interface CatInfo {
String name();
int age();
double weight();
}
* Create a dummy class to use it: class Cat {}
* Annotate using
@CatInfo(name="Princess", age=2, weight=5.5)
static Cat cat = new Cat();
* Read the annotations using
public CatAnnotator(Cat cat) 
throws SecurityException, NoSuchFieldException {
Field field = this.getClass().getDeclaredField("cat");
if( field.isAnnotationPresent(CatInfo.class) ) {
CatInfo catInfo =
(CatInfo)field.getAnnotation(CatInfo.class);
System.out.println(
catInfo.name()+", "+catInfo.age()+", "+catInfo.weight() );
}
}
* This is the complete source code CatAnnotator.java

Monday, November 14, 2005

Spring Framework (Part 4) - Forms and Validations

In this part we will accept user data from a form, validate it and process it. We continue with Spring Framework (Part 3) - JUnit via XML.
* Copy spring-framework-1.2/dist/spring.tld to the springapp/war/WEB-INF directory
* Make an entry for spring.tld in web.xml
* Modify springapp/src/bus/ProductManager.java to add a new method increasePrice.
* Create springapp/war/WEB-INF/jsp/priceIncrease.jsp
* Create springapp/src/bus/PriceIncrease.java
* Create springapp/src/bus/PriceIncreaseValidator.java
* Create springapp/src/web/PriceIncreaseFormController.java
* Modify springapp/war/WEB-INF/springapp-servlet.xml to define the new form and controller
* Modify springapp/war/WEB-INF/classes/messages.properties to add some messages
* Modify springapp/war/WEB-INF/jsp/hello.jsp to add a url link for price increase
* Compile, deploy and reload the application

References:
Spring Framework (Part 3) - JUnit via XML
http://www.springframework.org/
http://www.springframework.org/docs/MVC-step-by-step/Spring-MVC-step-by-step-Part-3.html

Friday, November 11, 2005

Spring Framework (Part 3) - JUnit via XML

Here we will unit test our spring application from Spring Framework (Part 2) - Display a list of objects. An XML will contain the test data.
* Copy junit.jar to ~\apache-ant-1.6.2\lib
* Modify springapp/build.xml so that it will test all Test*.* classes in the build directory.
* Create springapp/src/tests/TestSpringappController.java
* Create springapp/src/tests/WEB-INF/test-springapp-servlet.xml
* Run 'ant junit' in your project directory which has build.xml

References:
Spring Framework (Part 2) - Display a list of objects
http://www.springframework.org/
http://www.springframework.org/docs/MVC-step-by-step/Spring-MVC-step-by-step-Part-3.html

Wednesday, November 09, 2005

Spring Framework (Part 2) - Display a list of objects

Here I modify Spring Framework (Part 1) - Hello Spring to display a list of objects.
* Create springapp/src/bus/Product.java and springapp/src/bus/ProductManager.java
*Copy spring-framework-1.2.1/lib/j2ee/jstl.jar, spring-framework-1.2.1/lib/jakarta-taglibs/standard.jar into springapp/war/WEB-INF/lib
* Update springapp/war/WEB-INF/springapp-servlet.xml
* Update >springapp/src/SpringappController.java
* Create springapp/war/WEB-INF/jsp/include.jsp
* Update and move hello.jsp to springapp/war/WEB-INF/jsp/
* Update springapp/war/index.jsp
* Execute ant on the updated build.xml
* Fire your browser to http://localhost:8080/springapp/.
* You should now see the products listed in springapp-servlet.xml
References:
Spring Framework (Part 1) - Hello Spring
http://www.springframework.org/
http://www.springframework.org/docs/MVC-step-by-step/Spring-MVC-step-by-step-Part-2.html

Thursday, October 06, 2005

Log4J 1.3 RollingFileAppender

In version 1.3, RollingFileAppender reads configuration from an XML file. We need to read it using JoranConfigurator. This is an sample configuration called traceLogger.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration>

<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/' debug="true">

<appender name="ROLL" class="org.apache.log4j.rolling.RollingFileAppender">
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern" value="/trace.%d{yyyy-MM-dd}.log"/>
</rollingPolicy>

<triggeringPolicy class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
<param name="MaxFileSize" value="100000"/>
</triggeringPolicy>

<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%c{1} - %m%n"/>
</layout>
</appender>

<root>
<appender-ref ref="ROLL"/>
</root>

</log4j:configuration>

This is how we could configure it using JoranConfigurator:
new JoranConfigurator().doConfigure("traceLogger.xml", LogManager.getLoggerRepository());

Thursday, August 04, 2005

Fixed header in a datagrid

This piece of CSS will let you scroll the contents of you table while keeping the headers fixed.
<style type="text/css" media="screen">
#container{ border: solid 1px black;width: 50%; height:150px; overflow: auto; }
.noScroll { position:relative; top:expression(this.offsetParent.scrollTop);
background-color:white; font-family: Arial, Helvetica, sans-serif; }
</style>

<div id="container">
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%">
<thead>
<tr class="noScroll">
<TH>...</TH>
</tr>
</thead>
<tbody>
<TR><TD>...</TD></TR>
</tbody>
</TABLE>
</div>

Reference:http://codebetter.com/blogs/geoff.appleby/archive/2004/10/23/29486.aspx