Posts

Showing posts with the label java

java.io.Console broken in Eclipse

Today I discovered that java.io.Console doesn't work when running application inside Eclipse. I was writing a trivial application that accepts some configuration from console. On trying to run this in Eclipse I was getting NullPointerException.as the Console object was null. I used Eclipse Indigo(3.6) version. On Googling found that there is an open bug and is not fixed. Looks like nobody ever planned to fix it.See bug 122429 for detail. User XIMES commented a workaround solution in the bug. It is to use BufferedReader by passing System.in as the input stream. Also to write one can use PrintWriter by passing System.out as the output stream. Downside to this workaround is that it cannot mast passwords which can be done by java.io.Console. Alternatively one can also look at using java.util.Scanner. One issue I found with Scanner is that pressing enter key is not considered as input and had to enter some value other than carriage return key. Sample usage public static void m...

Suppressing JExcel warnings

JExcelAPI(Jxl) which is a Java library for parsing Excel files does not have any configuration to use a custom logger at deployment time. JExcel decides logger class at build time and to use any other logger like Log4J, one must build Jxl jar file after configuring JExcel build.properties to use a custom logger. This is not a practical solution for many as they have to go through the hassles of building a custom jar with every new release of JExcel. It is common that parsing an Excel file will have many warnings and one will not be interested in seeing those. Also this invokes curiousity of testers and they will create bug for the warning. One such warning is Warning:  Cannot read name ranges for GROUP5 - setting to empty To suppress such warnings, JExcel provides a system property jxl.nowarnings which can be set to disable warnings. This can be passed as a startup argument to JVM or can be set in a class. To pass it as argument to JVM add -Djxl.nowarnings=true This wil...

Generating Oracle trace files in a web application

Using Oracle trace files is the best way to investigate random performance issues. In a real world web application, it is not practical to enable database trace for entire database as it will result in performance degradation and generates huge trace files. Also it is difficult to dig out useful information from a large file. So the most efficient way is to enable trace for particular connections. Web applications use connection pools and there is no guarantee on which connection is used to serve each request. This makes it difficult to turn on trace for a particular connection. Here comes the Oracle client identifier (CLIENT_IDENTIFIER) to rescue. What is client identifier and how to set it?  Client identifier is a token which can be set by the application. This can be reset/changed by the client application. This identifier can be used to uniquely identify users associated with a connection. I suggest every serious application to set this identifier as this will come handy s...

Oracle JDBC : Protocol violation

We have batch process running on Solaris servers. We have been using this for several months and one day we started getting JDBC Protocol violation exception from these jobs. This errors happens after several successful database operations and each time it occurs with a different operation. Fix is to use 64 bit VM. To use 64 bit VM pass argument -d64 as command line parameter to Java. We are using Oracle 11g R3, Solaris 5.10 and JDK 1.6.0_21-b06. java -d64 test.Main This is the stack trace. java.sql.SQLException: Protocol violation ----------- ----------- Caused by: java.sql.SQLException: Protocol violation         at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:459)         at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:191)         at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:523)         at oracle.jdbc.driver.T4...

Left shift(<<) and right shift(>>) operators

This is a quick answer for what a left shift( >) operators do in Java. A left shift operator returns a value that is obtained by multiplying the left hand operand with 2 raised to the power of right hand operand. eg. 5 > 2 = 5 / (2 raised to the power 2) = 2. There are several factors that decides the behaviour of the operation. One is the sign of the integer.

Difference between servlets and JSP

Many people think that difference between JSP and servlet is that a JSP cannot be mapped to some other URL and JSP's don't have a life cycle mechanism. That is not correct. It is possible to map any URL to a JSP file. The configuration is almost the same except that instead of element element is used. Also a JSP can define init and destroy methods. The JSP should implement jspInit() and jspDestroy() methods as jsp declarations. It is possible to specify element for a JSP file and load the JSP file on container start up. Then what is the difference between JSP and servlet ? Some differences I can point out are 1 , JSP can contain HTML mark up same as in a pure HTML file where as in a servlet it needs to be put inside Java code. HTML editors can work with JSP files. 2 , JSP has a scripting and expression language. 3 , A servlet has access to underlying ServletOutputStream and can send binary data to the client. A JSP gets a java.io.Writer object only and it is not suitabl...

Methods for generating random data for testing

Very often for testing purpose huge amounts of data is required. Instead of using dictionary words it is better to generate random words and numbers. Here is a class with methods that generate random random String data. import java.util.Random; public class RandomStringGeneratorUtil { private final static char[] alphabets = { 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' , 'n' , 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z' }; /** * Use this method to generate strings of specified length. * * @param rng * @param chars */ public static void randomString(Random rng, char[] chars) { for(int i=0; i chars[i] = alphabets...