discussing some technical aspects covering tools, frameworks, technologies etc (the areas where i m involved and getting experienced) - feel free to discuss!

Showing posts with label Tips. Show all posts
Showing posts with label Tips. Show all posts

Thursday, November 06, 2008

java.io.UnsupportedEncodingException: Cp850

Wow.. A nice exception with which I learnt and found a new thing yesterday.

As being asked my one of my colleagues as she was facing this error soon after she launched the application in IE (the browser, Internet Explorer). It was a Struts based application and We digged around all the possibilities by looking at the flow to find out where the encoding has been specified.

I was sure that it has to deal with the encoding settings at the client side (obviously thats what the meaningful Exception says! lol..). The research continued with applicationResources.properties of struts, other application specific properties file, web.xml (to specify the encoding if any) etc., But no luck!!

The entire stack trace is below (fyi).

java.io.UnsupportedEncodingException: Cp850
sun.io.Converters.getConverterClass(Unknown Source)
sun.io.Converters.newConverter(Unknown Source)
sun.io.ByteToCharConverter.getConverter(Unknown Source)
java.lang.StringCoding.decode(Unknown Source)
java.lang.String.(Unknown Source)
java.lang.String.(Unknown Source)
...... [here the application specific code goes -- PRIVACY and CONFIDENTIALITY :)]

Well, she found out the solution with the googling that the "Issue was with the browser (IE 7) " and it was a bug in it. She got the application up and running in IE 6 :) Cool.

Thought of posting it here so that it may be useful for others whoever faces this issue :).

Monday, September 08, 2008

Editing the shell script in Windows OS

Are you one among them who happened to edit the shell script in Windows OS? You may have various reasons to do that.

You should have got the script working very well in the appropriate linux (or any other flavour) box. But just before doing the final operation (be it checking it into CVS (version control system) or sending to the client waiting at onsite) you might have thought of giving a final/finishing touch like adding some documentation etc.,

Are you asking, "wats up?" or "what's a big deal.. huh?". Wait a minute. All is fine until you execute the script back in the linux box again.

Boooom.. The linux OS spits out an error message saying that "/bin/bash^M: bad interpreter: no such file or directory". What had happened?
Wondering..? Surprised? Shocked rather??

Well, here it goes. The way the Windows OS deals with the file saving option is quite different than that of Linux OS. Once you are done with the so called final/finishing touch and when you save the file, some control characters are getting added to the shellscript (.sh file). That's what the culprit is and hence the error when executing it again in linux. The Bash Shell is unable to deal with such special character ('Control M' -- is referred as "^M").

It is assumed that Bash is the default and widely used Shell. The shell being used and the actual error message might vary as per your system's default shell and the way it is customized.

What and Where is the rescue?

There has to be a way. Isn't it? Yes, of course. There are two options.

(1) You can open the file in Linux OS (using your favorite editor like
vi, emacs etc.,) and can delete such characters what you find at the end. I am not very sure of this as to what extent those characters are explicitly visible to the end user.

(2) But the other excellent alternative is to use the 'dos2unix' command wherein the usage goes as follows.

'dos2unix ' [eg., dos2unix myShellScript.sh]

This do2unix command converts the file in the DOS (for Microsoft environment) to the Unix properitary format. While doing so, it removes the special/control characters and it ultimately cleans the file to be used in Linux versions :).

Note/Word of Caution: Never edit the Linux related files in non-linux OS (Windows)! :)

Hope this helps to solve the tactics and saves you from the critical moment!

Sunday, March 09, 2008

to set the color of the windows cmd prompt


Basically I  too fall in that 'customizing-freak' category to customize certain features of the OS apart from the specific applications we work on.

One among them is, to have the preferred font, foreground and background colors in the Comman Prompt and with a shortcut to the command prompt on the Quick Launch bar.

Just today when i was searching for a help command for another utility (services.msc), got to know about this favorite-and-regular routine of mine. 

Windows has a command utility named Color with an argument to set the foreground and background colors for the window being opened.

I bet no one can beat the Microsoft's help/documentation. so for  your info, just copy-pasting the information available in the help!

Command : color help

Sets the default console foreground and background colors.

COLOR [attr]

  attr        Specifies color attribute of console output

Color attributes are specified by TWO hex digits -- the first
corresponds to the background; the second the foreground.  Each digit
can be any of the following values:

    0 = Black       8 = Gray
    1 = Blue        9 = Light Blue
    2 = Green       A = Light Green
    3 = Aqua        B = Light Aqua
    4 = Red         C = Light Red
    5 = Purple      D = Light Purple
    6 = Yellow      E = Light Yellow
    7 = White       F = Bright White

If no argument is given, this command restores the color to what it was
when CMD.EXE started.  This value either comes from the current console
window, the /T command line switch or from the DefaultColor registry
value.

The COLOR command sets ERRORLEVEL to 1 if an attempt is made to execute
the COLOR command with a foreground and background color that are the
same.

Example: "COLOR fc" produces light red on bright white.

My preference goes as : "Color 0a" :P

Note: It just sets the colour for the current window being opened! :)

Another Info about the 'DefaultColor' registry value: it is present in the HKEY_USERS/.DEFAULT/Software/Microsoft/CommandProcessor directory in 'regedit' (Registry Editor).

Monday, January 21, 2008

Getters and Setters for better encapsulation

This is an interesting topic in the beginner's level. Well, at the end of this discussion you would have an answer for these questions.


  1. What are Getters and Setters?

  2. What way they are beneficial?

  3. Are they the means of achieving Encapsulation?

  4. Summary of this encapsulation



Let's take you have a Person object and whose size object is what you are trying to access or set.

The plain version of the class would look like as follows:



class Person
{



public int age;
public String name;




public static void main(String[] args)
{
Person obj1 = new Person();
obj1.age = 5;
obj1.name = "Raghavan";
System.out.println("obj1.age="+obj1.age);
System.out.println("obj1.name="+obj1.name);
}
}



The following program produces the output as:



obj1.age=5
obj1.name=Raghavan



By looking at the above code, you don't seem to get anything strange! Yes so do I :).

Let's have a different scenario on the same class.



class Person
{



public int age;
public String name;




public static void main(String[] args)
{
Person obj1 = new Person();
obj1.age = -5;
obj1.name = "";
System.out.println("obj1.age="+obj1.age);
System.out.println("obj1.name="+obj1.name);
}
}



Now did you see something strange?

If not, look at the two lines carefully.



obj1.age = -5;
obj1.name = "";



They are perfectly valid and legal to the compiler and runtime, they are syntactically correct! But logically?



  • Can any person have a negative age?

  • Can a person's name be empty?




Why these occurred? Because the variables were of public during declaration.

That's why they are allowed outside and any legal value is allowed in the program without any harm.

How we go ahead in preventing this scenario?

Before the setters/getters were in picture or came into existence, the variables were declared public and given outside access without any harms.

In that case,



class Person
{
public int age;




public static void main(String[] args)
{

Person myObj = new Person();
myObj.age = 5;
System.out.println("myObj.age : "+myObj.age);




Person myObj2 = new Person();
/* Check here! */
myObj.age = -2; //Ouch! a syntactically correct but logically incorrect value
System.out.println("myObj2.age : "+myObj2.age);
}
}



Just to have a better control on this value being accessed, there came a rescue in the form of Getters and Setters. As the name indicates, Getter is used to get/obtain a value and Setter is to set a value back. They are also called as Accessor and Mutators.

What if you allowed the access through a method and NOT directly?

For which you need to have two things.


  • First "protect" your variable from outside access. Means, declare them to be of private.

  • Then provide getter and setter for your private variable. As the variable is pricate, the getter and setters should be public.



Following the same, the code will initially look like



class Person
{
private int age;




/* Getter Method */




public int getAge() {
return this.age;
}




/* Setter Method */




public void setAge(int newAge) {
this.age = newAge;
}



public static void main(String[] args)
{
Person myObj = new Person();
//myObj.age = 5; // can't access directly!
myObj.setAge(5);
System.out.println("myObj.age : "+myObj.getAge());




Person myObj2 = new Person();
/* Check here! */
myObj.SetAge(-2); //Ouch! a syntactically correct but logically incorrect value
System.out.println("myObj2.age : "+myObj2.getAge());
}
}



But had you been exposing your code through this way, it would be very easy in a later point when you realize that setting a negative value is a sin! you should NOT allow that, you have an easy way to get rid of it.

Just modify/update the setter method as below :



...
public void setAge(int newAge) {



/* introducing a new check for Age */




if(newAge <=0){ /*definitely age should NOT be zero! */
System.err.println("Invalid age!");
return;
}




this.age = newAge;

}



This way, you really don't break the existing code. As such the users would still continue using your setAge() method to set an age.

Here dealing with the invalid values and the condition to confirm the same may definitely based on the user and requirement. You can either set a default value or throw an exception or just let the user know about invalid value and continue the program normally.

A Quick Summary of encapsulation goes:

Encapsulation reduces coupling between classes, allowing it to make changes to classes without having to change its clients (whoever invokes the methods of this class). This makes maintenance and extension of a software system much easier and less costly.

Remember that for most systems, much more effort is put into maintenance than into initial development, so proper management of code dependencies pays manyfold.




/* 2-arg Constructor */
public Person(int age,String name)
{
this.age = age;
this.name = name;
}



/*Overridden toString() for our Person class*/
public String toString()
{
return "[Person] : age="+this.age+", name="+this.name;
}

Monday, November 19, 2007

tasklist - cmd in windows similar to ps cmd in linux

The people who have worked in Unix and its flavours, would have definitely come across a command called 'ps' (which stands for 'process status'). As such the 'ps' command gives you the list of processes running and the pid (process id) etc.,

There is a command to achieve the same in Windows OS too. That is 'tasklist'. Just execute in the command prompt. Here it gives the "Image Name", "PID", "Session name", "Session#", "Mem Usage".

Image Name -> the name of the process running
PID -> as you know, the process id (unique id which the OS refers to)
Session name -> it shows 'Console' but have to check what it really means
Session # -> should be a session id and for console it gives '0'.
Mem usage -> the amount of memory each task/process occupies.


Here is the sample screenshot of the same.


It looks like the CUI version of the 'task manager' :)

NOTE: It may not work in all versions of Windows. As such i have tested in Windows XP. I believe it must definitely work in 2000 also. Not sure about the previous versions of 98,95 etc., will check and update later.

Wednesday, April 04, 2007

How to get the list of files in a directory and save it in a file

You may need to get the list of files inside a folder with the appropriate structures being maintained.

This tip will help you to get the complete list of filenames within a directory structure in Windows OS.

(1) Go to Command Prompt
(2) Navigate to any directory of your interest
(3) Type dir /b /s "Root-directory" > "filename.extension"

whereas

/b -> use bare format (no heading information or summary)
{summary is the date time info, size , file or directory attribute etc.}

/s -> displays file in specified directory and all subdirectories.

Root-directory -> is the directory from which you want the file list info
{if Root-directory is omitted it will process from the current directory}

> -> Redirection Operator or Symbol.
{the output of the command on its left would be sent to its right}

fileName.extension -> any file name with extension you want
{example. directory-structure.txt}

Make it Automated in a Batch File

You can even write a batch file with another line to open it in the
appropriate application.


dir /b /s "Root-directory" > "filename.extension"
notepad "filename.extension"
Save these two lines in a batchfile as ".bat" and run this batch file from command prompt. It would automatically save it in the file and gets opened in Notepad. You could use any other convenient applications to open as you wish.

Hope you may need not take screenshots for directory structure {if at all you had been doing so} ;-).

Friday, February 23, 2007

Tip to increase the efficiency while deleting huge volume of rows

This tip would be very much useful when you have enormous rows in a table and you are trying to delete/remove the rows using the usual, "DELETE FROM <tableName>;" statement.

You may ask whats the consequences of using the DELETE statement and thats what its purpose is. But, not just deletion, for that matter any DML statement will make use of something called TRANSACTION LOG which is nothing but the buffer/temporary storage where all the data would be kept until there is an explicit COMMIT or ROLLBACK.

Once a Commit is issued, the data kept in the Transaction Log is written into the database files in its own appropriate format (.dat for data files, .inx for index files in IBM DB2 database - which may vary in Oracle, MySQL, SQL Server etc..).

When a rollback is issued, the data in the temporary Transaction Log is removed and thereby no state change happens to the data files.

Moreover, its gonna be time consuming wherein the time is directly proportional to the dense of data.

When you deal with large number of data to be dealt with, be it inserting, updating or deleting, you might end up in something called, "The Transaction Log is FULL" while doing the operation. Most of us might not have encountered this situtation as we do just deal with 100s or max 1000s of data. People who are dealing with lakhs, millions of rows might have been aware of this. Say, who are in Data Warehousing, Business Objects etc.

When we encountered the situation, one of my teammates in my client place (Yadati Ravishankar) had come up with a query what he found out in googling. This query does the magic by just clearing up the table with very very less time compared to that of plain old DELETE statement.

Here you go...

"ALTER TABLE [<SchemaName>.]<tablename> ACTIVATE NOT LOGGED INITIALLY WITH EMPTY TABLE;"

This query does alter the table with empty rows but while doing the same it requests the db engine not to activate the Logging. Thereby it is able to get the request serviced within seconds!!

Try with huge data buddies..then only you can make out the difference!!

Good luck!

About Me

ஏதோ பிறந்தோம், ஏதோ வாழ்ந்தோம் என்றிருப்பதல்ல வாழ்க்கை! எப்படி வாழ்ந்தோம் என்பதும் ஒரு அங்கம். வாழக் கிடைத்த வாழ்க்கையில், ஒரு சிலருக்காவது வசந்தத்தின் முகவரியை அறிமுகம் செய்தோமேயானால் அதுவே வசீகரத்தின் வனப்பைக் கூட்டும்!