February 1, 2012 - Posted by vaibhav - 0 Comments
Lot of times you install some third party software by adding new
repositories in ubuntu but later you discover that it is broken.
Such broken packages would not let you update or upgrade
essentially breaking your entire update system.
#sudo apt-get upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
You might want to run 'apt-get -f install' to correct these.
The following packages have unmet dependencies:
fbreader : Depends: libzlcore0.12 but it is not installed
libzltext0.12 : Depends: libzlcore0.12 (= 0.12.10dfsg-4)
but it is not installed
libzlui-qt4 : Depends: libzlcore0.12 (= 0.12.10dfsg-4)
but it is not installed
E: Unmet dependencies. Try using -f.
This error might be with any package.
In above example, fbreader,libzltext0.12 and libzlui-qt4 are the
packages which brought this error.
At such times , you might want to remove these packages forcefully.
Solution
Run the following command
To remove fbreader Pakage
dpkg --remove --force-remove-reinstreq fbreader
You can replace fbreader by any package name which is giving the error.
September 9, 2011 - Posted by vaibhav - 0 Comments

from FasterJ
September 8, 2011 - Posted by vaibhav - 0 Comments
Why ? Because, if you return from finally clause; any uncaught exception from try block will be lost forever. Don’t believe it. Try below example:
public class IgnoredExceptionDemo {
public static void main(String[] args) {
try {
throwErrorInWrongManner();
System.out.println(“Something has gone wrong !! It shouldn’t have came here !!”);
} catch (RuntimeException e) {
System.out.println(“Voila!! Got an exception!!”);
}
}
public static void throwErrorInWrongManner() {
try {
//This exception is ignored in the code that calls it.
throw new RuntimeException();
} finally {
return;
}
}
}
Lesson learned : Never call return statement from finally block. Just try commenting return statement from finally block and see the functioning of code.
September 6, 2011 - Posted by vaibhav - 0 Comments
Recently I was tasked to research about data serialization layer that we can use in internally for communication between our own servers as well as with partner servers. I compared many libraries based on performance characteristics, IDL support, exception handling, code generation support in multiple languages, output formats, debugging ability , generated code readability, community support and adoption etc. Based on these following are the main candidates I finalized :
1. Google Protobuf
2. Thrift
3. Message Pack
4. WSDL
Out of these WSDL is very verbose and certainly is inferior if you think of performance. Message pack is nice with good community. However, there is no big success story about that which leaves us with Google Protobuf and Thrift.
Amongst Google Protobuf and Thrift , I decided to go with Thrift mostly because of below points :
1. IDL Support: Thrift and Protocol Buffer both has IDL Support. Code can be generated based on this IDL definition file. However, Thrift’s IDL is rich in features compared to Protocol Buffer. Protocol Buffer has one advantage here. It allows extending the entities once defined through extensions. Thrift doesn’t allow this.
2. Code generation support: Code generation support for Thrift is available inherently in majority languages. Protocol Buffer supports C++, Java and Python as its main code generation languages inherently. For other languages, you need to depend upon external plug-ins.
3. Exception Support: Thrift IDL has exception as a part of its design. Protocol Buffers does not have native Exception support. We need to find a workaround to do that.
4. Native RPC Support: Thrift has native RPC support for which Protocol Buffer needs external plugin.
5. Configurable Ouput formats: Thrift supports Binary as well as JSON as its output data format. It can help us in debugging applications better.
6. Performance: Protocol buffer has slightly better performance (Not more than 10%) from performance comparisons as it supports variable length data. Variable length data feature is in beta testing of Thrift currently.
7. Collection support: Thrift has built-in support for list , map and set.
8. Documentation / Examples: Protocol Buffer has much better documentation compared to Thrift.
9. Readability: Protocol Buffer code is much easier to read compared to Thrift.
Here is the detailed comparison between two:
|
Thrift
|
Protocol
Buffers |
|
Backers
|
Facebook and now Apache |
Google |
|
Bindings
|
C++, Java, Python, PHP, XSD, Ruby, C#, Perl, Objective C,
Erlang, Smalltalk, OCaml, and Haskell |
C++, Java, Python
(Perl, Ruby, and C# under discussion) |
|
Output
Formats |
Binary, JSON |
Binary |
|
Primitive
Types |
bool
byte
16/32/64-bit
integers
double
string
byte
sequence
map<t1,t2>
list<t>
set<t> |
bool
32/64-bit
integers
float
double
string
byte sequence
“repeated”
properties act like lists |
|
Enumerations
|
Yes |
Yes |
|
Constants
|
Yes |
No |
|
Composite
Type |
struct |
message |
|
Exception
Type |
Yes |
No |
|
Documentation
|
So-so |
Good |
|
License
|
Apache |
BSD-style |
|
Compiler
Language |
C++ |
C++ |
|
RPC
Interfaces |
Yes |
Yes |
|
RPC
Implementation |
Yes |
No |
|
Composite
Type Extensions |
No |
Yes |
December 15, 2010 - Posted by vaibhav - 0 Comments
I am an avid reader of hacker news. Today I came across an interesting post here.
This trick would be very helpful to simulate the behaviour of far away servers which would respond after certain latency.
This adds a RTT time to the localhost device. e.g. If we add a 100 milliseconds , it would make ping time of 200 milliseconds to localhost.
tc qdisc add dev lo root handle 1:0 netem delay 100msec
To restore it back to original use :
August 4, 2010 - Posted by vaibhav - 0 Comments
Yesterday , I had a very specific requirement.I wanted to write a JUnit 4 Test Suite which would have all the test classes added within it and to run only these test suites through Maven 2.
Let us write a simple Junit 4 Test suite.
package com.test;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith
(Suite.
class)
@SuiteClasses
({Test1.
class, Test2.
class})
public class TestSuite
{
@BeforeClass
public static void setUp
() {
System.
out.
println("Setting up from Suite");
}
@AfterClass
public static void tearDown
() {
System.
out.
println("Tearing down from Suite");
}
}
Your Test
class shall look something like
this:
<code lang
="java">
package com.test;
import org.junit.Test;
public class Test1
{
@Test
public void test1
() {
System.
out.
println("Test 1 : This is a test. Test Successful!!!");
}
}
package com.test;
import org.junit.Test;
public class Test2
{
@Test
public void test2
() {
System.
out.
println("Test 2 : This is a test. Test Successful!!!");
}
}
Run Class TestSuite as a Junit Test from eclipse , you would get output like this:
Setting up from Suite
Test 1 : This is a test. Test Successful!!!
Test 2 : This is a test. Test Successful!!!
Tearing down from Suite
But there is a problem if you just want to run Suite and not any individual test classes through maven test run (using mvn test).
There is currently no configuration setting which currently permits this. I invented a small hack here.
With Junit 4 , any class can be a test class. You just have to annotate test methods with annotation @org.junit.Test.
If , we follow a pattern here, it is very easy only to run suite here. End all your Test suite class names with Suite. e.g. IntegrationTestSuite
Make sure all your test class names end with Test. e.g. HelloWorldTest
In your surefire test plugin , exclude all classes ending with name Test.java and include all classes ending with Suite.java.
To do this , you have to add below code in your pom.xml.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<includes>
<include>**/*Suite.java</include>
</includes>
<excludes>
<exclude>**/*Test.java</exclude>
<!-- These are all helper classes with inner classes. You need to exclude them. -->
<exclude>**/Foo*.java</exclude>
</excludes>
</configuration>
</plugin>
Added Exclsions:
- What is Foo*.java doing here in the above code snippet? It is just added there to Ignore Anonymous Inner classes generated from Foo.java.So if you are using any inner classes, do not forget to add this exclusions.
- If you are using any helper class for tests which is placed in src/test/java folder, add @Ignore for this class.This is important as surefire plugin tries to run all the classes which are there in src/test/java assumming that all of them are Test Classes. After annotating such class, it shall look something like this:
import org.junit.Ignore
@Ignore
public class XmlUtil{
}
January 18, 2010 - Posted by vaibhav - 0 Comments
Came across this fantastic showcase of Vim Color Schemes.
There are around 428 schemes available.
Whether you are a newbie or a hardcore vim fan, go and try it. It is going to make your code reading very smooth and easy.
Here is a screenshot for you.

vim color schems
January 15, 2010 - Posted by vaibhav - 0 Comments
I am lazy bum. I hate to do work manually when there is an easier program that can do it for me.
I was very thrilled to see he entire website being translated to English from Chinese/Mandarin (I dunno what it was) when I was visiting Hongkong.
Google translate was the magic behind this.
I was just wondering if there is any api available. At that time, there wasn’t any api available.
Recently I found that Google has release Translation API in Javascript. But I wanted to use this api in my java code for which I need to use Rhino.
Finally Googling more gave me this.
This is a very lightweight , simple API.
All you need to get started is this.
import com.google.api.translate.Language;
import com.google.api.translate.Translate;
public class GoogleTranslationManager
{
//This is required by Google for tracking purposes.
private static final String mySiteUrl
= "http://www.test.com";
/**
* @param args
*/
public static void main
(String[] args
) {
String text
= "Hi People";
Language currentlang
= Language.
ENGLISH;
Language toTranslateLang
= Language.
FRENCH;
GoogleTranslationManager gtm
= new GoogleTranslationManager
();
System.
out.
println("Originial Text: {"+text
+"}");
String convertedText
= gtm.
translate(text, currentlang, toTranslateLang
);
System.
out.
println("Originial Text: {"+convertedText
+"}");
}
public String translate
(String text, Language currentLang,Language toTranslateLang
) {
String translatedText
= null;
Translate.
setHttpReferrer(mySiteUrl
);
try {
translatedText
= Translate.
execute(text, currentLang, toTranslateLang
);
} catch (Exception e
) {
e.
printStackTrace();
}
return translatedText
;
}
}
There is a very interesting program on this website for automatic translation of property based internalization-and-localization files.
It looks for files _en.properties and other files _.properties in its working directory and adds missing translations from English to all other property files.
You can get this piece of code here.
January 12, 2010 - Posted by vaibhav - 0 Comments
I recently shifted to a new project for which I was supposed to make build on my machine. We were using web services using JAX-WS and JAXB.
When I was compiling it using ANT, it threw errors like:
can not find symbol
[javac] symbol : class WebServiceFeature
[javac] location: package javax.xml.ws
[javac] import javax.xml.ws.WebServiceFeature;
cannot find symbol
[javac] symbol : class XmlSeeAlso
[javac] location: package javax.xml.bind.annotation
[javac] import javax.xml.bind.annotation.XmlSeeAlso;
What the heck, all required jars were present in the class path. But still classloaded was unable to load these classes.
The great google came to my help and I came across this bug .
As it is saying,
XmlSeeAlso,WebServiceFeature have been introduced in JAXB 2.1/JAXWS 2.1 respectively.
To override the JAXB/JAXWS 2.0 in Mustang with JAXB/JAXWS 2.1 for ant tasks like wsimport,xjc :
The solution is to copy $AS_HOME/lib/endorsed/webservices-api.jar to $JAVA_HOME/jre/lib/endorsed.
So that was it.It solved my problem. Sun introduced JAX-WS and JAXB in JDK 6. If you want to use JAX-WS 2.1 and JAXB 2.1 with JDK 6 , you need to override them using endorsed standard mechanism.
Question is “What is Endorsed Standard ?
An endorsed standard is a JavaTM API defined through a standards process other than the Java Community ProcessSM (JCPSM). Because endorsed standards are defined outside the JCP, it is anticipated that such standards may be revised between releases of the Java platform. In order to take advantage of new revisions to endorsed standards, developers and software vendors may use the Endorsed Standards Override Mechanism to provide newer versions of an endorsed standard than those included in the Java platform as released by Sun Microsystems.
How to fix this?
Just copy jaxb-api.jar, jaxws-api.jar to <java-home>/lib/endorsed Here <java-home> refers to the directory where the JDK 6 is installed.
If you do not want to copy this, you can set a system variable java.endorsed.dirs to JAX-WS and JAXB lib directory location where class loaded can look for endorsed libraries. Just pass “-Djava.endorsed.dirs=%JAXWS_HOME%/lib:%JAXB_HOME%/lib” as JVM argument and you are done.
This was quite an interesting and accidental discovery.
January 7, 2010 - Posted by vaibhav - 0 Comments
I am one the truest fans of Eclipse when it comes to IDE wars. One of the few complaints I had was that after upgrading the eclipse from my old version,
My already checked out projects from SVN were not getting detected at all after this upgrade.
I kept postponing to fix this problem because of Laziness and the great TortoiseSVN plugin.
I sorted out that finally when I could no longer use TortoiseSVN.
It seems like eclipse has changed its default SVN provider from Subclipse to Subversion. Because of some weird bug, it is not detecting already checked out projects.
I have googled a bit and found few different set of problems and solutions.
If you have project checked out by another Eclipse SVN plugin (for example Subclipse) and want to work with it using Subversive
1. You need to disconnect project first. Select project and execute Team > Disconnect. Select “Do not delete SVN metainformation” in disconnect dialog and SVN working copy will not be deleted. It give you possibility to use this working copy later with Subversive and avoid recheckout process.
Note that if this step will be skipped it causes problem if you will uninstall Eclipse SVN plugin which was used for project checkout.
2. Now you can connect project to repository using Subversive. Select project and execute Team > Share Project. Select Subversive SVN provider in Share Project wizard and at the end you will be prompted to choose the SVN location. You can keep repository settings defined in project or reconnect to another repository.
If you have project checkouted by another SVN client
In this case you should connect to the repository using Share Project operation as it was described at step 2 of previous chapter.