June 5th, 2009 by Christos Fragoulides

On April Google announced the support for Java on Google App Engine. I became aware of this last week and rushed to create an account and check it out. Since this is an ‘early look’ release, I had to wait until the guys at Google grant me the rights to use the service. Meanwhile I downloaded the SDK and started building a test application to check if the frameworks I mostly use are supported. My minimum requirements are JSF 1.2 with Facelets for templating (I also use ICEfaces but I can live without them). The results where positive while using the SDK on my computer but when I’ve uploaded the application on App Engine (my account was activated two days ago) I received a nasty error thrown during the initialization phase of JSF. Read the rest of this entry »

April 4th, 2008 by Christos Fragoulides

Starting a native application from your Java code is as simple as the following command:
Runtime.getRuntime().exec("notepad.exe");

Of course, this adds platform dependency to your code, and if you want your application to be portable it’s a good practice to avoid using the exec() method. But if it is necessary to initiate a native application for any reason, the way is easy and pretty straight-forward. The fact is that there’s always a case where executing a command of the operating system is the only way to achieve specific functionality.

In this case, independence can be approached if the command to be executed exist(with common or different syntax) in various operating systems. The first step is to find out the syntax of the command for every OS. Then within the code, you can use System.getProperty(“os.name”) to determine the OS that is being to host your Java application and finally use this information to execute the command using the corresponding syntax.

There is one more useful feature of the exec() method: it’s return value is an instance of the Process class, which can be used to get access to the actual process of the application initiated. Check out the javadoc of the Process class to find out more.

January 8th, 2008 by Christos Fragoulides

Logging is an important and pretty useful mechanism for every application. It can help developers to debug and improve their code or test it’s functionality. Apache has made a great work in this field with it’s tool named log4j. You can find more information about it here.

In this post I will provide a quick guide to help you get up and started with log4j. But first of all you’ll have to download the latest version. Once you do, follow the instructions below:

1. Add log4j’s jar(log4j-x.x.xx.jar where x.x.xx is the version of the release you’ve downloaded) file to your application’s classpath.

2. Create a file named “log4j.properties” and place it in the default package of your source code(the root folder where all your code resides). Copy the following lines in it:
# Log levels
# Uncomment the following line to enable full loggin for every class
#log4j.rootLogger=trace, stdout, R
log4j.logger.gr.xfrag=trace, stdout, R

# Console appender configuration
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

# Rolling File Appender
log4j.appender.R=org.apache.log4j.RollingFileAppender
# Path and file name to store the log file.
log4j.appender.R.File=./logs/applog.log
log4j.appender.R.MaxFileSize=500KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1
# Rolling File Appender layout
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d - %c - %p - %m%n
Read the rest of this entry »

October 2nd, 2007 by Christos Fragoulides

Over the past few months, I’ve been working on JavaServer Faces, making an effort to learn the framework and ultimately use it to create a web application. Currently I’m still in the learning process and the more I’m involved with JSF, the more I’m becoming a fan of it. The last challenge I’ve faced is not directly related with JSF, but has to do with Servlets in general.

When you declare a Servlet in your web application’s configuration file (that’s web.xml), you also have to declare a mapping for the Servlet to make the web container forward the requests to the Servlet. There’s two types of mappings you can choose from: extension mapping and prefix mapping. Extension mapping will be of the form “*.extension” and path prefix mapping will be “/path/*”. Here’s an example:

<servlet>
    </servlet><servlet -name>Faces Servlet</servlet>
    <servlet -class>javax.faces.webapp.FacesServlet</servlet>
    <load -on-startup>1</load>

<servlet -mapping>
    </servlet><servlet -name>Faces Servlet</servlet>
    <url -pattern>*.faces</url>

I prefer extension mapping for JSF, because this way I’m not restricted to have a certain path for my JSF-enabled pages. The challenge I was talking about previously, has to do with my requirement to go beyond the default mapping mechanism. I want to be able to invoke JSF pages from any path, without using an extension. For example, say there is a JSF page displaying user account information, that has to be invoked using the URL “/account/view.faces”. Wouldn’t it be nicer to have “/account/view” instead?

After a small research, I’ve found out that a Filter can do the job. All that have to be done is a check of the requested URL pattern and if it match the mapping criteria, forward the request to the FacesServlet. Here is how I’ve done it: Read the rest of this entry »

September 6th, 2007 by Christos Fragoulides

After spending hours and hours playing with HTML, CSS and image editors, I’ve managed to give JavaWords a unique, nice and clear layout. The process was not easy at all for me, as my knowledge on the field is limited. Well, at least it was before making the decision to create my own theme.

The good thing is that now I can say I have a clear image of what it takes to consider yourself a web-designer. The whole site is XHTML and CSS valid and cross-browser compatible. I assume the later relying on my tests on Firefox, Internet Explorer 6.0 and Opera 9.0. If you notice any odd behavior on your browser, please notify me and I’ll try to fix it. I really want everyone to be able to read the blog without problems.

The bad side is that I didn’t had any time to spend writing anything about Java… Well I’m pretty stressed up lately studying for the exams at the University, but I’ll try to steal some time and write a few posts.

August 13th, 2007 by Christos Fragoulides

Here is the concept: We want to extend the JLabel swing component and make it perform a simple fade effect each time it’s value changes. Using this kind of label can be helpful to build a user interface for an application having the need to notify the user for important state changes.
The following applet demonstrates the component:

A good example is an application which continuously tracks a number of important data and represents values on the screen, each one on a separate label. It will be easier for the user to notice a change if the labels perform some kind of animation in response to an update of their value.

It is obvious that to make the above happen, we must “inject” animation code inside JLabel. The task is to figure out a way to start an animation thread whenever the text value of the label is updated. Well you can say this one is easy, because there’s the setText() method which we can override like this:

public void setText(String text){
  super.setText(text);
  if (!text.equals(lastValue)){
    lastValue = text;
    animate();
  }
}

Alright, now that we have set up a mechanism to start the animation, we have to implement the animation procedure itself. The question is: What do we have to put inside animate() method?
When I was trying to find an answer to the above question, I was sure for one thing only: There must be a thread to carry on the animation, witch I had to initiate during the call of animate() method. So I decided to make the class implement the Runnable Interface, and put my core animation code inside the run() method. I ended up with the following body for animate():

private void animate() {
// Only animate if there's not another animation in progress
// and this Label is actually added to a parent Container, i.e.
// if it is visible.
  if(!animating && this.getParent()!=null){
    Thread t = new Thread(this);
    t.start();
  }
}

Now we have to deal with the animation procedure, which involves the manipulation of the paintComponent() method. But this is the most tricky part: Simply having a loop repeating a call to paintComponent() is not a solution, because it requires a Graphics object reference to the screen area where the label resides. This object is normally passed to the method by the Event Model and you actually don’t have control over the last. The right way to make paintComponent() get called with an appropriate Graphics object is to call the repaint() method of the Component. This will add a request to the event queue and eventually will lead to a call of paintComponent(). Read the rest of this entry »

July 25th, 2007 by Christos Fragoulides

Ok, after spending a lot of time while designing and tweaking the blog’s theme, I think I’ve come up to an acceptable result.

Finally I can enable search engine bots to crawl the site and make JavaWords available to everyone. Unfortunately(for the readers), I will be on vacation the next days and obviously there will not be anything of great interest to read here until my return…


Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries.