javax.validation.ValidationException: HV000183: Unable to load ‘javax.el.ExpressionFactory’. Check that you have the EL dependencies on the classpath, or use ParameterMessageInterpolator instead

I got this exception when I executed my newly written junit test for a Spring DAO with Hibernate Validations.

After adding javax.el to pom, this is resolved.


<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.4</version>
</dependency>

 

Live internet radio stream extractor

radio.jpg

Friends,

This is to give you a code pack I wrote today. I was listening to one of the internet radios in Tamil. I want to store it on my hdd, the tools I searched dint helped me much. I want a simple tool, that saves my time. And It should be able to work in my absence. I wrote couple of classes, which I am going to test it now.

Why dont you test it youself 🙂
It doesnt require any installation. you need only three files.

mp3_stream_extractor.jar, The execution binary,
RUN.CMD, the invoking script
radio.properties, where you configure the radios

run the run.cmd, The audio is extracted to your home folder automatically.

Enjoy.

Download MP3 stream extractor

radio properties

java.io.UTFDataFormatException: Invalid byte 1 of 1-byte UTF-8 sequence

Hi,

I have taken a short break! There are many more error messages, which are keep on accumulating behind the screens. But I am so lazy to put them in grassfield.

Anyway, today I got interrupted with an interesting exception

java.io.UTFDataFormatException: Invalid byte 1 of 1-byte UTF-8 sequence

The scenario was, I am trying to parse an xml string. I am taking the byte array from the xml string, and give that array as input to xml reader stream. I have used java.lang.String.getBytes() for this.

Unfortunately, I got a chinese (or any other funny) characters as a value of one node in the xml. Ooof. I got up with the above error. Later, I found that getBytes() method supports only the western encoding, not UTF-8. So by using java.lang.String.getBytes("UTF-8") method, we solved the issue! nice na!

Java Runtime exec can hang

Today I was trying to extract the windows eventlog entries in my win-xp box. I have used Eldump to do this. please see my earlier post about this tool http://blog.grassfield.org/2007/06/24/fetch-your-eventlog-entries-with-eldump/ . I have used runtime.exec() to execute this command and extracted the information from the process streams.

The program was extracting the log entries, but hang in between, and went for endless sleep; i.e, the process never ended. Later with some googling, I was adviced to read error stream first, and then process output stream. Also both the streams need to be read concurrently with two threads. This could solve my issue. Try then!

java.util.zip.ZipException: invalid CEN header (encrypted entry)

friends,This week started with some more problems :). Nothing to worry, problems only feed us! Otherwise, who will give us job!!! Today I was trying to unzip a zip file and read the contents inside the file, using java.util.zip.ZipFile. It was working fine till I increased the file size. The zip file is around 160MB, and the single file inside that zip file is 4.5GB contains raw text. I hit with the exception

java.util.zip.ZipException: invalid CEN header (encrypted entry)

Pathetic, I was totally helpless! Later I came to know that It is a open bug in java, which will not support above 3.2G!  I am in darkness. Latermy lead had pointed out that is due to java’s non-compliance to zip64, which is fixed in jdk1.6, 24th build. I need to get it installed and get rid of this issue!

Make fun with Text : java.util.Scanner


One of my friend came with the sun’s newsletter today morning. I was wondering about their demo on a new class, java.util.Scanner
See, parsing the string becomes very simple, like iterating a list.

Scanner accepts streams, file and other string input mechanisms and parses the string and give is the tokens. (It also allows the user to specify using which encoding the text has been built. goo news for localisation guyz like me). By default, whatever you have given, it is tokenized by having the default delimiter, space, See the following example,

import java.util.*;
import java.io.*;
public class test
{
public static void main(String [] args) throws FileNotFoundException
{
File f = new File(“test.java”);
Scanner scanner = new Scanner(f);
while (scanner.hasNext())
{
System.out.println(scanner.next());
}
scanner.close();
}
}

The output is

C:>java test
import
java.util.*;
import
java.io.*;
public
class
test
{
public
static
void
main(String
[]
args)
throws
FileNotFoundException
{
File
f
=
new
File(“test.java”);
Scanner
scanner
=
new
Scanner(f);
while
(scanner.hasNext())
{
System.out.println(scanner.next());
}
scanner.close();
}
}

funny, isnt it!!!

We can also change the delimiter, see the following example

import java.util.*;
import java.io.*;
public class test
{
public static void main(String [] args) throws FileNotFoundException
{
File f = new File(“test.java”);
Scanner scanner = new Scanner(f);
scanner.useDelimiter(“n”);
while (scanner.hasNext())
{
System.out.println(scanner.next());
}
scanner.close();
}
}

The output is same as that of above code, want to see that one also?

C:>java test
import java.util.*;
import java.io.*;
public class test
{
public static void main(String [] args) throws FileNotFoundException
{
File f = new File(“test.java”);
Scanner scanner = new Scanner(f);
scanner.useDelimiter(“n”);
while (scanner.hasNext())
{
System.out.println(scanner.next());
}
scanner.close();
}
}

Really good one! But I really miss the fun of using the streams 😦

How to display tamil text with java5 (applets and java swing based applications)

Java 1.3+ should display tamil without altering anything. but jre is
not configured for tamil officially till now. (Devanagari is added).
So you will see only boxes in applets or any other swing applications.
here is the procedure to see tamil characters.

It is considered you have Latha font installed.

Open java directory then go into ‘jre’ directory. Copy-Paste the
“fontconfig.properties.src” file, and rename it as
“fontconfig.properties”.This file then should be the one Java will use
by default. now open the “fontconfig.properties” file.

Find this line :

# Component Font Mappings

Then add :

allfonts.tamil=Latha

You must then find

sequence.allfonts=alphabetic

/default,dingbats,symbol

and add tamil: (am not sure, whether it is really to be done. it
worked without this entry)
sequence.allfonts=alphabetic/default,dingbats,symbol,tamil

add the line

sequence.allfonts.UTF-8.ta=alphabetic/1252,tamil,dingbats,symbol

restart if you are using any java applications. and restart the
browser (deleting temporary files may help if it dint reflect)

post a message, if anything dint work as expected.

-p.

serialVersionUID

I found eclipse is continuously warning me about ‘serialVersionUID’
whenever i tried to save a Java file. I was thinking that there will
be a relation between jdk version in which the class has been created.
please see http://www.javaworld.com/javaworld/jw-02-2006/jw-0227-control.html

Java serialisation provides a serialisation id serialVersionUID or
suid for version control. this informs the java serialisation
mechanism which class version is suitable with that serialised object.

ooff…


—————————————————
*Free* software is a matter of liberty not price. You should think of
“free” as in “free speech”.

How should I compare String objects

You can compare String objects in a variety of ways, and the results are often different. The correctness of your result depends largely on what type of comparison you need. Common comparison techniques include the following:

  • Compare with the == operator.
  • Compare with a String object’s equals method.
  • Compare with a String object’s compareTo method.
  • Compare with a Collator object.

Comparing with the == Operator

The == operator works on String object references. If two String variables point to the same object in memory, the comparison returns a true result. Otherwise, the comparison returns false, regardless whether the text has the same character values. The == operator does not compare actual char data. Without this clarification, you might be surprised that the following code snippet prints The strings are unequal.

String name1 = "Michèle";
String name2 = new String("Michèle");
if (name1 == name2) {
System.out.println("The strings are equal.");
} else {
System.out.println("The strings are unequal.");
}

The Java platform creates an internal pool for string literals and constants. String literals and constants that have the exact same char values and length will exist exactly once in the pool. Comparisons of String literals and constants with the same char values will always be equal.

Comparing with the equals Method

The equals method compares the actual char content of two strings. This method returns true when two String objects hold char data with the same values. This code sample prints The strings are equal.

String name1 = "Michèle";
String name2 = new String("Michèle");
if (name1.equals(name2) {
System.out.println("The strings are equal.");
} else {
System.out.println("The strings are unequal.");
}

Comparing with the compareTo Method

The compareTo method compares char values similarly to the equals method. Additionally, the method returns a negative integer if its own String object precedes the argument string. It returns zero if the strings are equal. It returns a positive integer if the object follows the argument string. The compareTo, method says that cat precedes hat. The most important information to understand about this comparison is that the method compares the char values literally. It determines that the value of ‘c’ in cat has a numeric value less than the ‘h’ in hat.

String w1 = "cat";
String w2 = "hat";
int comparison = w1.compareTo(w2);
if (comparison < 0) {
System.out.printf("%s < %sn", w1, w2);
} else {
System.out.printf("%s < %sn", w2, w1);
}

The above code sample demonstrates the behavior of the compareTo method and prints cat < hat. We expect that result, so where’s the weakness? Where’s the problem?

Producing Errors

A problem appears when you want to compare text as natural language, like you do when using a word dictionary. The String class doesn’t have the ability to compare text from a natural language perspective. Its equals and compareTo methods compare the individual char values in the string. If the char value at index n in name1 is the same as the char value at index n in name2 for all n in both strings, the equals method returns true.

Ask the same compareTo method to compare cat and Hat, and the method produces results that would confuse most students. Any second grader knows that cat still precedes Hat regardless of capitalization. However, the compareTo method will tell you Hat < cat. The method determines this because the uppercase letters precede lowercase letters in the Unicode character table. This is the same ordering that appears in the ASCII character tables as well. Clearly, this ordering is not always desirable when you want to present your application users with sorted text.

Another potential problem appears when trying to determine string equality. Text can have multiple internal representations. For example, the name “Michèle” contains the Unicode character sequence M i c h è l e. However, you can also use the sequence M i c h e ` l e. The second version of the name uses a “combining sequence” (‘e’ + ‘`’) to represent ‘è’. Graphical systems that understand Unicode will display these two representations so that they appear the same even though their internal character sequences are slightly different. A String object’s simplistic equals method says that these two strings have different text. They are not lexicographically equal, but they are definitely equal linguistically .

The following code snippet prints this: The strings are unequal. Neither the equals nor compareTo methods understand the linguistic equivalence of these strings.

String name1 = "Michèle";
String name2 = "Micheu0300le"; //U+0300 is the COMBINING GRAVE ACCENT
if (name1.equals(name2)) {
System.out.println("The strings are equal.");
} else {
System.out.println("The strings are unequal.");
}

If you’re trying to sort a list of names, the results of String’s compareTo method are almost certainly wrong. If you want to search for a name, again the equals method will subtly trip you up if your user enters combining sequences…or if your database normalizes data differently from how the user enters them. The point is that String’s simplistic comparisons are wrong whenever you are working with natural language sorting or searching. For these operations, you need something more powerful than simple char value comparisons.

Using a Collator

The java.text.Collator class provides natural language comparisons. Natural language comparisons depend upon locale-specific rules that determine the equality and ordering of characters in a particular writing system.

A Collator object understands that people expect “cat” to come before “Hat” in a dictionary. Using a collator comparison, the following code prints cat < Hat.

Collator collator = Collator.getInstance(new Locale("en", "US"));
int comparison = collator.compare("cat", "Hat");
if (comparison < 0) {
System.out.printf("%s < %sn", "cat", "Hat");
} else {
System.out.printf("%s < %sn", "Hat", "cat");
}

A collator knows that the character sequence M i c h è l e is equal to M i c h e ` l e in some situations, usually those in which natural language processing is important.

The following comparison uses a Collator object. It recognizes the combining sequence and evaluates the two strings as equal. It prints this: The strings are equal.

Collator collator = Collator.getInstance(Locale.US);
String name1 = "Michèle";
String name2 = "Micheu0300le";
int comparison = collator.compare(name1, name2);
if (comparison == 0) {
System.out.println("The strings are equal.");
} else {
System.out.println("The string are unequal.");
}

A Collator object can even understand several “levels” of character differences. For example, e and d are two different letters. Their difference is a “primary” difference. The letters e and è are different too, but the difference is a “secondary” one. Depending upon how you configure a Collator instance, you can consider the words “Michèle” and “Michele” to be equal. The following code will print The strings are equal.

Collator collator = Collator.getInstance(Locale.US);
collator.setStrength(Collator.PRIMARY);
int comparison = collator.compare("Michèle", "Michele");
if (comparison == 0) {
System.out.println("The strings are equal.");
} else {
System.out.println("The string are unequal.");
}

Summary

Consider when the equals method is more appropriate than the == operator. Also, when you need to order text, consider whether a Collator object’s natural language comparison is needed. After you consider the subtle differences among the various comparisons, you might discover that you’ve been using the wrong API in some places. Knowing the differences helps you make the right choices for your applications and customers.

More Information

Use the following resources to find more information about the material in this technical tip: