java.util.regex.PatternSyntaxException: Unclosed character class

Exception in thread "main" java.util.regex.PatternSyntaxException:
Unclosed character class near index 1 $[ ^ at java.util.regex.Pattern.error (Pattern.java:1503) at
java.util.regex.Pattern.clazz(Pattern.java:2016) at java.util.regex.Pattern.sequence(Pattern.java:1560)
at java.util.regex.Pattern.expr(Pattern.java:1520) at java.util.regex.Pattern.compile (Pattern.java:1288)
at java.util.regex.Pattern.<init>(Pattern.java:1044)  encountered while splitting the string
using a delimiter which contains "["

JSON communication between server and client Part1

JSON (JavaScript Object Notation) provides lightweight data exchange between server and client. It is human readable.
Sending data in JSON format from server to client:
There are APIs present for many languages (Java, ASP, Flex, C, C++ etc). With the help of these APIs, we can get JSON representation for data or objects. This data can be send to client just like some other data.
On the client side browser can handle this format and one can retrieve the information send from server.
I am writing code sample for spring framework

public void getJSONInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
    JSONObject result = new JSONObject();
    try {
        Object obj = new Object();
        // To get JSON data from obj
       JSONObject jsonObj = JSONObject.fromObject(obj);
       result.put("result", "success");
       result.put("jsonInfo", jsonObj);
   } catch (Exception e) {
       result.put("result", "error");
  } finally {
      // Write this jsonObj in HttpServletResponse
      response.getWriter().print(result);
      response.flushBuffer();
   }
}

While on the client side, I am using jquery to make AJAX request,

function getJSONInfo() {
    $.ajax({
       url: '../location?q=getJSONInfo',
       type: "POST",
       cache: false,
       dataType: "json",
       success: function(response){
           alert(response.jsonInfo);
       },
       error: function(msg){
       }
    });
}

As we can see from the example, an ajax request is made to get JSONInfo and result is displayed in popup window.

Sending data in JSON format from client to server will be covered in part2

javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS

I am getting exception while sending mail using JavaMail APIs. Mail properties are as

	mail.transport.protocol = smtp
	mail.smtp.host = mailHost
	mail.smtp.port = 25
	mail.smtp.auth = true

The exception I am getting while sending email

Exception in thread "main" javax.mail.SendFailedException: Sending failed;
        nested exception is:
	javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first m1sm949464nzf
 	at javax.mail.Transport.send0(Transport.java:219)
	at javax.mail.Transport.send(Transport.java:81)
	at example.SendMailUsingAuthentication.postMail(SendMailUsingAuthentication.java:77)
	at example.SendMailUsingAuthentication.main(SendMailUsingAuthentication.java:35)

Installing tomcat in ubuntu/linux

Download tomcat from http://tomcat.apache.org
Go to the directory where you have downloaded apache-tomcat-6.0.18.tar.gz

$ cd
$ tar xvzf apache-tomcat-6.0.14.tar.gz

Move tomcat to ~/opt directory

sudo mv apache-tomcat-6.0.18 ~/opt/tomcat

Now you can start tomcat by executing startup.sh script located in tomcat/bin directory.

<< Back to Setting up j2ee developer environment …

Installing eclipse in ubuntu/linux

Download eclipse from http://www.eclipse.org/downloads/
Make an opt directory in user’s home directory

$ mkdir ~/opt

Go to the directory where you have downloaded eclipse

$ tar -xvf eclipse-SDK-3.4.1-linux-gtk.tar.gz && mv eclipse ~/opt

Make a bin folder in user’s home directory

$ mkdir ~/bin

Next create an executable for Eclipe at ~/bin/eclipse

$ vi ~/bin/eclipse

Add the following lines in this file

export MOZILLA_FIVE_HOME="/usr/lib/mozilla/"
export ECLIPSE_HOME="$HOME/opt/eclipse"
$ECLIPSE_HOME/eclipse $*

Finally make script executable

$ chmod +x ~/bin/eclipse

You can now execute that file to start up Eclipse.

<< Back to Setting up j2ee developer environment ...

Installing JDK in ubuntu/linux

To install jdk6.0 type the following command in terminal window

$ sudo apt-get install sun-java6-jdk

Provide password for root. It will show you agreement. Select and wait for installation to be completed.
We should set JAVA_HOME environment variable after successful installation of jdk.

$ vi ~/.bashrc

Insert following line in above file

export JAVA_HOME=/usr/lib/jvm/java-6-sun

<< Back to Setting up j2ee developer environment ...

Setting up j2ee developer environment in ubuntu/linux

A java developer machine has following most frequent tools in his machine.
1.  Sun JDK 6.0
2.  Eclispe 3.4
3.  Tomcat 6.X
4.  MySql 5
5.  Apache 2

Currently above are the popular versions, please update version if a newer version is available. Also, I have tried to list down all the necessary links and commands so that you can save time in finding these links or commands.

if you find something missing, please post a comment.

Debugging vicious java.net.BindException (Windows)

java.net.BindException is thrown when JVM fails binding a socket to a local address and port.. Most common reason is that port is already used by some other application/service. Message like “Port already in use: 1098;” further confirms this.

This is how you can debug this exception:

  1. Check which service is binding the port by command “netstat –b”

This will list down all port and the application which binds the port like:

Proto Local Address Foreign Address State PID

TCP hddlntd100000:1090 test.test.cim:1026 ESTABLISHED 2248 [OUTLOOK.EXE]

  1. Now check for the port which is shown as already in use. Like if the message resulted by BindException is “Port already in use: 1098”, then 1098 is in use by other application.
  2. Identify the application running on the given port. Like Outlook is acquiring port 1090.
  3. Either close the application which is acquiring the port directly by exiting the application like in case of Outlook or go to task manager and do end task for the given application/service. If application/service closes successfully you are done, now retry whatever had caused BindException earlier.

What is singleton pattern? Write an example singleton class?

A singleton class is a class which has only one instance per application. Rather per JVM. This design pattern is extensively used when user needs exactly one instance. For example facade objects, state objects or for global variables (as it permits lazy allocation and initialization which may not happen in all languages).

To make a class singleton, its constructor is made private or protected so that it can not be instantiated. Instead, a getInstance () method is written to return a new instance of that class if it doesn’t exist or return the reference of that object if it already exists. If it’s a multithreaded environment make sure to synchronize the getInstance() menthod.
Conventional way of writing a singleton class is :

public class Singleton {
private static Singleton INSTANCE = null;
// to prohibit instantiation
private Singleton() {
}

public static Singleton getInstance() {
if (INSTANCE == null) {
// lazy initialisation
INSTANCE = new Singleton();
}
return INSTANCE;
}
}

You can synchronise on getInstance() method for a multi threaded environment.

Another thread-safe Java programming language lazy-loaded solution is:

public class Singleton {
private Singleton() {}

// lazy initialisation
private static class SingletonHolder {
private final static Singleton INSTANCE = new Singleton();
}

public static Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
}

Remember a singleton class is one instance per JVM. So if you have your application running in a cluster of servers. Every server will have its own JVM, therefore its own instance of singleton class. In this case, purpose of making a class singleton is not fulfilled. Hence this pattern should be used with care.

nested exception is java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel /HSSFCellStyle

I am getting following exception while exporting jasper report in xls format.

org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFCellStyle
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:899)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:793)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:476)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:431)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:265)
org.acegisecurity.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:107)
org.acegisecurity.intercept.web.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:72)

Next Page →

Technology