Friday, May 13, 2011

Dynamic code generation in java

Following code helps to generate a particular java class dynamically from our code.

First create an Interface called "Base" which is as follows,

public interface Base{
           public void run();

}

Next step is creating the implementation class of the Base interface from our code which is named as "C".

import java.io.*;

public class Invoker {

  public static void main(String[] argv) throws Exception {

         String code = "public class C implements Base {\n"
                         + "   public void run() {\n"
                         + "      System.out.println(\"I am the new class\");\n"    
                         + "   }}";

         createClass(code,"C.java");
         Class classB = Class.forName("C");
         Base b = (Base)classB.newInstance();         
         b.run();
  }

public static void createClass(String code,String file) throws Exception {
               OutputStream os =
                         new FileOutputStream(new File(file));
               os.write(code.getBytes());
               os.close();
               Process p = Runtime.getRuntime().
               exec("javac -classpath . "+file);
               p.waitFor();
  }
}








Wednesday, December 1, 2010

Recovering accidentaly removed PATH environment variable in ubuntu

By using following instructions you can overcome if you accidentally removed PATH environment variable from /etc/bash.bashrc file

first step:

Open a terminal and type following command

export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"

second step:

Remove accidentally added thing from the bash.bashrc file.

Sunday, August 29, 2010

Struts 2 Mapping a bulk of entries in a JSP to an array in an Action Class

To pass selected multiple entries which are in a JSP to the action in order to process all of them rather than processing one by one following sample codes can be used.

In the JSP code following has to be kept. Here it is having only the required fields to handle the this thing. Additional stuff has to be added by yourselves :
fileldValue : value in this will be mapped to a particular index in a "bulk" array which is in the action class here "key" is a long value in a request.

<s:checkbox name="bulk" value="%{#bulk}" fieldValue="{#key}"/>

In the action class there should have an array to map the selected entries in the JSP.

public class BulkHandler extends ActionSupport {

private long[] bulk;

public long[] getBulk() {
        return bulk;
    }

public void setBulk(long[] bulk) {
        this.bulk = bulk;
    }

/**
* @return String which desides the next page
*/
public String ProcessBulk(){

/* here you can process the bulk array which is having the long parameter values of every entries  which were selected and sent to the action from the jsp */

   return  SUCCESS;
                    }

}

Thursday, August 20, 2009

Crazy Microsoft

This is just a simple article. Here I am trying to give some kind of bugs which are not already being solved by the Microsoft developers. It is they are pretty cool. Just try them out by yourself.

Unbelievable.........

An Indian discovered that nobody can create a folder anywhere on the computer with the name of "CON". This is something pretty cool ...and unbelievable... At Microsoft the whole Team, couldn't answer why this happened!

HURRY UP TRY IT OUT NOW ,IT WILL NOT CREATE " CON " FOLDER

Magic goes here........

1. Open the note pad
2. Simply type "Bush hid the facts" without quotes
3. Save it as what ever you wish
4. Just close it and re-open it

Hope you enjoyed with the article.

Wednesday, August 19, 2009

Shell Scripting

Here I am trying to give kind of a basic idea for people who are interested in shell scripting. Today in computer world Linux is the most famous topic therefor I thought of writing something which is very helpful for Linux users.

What do you need to write Shell Scripts?
Simply you need only the Linux OS and it's bash shell (Available with almost all Linux Distributions. By default bash is default shell for Red Hat Linux Distribution).

What is Linux Shell?
Computer understand the language of 0's and 1's and this is called binary language. Shell is a user program which helps user to interact with the kernel. Simply it takes user's commands in English and if it is a valid command it is passed to the kernel. Commands which were mentioned above are Linux Commands.

What is a Shell Script?
Shell script is a series of commands which are written in a plain text file. Shell script something similar to MS-DOS batch file but it is more powerful than the MS-DOS batch file.

How to write your first Shell Script?
1. Use any editor like 'vim' to write the shell script.
2. After writing the script set the execute permit ion to your
shell script as follows,

chmod 755 your_shell_script_name

Note: This will set read write execute (7) permit ion to owner and for group and other permition is read and execute(5).

3. Here we go...Now the script can be run by using one of following three ways,

Assume your shell script name is "myFirstScript"
1)./myFirstScript
2)bash myFirstScript
3)sh myFirstScript

following are some examples for shell scripts.
example 1

prints "Hello World" on the screen.
--------------------
clear
echo "Hello World"
--------------------
example 2
Script to print user information who currently login,current date& time
--------------------
clear
echo "Hello $USER"
echo "Today is ";date
echo "Number of user login :";who | wc -l
echo "Calender"
cal
exit 0
#end of script
---------------------
example 3
This read your name from the key board and print it.
---------------------
echo "Your name please :"
read name
echo "Hello $name, lets be friend!"
---------------------

If else in Shell Scripting
if condition
then
condition is zero (true - 0)
execute all commands up to else statement
else
if condition is not true then
execute all commands up to fi
fi
example 4
---------------------
if [ $# -eq 0 ]
then
echo "$0 : You must type one integer"
exit 1
fi
if test $1 -gt 0
then
echo "$1 is a positive number"
else
echo "$1 is a negative number"
fi
------------------------
Run the above script as follows,
./script_name integer_number

assuming sript name is "test"
./test 10

output is as follows,
10 is a positive number


Detailed explanation
First script checks whether command line argument is given or not, if not given then it print error message as "./test : You must type one integer". if statement checks whether number of argument ($#) passed to script is not equal (-eq) to 0, if we passed any argument to script then this if statement is false and if no command line argument is given then this if statement is true.

Those are few examples for you to get an idea what shell script is. Hope you would enjoy with note. Finally there are lot more about Shell Scripts. Here hoped to give you a brief introducion.

Sunday, August 16, 2009

Who is responsible for this?

It was a bright sunny day. I got up early in the morning with heaps of hopes. It was my 5th birth day. I thought my father was there to wish me but... it was hopeless he was not there in my room and even in my home. I searched for some thing under my pillow,under my bed and on top of my cup board, for my birth day gift. But it was also hopeless because I could not find anything. Even though it was a bright sunny day . I felt that I was alone. I could remember that my father told me i would bring you a bicycle for your birth day. I don't want a bicycle dad please come to visit us. After few days there was a ceremony in our home and there was a huge black box in our veranda. There were lot of people in our place lot of them ware crying. I could see my mother was also crying near that box. I did not want to cry but.... I had to cry just because others were crying. Then after two days that black box was taken away from my home to a place called cemetery. Since I could not understand what was really happening there I asked from my mother "mother what was going on" so she said me that "your dad left all of us. he never come Just say good bye! to your dad " but I wonder what she was telling. And I didn't say good bye! because I knew for sure If my dad was there definitely he would talk to me. He never wanted hide from me. After four years time I really could understood what was happened to my dad. It's the war who took my father away. It's the war who is responsible for thousand of lives. It's the war who is responsible for millions of refugees. It's the war who took my father away.
This is based on a real story. As Sri Lankans we were lucky to defeat the war. Today all of us breathing freely under one a flag but there are thousands of people who are still suffering because of cruel war. Let's stand against war.