Blackberry Code Signing Hangs or Taking too long: Java 7 Issue

I am working on a blackberry app for OS 7. I had received keys from Blackberry and tried to sign my app using Eclipse Signature tool in Blackberry Eclipse plugin. But whenever I started signing the app, it signed some of the .cod files and then hanged. Even after waiting for hours, it kept showing “Receiving Response” for some .cod files, specially for RRT key file. After searching on net , I found that it was because of Java 7.

Though I have not referred Java 7 anywhere in Eclipse, it was using it when signing the code. because by default, my system path was set to Java 7, though Java 6 was also installed. I uninstalled Java 7 and then system was set to using Java 6 by default. After that, I signed the code and Wow! it was completed at the blink of the eye.

So Blackberry does not support Java 7 . You need to install Java 6 and set its path in System Environment variables so that code signing process works well.

PS: My system is on Windows 7, 64 bit. I am developing app using Blackberry JRE 5.0.0

Leave a comment

Difference in App and Web Server

Here is a very good article describing difference between application server and web server.

In another article, we can see what differentiates a full fledged Java EE application from a Java Web application.

Leave a comment

Method Overloading Ambiguity Problem

While going through an article about method overriding in OOP, I stumbled upon an ambiguity problem in overriding.

Here is the java code I wrote to test it:

public class OverLoadingTest {

static void testOverload(int i,double d) {
System.out.println("testOverload 1: int, double");
}

static void testOverload(double d,int i) {
System.out.println("testOverload 2: double,int");
}

public static void main(String[] args) {

testOverload(2,3.5);
testOverload(2.5,3);
testOverload(2,3); // Compilation error:  The method testOverload(int, double) is ambiguous for the type OverLoadingTest
  }
}

In the third call testOverload(2,3), there will be a compilation error, because:

First argument 2 is int, so java compiler will decide to call the method testOverload(int,double) because its first argument is int, so it is better match than .testOverload(double,int)

Second argument 3 is also int, so java compiler will decide to call the method testOverload(double,int) because its second argument is int, so it is better match than testOverload(int,double)

As compiler will not be able to decide the right overloaded version for call, compilation error occurs.

Happy Coding !

Leave a comment

Enable/Disable Speakers/Headphones in Windows 7

Today I accidentally disabled my Windows 7 laptop speakers. I was installing Skype and testing it with headphones. I was getting the voice from other side from the laptop speakers, not through headphones. So I went to Control Panel–> Sound and disabled the speakers. So now only headphones were enabled, so the voice started coming through headphones. But now how to enable the speakers back? Now in Control Panel–> Sound, I was not getting speakers anywhere, so there was no way to enable them. Then when I googled it and found this article.

The solution was simple. In Control Panel–> Sound, in Playlist tab, Right Click and check Show Disabled Devices. Then select the disabled device, speakers in this case, go to Properties and there you can enable them. Hope this helps.

Happy Listening !

 

enable speakers in windows 7

,

6 Comments

toLowerCase() is Locale Based

I have found this very interesting and informative post for Java developers. Many Java developers may not know that toLowerCase() and toUpperCase() methods of java.lang.String class are Locale based. Have a look here at javadocs for these methods.

toLowerCase() internally calls toLowerCase(Locale locale). Same is the case for toUpperCase().

Leave a comment

Reading rss feed using java (JSP Taglib)

We can read RSS (Real Simple Syndication) feeds and publish them on our web page using JSP(Java Server Pages) tag library. Sun provides a rss taglib that can be used in JSP. It is extremely simple to use. You just need to have basic knowledge of JSP and HTML.

So, how to do that? First you need to download the tag library file (rssutils.tld, rssutils.jar) from Sun’s website. Here is the url to download it. Put the files rssutils.tld and rssutils.jar in WEB-INF/lib directory of your web application.
After that, choose a blog or website who publishes its rss feeds to users, and get the url of its rss feed. In below example, I have used the rss feed url of this blog. Please find the attached file here and save it with .jsp extension (rssTest.jsp)
You can use tag also to read content in a loop.
For a small tutorial on rss taglib, go here

To know what RSS is, go here

,

2 Comments

enum as a class

Here I have compared enum with a class. In java, enums are like classes. What I have done here is, if we have to write a class that works like en enum, how can we do it. Though it is not of practicle use,what I have tried is to make the concept clear about enums.By comparing enum with a class, we can understand how enums work.

Here DayEnum is a enum and DayClass is class for that enum.

enum DayEnum {
SUNDAY, MONDAY
}

class DayClass {
String value;
public DayClass (String str)
{
value=str;
}

static DayClass SUNDAY, MONDAY;
static
{
SUNDAY = new DayClass("SUNDAY");
MONDAY = new DayClass("MONDAY");
}
}

public class Test
{
public static void main(String args[])
{
DayClass dayClass = DayClass.SUNDAY;
DayEnum dayEnum = DayEnum.SUNDAY;
System.out.print("dayClass "+dayClass.value+" \n dayEnum "+dayEnum);
}
}

Here SUNDAY, MONDAY are similar to static objects of Day if Day were a class.

We use enum as:
DayEnum day = DayEnum.SUNDAY;

enums are like constants. To simulate that, I have used value in DayClass. So every object of DayClass has an integer value associated with it.

Leave a comment

Java Good Practices

Here is an article on java good practices.Have a look!

1 Comment

Private Fields of Reference Type

Hi, today I stumbled upon a strange problem while wriring some java code.
There is a class with a private field of reference type, say an ArrayList. Now,what is the use of private fields? They are used so that they can not be modified from outside the class like public fields. General practice is to provide getter methods in the class to return the values of private fields.It goes fine with the primitive type variables and Strings(because Strings are immutable). But when the field is of reference type, we can still change its value.Have a look at the code:

import java.util.*;

class Aclass
{

private ArrayList list;
private String s= "My string";

public Aclass()
{
list= new ArrayList();
list.add("Hello");
}

public ArrayList getList()
{
return list;
}

public String getString()
{
return s;
}
}

public class TestPrivate
{

public static void main(String args[])
{
Aclass ob= new a();
ArrayList alist= ob.getList();
System.out.println(l2);
alist.add("Hi");
ArrayList anotherList= ob.getList();
System.out.println(anotherList);

String s2= ob.getString();
System.out.println(s2);
s2="changed";
String s3= ob.getString();
System.out.println(s3);
}
}

When you run this code,the output is:
[Hello]
[Hello, Hi]
My string
My string

You can see that content of list has been changed, but for string, they are not changed.So you can change the list object even if it is private.It is because the getList() method returns the reference to the same list object, so we can modify the list even if it is private.Now if you think that it is some loophole or bug of java,the you are wrong.It is just a simple basic concept of reference type variables.

Now, what should you do to avoid this thing. You have to modify the getList() method so that it returns the clone of list, not the actual list object.Here is the solution:

public ArrayList getList()
{
return (ArrayList) list.clone();
}

clone() will return the clone of object, not the actual list. Return type of clone() is Object, so we have to typecast it to ArrayList. Any modification done from the reference returned by this method will not change the actual one.For more information on clone() method,
go here.

, ,

1 Comment

Problem With clone() if Object Has a Reference Type Attribute

You might have used the clone() method of Object class to get a clone of an object.But have you ever faced a problem if your object has reference type attributes? Well, I have, and thats what I am going to discuss in this post.
A class can have two types of class level variables,primitive type (as int,float) and reference type(as List).Here is an example:

class AClass
{
int i; //primitive type
List alist; //reference type
}
For more on types, click here

Well, lets return on the original topic.When we make a clone of an object, the primitive type variables are copied in the new clone object, but for the reference type variables,only the reference is copied, not the original instance.For example, if we create the clone of above class’s object,there will be only one List object, and both object will have a reference to this.Here is what I tried:

public class ClassToClone {
ArrayList list;
public ClassToClone(ArrayList list)
{
this.list=list;
}

protected Object clone()
{
try{
return super.clone();
}
catch(CloneNotSupportedException e)
{
return null;
}
}
}

To test it:
public class TestClone {
public static void main(String args[])
{
ClassToClone c1,c2;
ArrayList arraylist= new ArrayList();
arraylist.add("Hello");
c1= new ClassToClone(arraylist);
c2 = (ClassToClone)c1.clone();
c1.list.add("Hi"); //change contents of the list of c1
System.out.print(c1.list.toString());
System.out.print(c2.list.toString()); // prints the list of both objects
}
}

When you run this program, you will see that on changing the list of c1, list of c2 is also changed and they both have the same content.
To avoid this problem, you have to explicitly clone the reference type variables. In ClassToClone, replace clone() method by this:

protected Object clone()
{
try{
ClassToClone aobj = (ClassToClone)super.clone();
aobj.s = (ArrayList)list.clone();
//explicit call to clone() for arraylist
return aobj;

}
catch(CloneNotSupportedException e)
{
return null;
}
}

Here we have explicitly cloned the arraylist.Now a new list will be created for clone object.Now changes done in c1.list do not reflect in c2.list.You can try this by yourself.But remember,clone() can be called only if the class implements Cloneable interface, otherwise we get CloneNotSupportedException.Here class ArrayList implements this interface.
Here and here is a great article to read on object cloning.

Leave a comment