Starting tomcat in debug mode – Linux

To start tomcat in debug mode, you need to set JPDA_TRANSPORT=dt_socket and JPDA_ADDRESS=9000 environment variable. To set these variable for a session only, open command prompt and run following commands

export CATALINA_HOME=/home/xyz/opt/tomcat
export JPDA_TRANSPORT=dt_socket
export JPDA_ADDRESS=9000

now start tomcat using following command

$ %CATALINA_HOME%/bin/catalina.sh jpda start

Setting this variable in your personal ~/.bashrc file has the advantage that it will always be set (for you, as a user) each time you log in or reboot the system. To do so, open ~/.bashrc in a text editor (or create the file if it doesn’t already exist) and insert the following line anywhere in the file

export CATALINA_HOME=/home/xyz/opt/tomcat
export JPDA_TRANSPORT=dt_socket
export JPDA_ADDRESS=9000

Save and close .bashrc.
You must logout and login again to make your change take effect. or source your .bashrc file to make your change take effect for the current session

$ source ~/.bashrc

Start tomcat6 (installed as a windows service) debug mode using eclipse remote debugging

I have spent around 5 hrs in google to know how to start tomcat6 (installed as a windows service) debug mode using eclipse remote debugging. Here are the steps.

1) Go to your catalina_home/bin.
2) You will find a file called tomcat6w.exe
3) double click on the file and a window will be opened.
4) In the opened window, go to “java” tab.You will find a “text area box” with label as “Java Options”

5) Add the following lines of the code at the the beginning of the text area. (I mean above the first -D…)

6) The below are the lines which you need to add.
-Xdebug -Xnoagent -Djava.compiler=NONE
-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005

7) Once you have added go to your eclipse and go to Debug–>Debug Configurations and create a new configuration for your project with the port as 5005 (as given above) and save it.

8) Now start your tomcat service. You’ll observe at the middle of starting of your service it will become very slow and not responsive. At that time start your already configured debugger. Now you see your server started successfully in debug mode. Remember that as i mention above, if when your service start up is slow and you have not started your eclipse debugger at that time, then your server won’t start and you’ll get some error.

I have spent a lot of time on this and so i am posting it so that, the people who sees this can continue their work from here easily….

Changing project type form general to java in eclipse

You have a project, which is basically a java project but during checking out the project or creating a new project, you didn’t selected type of project as java. You are stuck now as there is no option to change it to java type in eclipse UI.
You can change type of project by modifying.projectfile, located in the root directory of your project. Open .project file in any text editor and search for text “natures”. You’ll see an empty natures node.

<natures>
</natures>

Change this value to

<natures>
        <nature>org.eclipse.jdt.core.javanature</nature>
</natures>

See Servlet Spec 2.3, section 9.7.2

If you are getting warnings or errors related with Servlet Specification, version 2.3, section 9.7.2. It means that you are trying to override web container’s implementation classes from your applications classes.
Servlet specification 2.3 section 9.7.2 says this clearly

The classloader that a container uses to load a servlet in a WAR must allow the developer to load any resources contained in library JARs within the WAR following normal J2SE semantics using getResource. It must not allow theWAR to override J2SE or Java servlet API classes. It is further recommended that the loader not allow servlets in theWAR access to the web container’s implementation classes. It is recommended also that the application class loader be implemented so that classes and resources packaged within the WAR are loaded in preference to classes and resources residing in container-wide library JARs.

You can get away from these warnings by doing one of the following

1. server-api.jar is needed to compile an application and is not required in application war to run application as container already have this file. You must remove this file from WEB-INF/lib.

2. It can be because of other jar files that are bundled with container. e.g. j2ee.jar. You need to remove this file from your application’s lib directory.

In tomcat 6, $CATALINA_HOME/lib/ contains all the jar file which one should not override with those of application’s jar. tomcat 5 has these jars in $CATALINA_HOME/common/lib folder.

Running a process in background/foreground in linux

Run process in background
To run a command in background, you can use following command at command prompt

$ command &

Above command will make your process run in background. You need to make sure that process is finished before logging off. A process run by above command will be terminated as soon as you logout.
If you want to run a command in background even after you logout then you can use following command

$ nohup command &

nohup Run a command immune to hang ups, with output to a non-tty.
This command will run your process in background even if you logs out.

Making current process to run in background
You can make current process as a background process by issuing following commands.

$ [ctrl] + z
$ bg

You need to remember that the process is running in background, if you logs out then process will be terminated. You can disown to avoid killing the process after you close the terminal.

$ [ctrl] + z
$ bg
$ disown

Execute following command to run any process in background

$ bg process_id

You can find process id by issues ps command. It displays the list of currently running process and their process ids. Use ps aux to see all the process currently running on your system including from other users. Use top to get updated list of all the processes running currently.

Run a process in foreground

$ fg process_id

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.&lt;init&gt;(Pattern.java:1044)  encountered while splitting the string
using a delimiter which contains "["

NoClassDefFoundError: com/lowagie/text/DocumentException

I am get following exception.

Exception in thread "main" java.lang.NoClassDefFoundError: com/lowagie/text/DocumentException at net.sf.jasperreports.engine.JasperExportManager.exportReportToPdf(JasperExportManager.java:183) at net.sf.jasperreports.engine.JasperRunManager.runReportToPdf(JasperRunManager.java:305)  at Report.main(Report.java:45)

Help me asap.

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

java.util.ConCurrentModificationException

I tried to execute following code

ListIterator iterator = i.listIterator();
while(iterator.hasNext()) {
      System.out.println(iterator.next());
      Object obj=((Integer)iterator.next()).intValue();
      if(obj.equals(i.get(1))) {
           i.add(10);
      }
}

ant it throws following exception

Exception in thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)

More than one value of type [net.sf.jasperreports.engine.JRDataSource] found

I getting this exception while using a subreport in my jasper report and trying to pass a datasource for my subreport. Datasource for report is ‘datasource’ and datasource for subreport is ‘pardatasource’.

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: More than one value of type [net.sf.jasperreports.engine.JRDataSource] found
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:488)
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)
org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:275)
org.acegisecurity.ui.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:110)
org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:275)
com.ccclogic.core.filter.SessionTimeoutFilter.doFilter(SessionTimeoutFilter.java:127)

Next Page →

Technology