Archive

Archive for March 29th, 2007

Stack trace without line numbers — Unknown Source

March 29th, 2007 Arun Manivannan No comments

Sometimes, application servers don’t display the line numbers during printing the trace but instead just print the method name with the “(Unknown Source)” on it.

eg. something like this.

Exception in thread “main” java.lang.ArithmeticException
at TraceWithoutLineNumbers.main
(Unknown Source)

I wrote this small class to just help me learn why the line numbers get missed and Unknown Source comes up when the trace gets printed.

public class TraceWithoutLineNumbers {

public static void main(String[] args) {

System.out.println(”hello world”);

if (true){
throw new ArithmeticException();
}
System.out.println(”very true”);

}

}

For the above code, I gave a

javac TraceWithoutLineNumbers.java

and got

java TraceWithoutLineNumbers
hello world
Exception in thread “main” java.lang.ArithmeticException
at TraceWithoutLineNumbers.main(TraceWithoutLineNumbers.java:9)

Cool. Got the line number.

By default, the minimal debugging option is on. (Only line number and source file information is generated). Here is the list of all the other java compiler options.

So, I do a

javac -g:none TraceWithoutLineNumbers.java

java TraceWithoutLineNumbers
hello world
Exception in thread “main” java.lang.ArithmeticException
at TraceWithoutLineNumbers.main(Unknown Sourc
e)

There is nothing you could do on this if the class is built and run by the server ( read if you dont have control over the class’ compilation). However, if it is a standalone class, you can very well do a javac -g .

Or use this option.

-g:{keyword list}
Generate only some kinds of debugging information, specified by a comma separated list of keywords. Valid keywords are:
source
Source file debugging information
lines
Line number debugging information
vars
Local variable debugging information

Probably, the app server or the web server you are running your class on would have disabled the debugging option for performance purposes.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • De.lirio.us
  • description
  • Furl
  • Reddit
  • Slashdot
  • Spurl
  • StumbleUpon
  • Technorati
  • TwitThis

Why finalize method is protected?

March 29th, 2007 Arun Manivannan 1 comment

I used to wonder why all methods in the java.lang.Object is public and only finalize is protected.  Finalize is just a callback method which is supposed to be called by the JVM.  So, ideally, it should be private.  Here is a mind-blowing explanation of why finalize is protected?

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • De.lirio.us
  • description
  • Furl
  • Reddit
  • Slashdot
  • Spurl
  • StumbleUpon
  • Technorati
  • TwitThis
Categories: Uncategorized Tags: , ,

RequestURI URL Pattern Matching

March 29th, 2007 Arun Manivannan No comments

Back when i was preparing for the Web Component Developer exam, i had an idea of the requestURI and pattern mapping against the exact servlet. In simple words, the logic behind how the server maps the “<url-pattern>” tag and the <servlet-class> tag. I know that it is really a monstrous job, but then i wanted to try it using a simple array. Of course the code i wrote is buggy ( I myself found one big bug in there.) But for most cases, it works fine.

Here is my code. Download from here.
public class Navigation {

static String[] config= { “/test2″, “TestServlet2″, “/test”, “TestServlet” };
static String requestUri= “/test2/”;

public static void main(String[] args) {

System.out.println(”output” +new Navigation().getHandler(config, requestUri));
}

public String getHandler(String[] config, String requestUri)
{
System.out.println(”Request uri : “+requestUri);
int maxMatch=0; //store the length of the match that is the longest.
String currentMatch=null; //the most accurate match string

for (int i=0;i<config.length;i=i+2){
//looping through the array.
// Coz, you know that every odd String in the array is the Handler String,
//you can just skip them

System.out.println(”config[i] :”+config[i]);

if ((requestUri.startsWith(config[i]))){
if (config[i].length()>maxMatch){ //compare the length of the
//previously matched String, if any
maxMatch=config[i].length(); //if so, store it
System.out.println(”max match… “+config[i+1]);
currentMatch=config[i+1]; //Dont forget the String too
}

}

}

if (currentMatch!=null){
return currentMatch; //return it.
}

return “/”;
}

}

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • De.lirio.us
  • description
  • Furl
  • Reddit
  • Slashdot
  • Spurl
  • StumbleUpon
  • Technorati
  • TwitThis
Categories: url pattern matching Tags: ,