Pages Navigation Menu

Coding is much easier than you think

Create and Deploy Web Service and Web Service Client in Eclipse

Posted by on Mar 23, 2015 in Java, Web Service | 2 comments

Web-Services
 
In our previous tutorial, we got introduced to Web Services and the Concept associated with web service technology. In this article we shall learn to Create and Deploy Web Service and Web Service Client in Eclipse.
 
There are two ways to develop a web service namely top-down approach and bottom-up approach. To know about these approaches and in general about web service refer my Concept associated with web service article. In this article we will be using bottom-up development approach.
 

Environment Used

 
JDK 7 (Java SE 7)
Eclipse JUNO IDE
Apache Tomcat 7.x
 
Just for your information, Eclipse by default uses Apache Axis to implement the web service and it provides option to use our choice of web service engine. I decided to go with the default bundled Apache Axis.
 
Initially Install Eclipse IDE and configure apache tomcat in it, and Create a dynamic Web Project with the following configuration
 
creating-dynamic-web-project-eclipse
 
Now create a simple calculator java program as with the code below
 
File: Calculator.java

package com.webServices;

public class Calculator {
	public int add(int a, int b) {
		return (a + b);
	}

	public int subtract(int a, int b) {
		return (a - b);
	}

	public int multiply(int a, int b) {
		return (a * b);
	}

	public int divide(int a, int b) {
		return (a / b);
	}
}

 

Initial Project Structure

The initial project structure of application should be as shown below
 
Initial project structure
 
Now Right Click on file Calculator.java -> Web Services -> Create Web Service and Select options as mentioned in below diagram and Click finish.
 
Create-Web-Service-from-Java-Class-in-Eclipse
 
Here we are instructing Eclipse to generate a web service and its client. These steps will create a new dynamic web java project WebServicesClient .
 

Final project structure:

 
Final Project Structure
 
Both WebServices and WebServicesClient projects will be automatically deployed to server. Also, Eclipse automatically opens Web Service Test Client Window with URL: http://localhost:8089/WebServicesClient/sampleCalculatorProxy/TestClient.jsp?endpoint=http://localhost:8224/WebServices/services/Calculator
 
Now click on add(int,int), subtract(int,int), divide(int,int) ,multiply(int,int) on the Web Service Test Client Window and provide an input to check updated result.
 
WebService-Client-Invoke-Result-Page-in-Eclipse
 
That’s all on how to Create and Deploy Web Service and Web Service Client in Eclipse.
Happy learning :)

Read More

Java Heap Dump Analysis using Eclipse Memory Analyzer (MAT)

Posted by on Mar 31, 2014 in Core Java, Eclipse, Java, JVM, Software | 1 comment

In this article we will learn what a Java heap dump is and how to analyze a heap dumps generated through OutOfMemoryError using Memory Analyzer in Eclipse.

What is a heap dump?

A heap dump is a snapshot of memory at a given point in time. It contains information on the Java objects and classes in memory at the time the snapshot was taken.

Why would we want to read heap dump?

If your Java application crashes with an OutOfMemoryError it is possible to automatically get a heap dump to analyze. This view into the memory profile of the application at the time it crashed can help us to figure out what caused the error. This can help decide what to optimize in our code.

How to get a heap dump?

To generate heap dump we have to execute a JVM with the following parameters in eclipse

-XX:+HeapDumpOnOutOfMemoryError writes heap dump on first  OutOfMemoryError 

 

Here the heap dump will be generated in the “current directory” of the JVM by default. It can be explicitly redirected with

-XX:HeapDumpPath= for example -XX: HeapDumpPath=/disk/myFolder. 

GC_Heap Dump analysis_Run_Configuration

GC_HeapDump anaylysis_Run_Configuration_JVM

How to read a heap dump?

Heap dump will be in binary format so you don’t read the plain file. Instead use a tool like Memory Analyzer Tool.
Download MAT plugin from this location, and install it in your eclipse.

OOM Java program

 

Here the Java program below is used to trigger an OutOfMemoryError. This program is basically creating multiple String instances within a List data structure until the Java Heap depletion.

package com.simplecode.heap;

import java.util.ArrayList;
import java.util.List;

public class OOMHeapGenerator 
{
public static void main(String[] args) 
{
	System.out.println("JVM OutOfMemoryError Simulator");
	List leakingVariable = new ArrayList();
	
	try 
	{
		while (1 < 2) 
		{
		leakingVariable.add("OutOfMemoryError");
		}
	} 
	catch (Throwable exp) 
	{
	     if (exp instanceof java.lang.OutOfMemoryError) 
	     {
	     System.out.println("OutOfMemoryError triggered! " + "[" + exp + "]");
	     }
	     else 
	     {
	     System.out.println("Other Exception! " + "[" + exp + "]");
	     }
	}
	System.out.println("Simulator done!");
}
}

 

On running this program, when the JVM ran out of memory it created a heap dump file java_ pid1244.hprof.

GC_Heap Dump Analysis_Run_Java_Program_Exec

Press F5 in your project folder, so now the generated heap dump file will appear in eclipse.

Eclipse Heap Dump Memory Analyser

Load Heap Dump

Just double click on the heap dump file and select the Leak Suspects Report, and then this file will be loaded into MAT.

GC_Heap_Dump_analysis_Leak Suspect Report Using MAT
On clicking finish the following Screen is obtained

GC_Heap_Dump_analysis_Chart

Analyze Heap Dump

Clicking on the “See stacktrace” link will give you the exact line of code that was building up the huge List causing the application to crash.

GC_Heap_Dump_analysis _Stacktrace
Now we know where to go look in at the code to fix this bug. Hence by analysing the Heap Dump using the MAT we could easily identify our primary leaking Java class and data structure.

 

Read More

What’s New in JDK 8

Posted by on Mar 19, 2014 in Java | 0 comments

 
Oracle announced the general availability of Java 8 on March 18 2014. One of the features is complete removal of Permanent Generation (PermGen) space. The JDK 8 HotSpot JVM is now using native memory for the representation of class metadata and is called Metaspace”; similar to the Oracle JRockit and IBM JVM’sThe good news is that it means no more java.lang.OutOfMemoryError: PermGen problems and no need for you to tune and monitor this memory space anymore.

In Java 8, we will still need to worry about the class metadata memory footprint. Please also keep in mind that this new feature does not magically eliminate class and classloader memory leaks. You will need to track down these problems using a different approach and by learning the new naming convention.

  • Refer the article at Dzone.com for details about the PermGen successor: Metaspace.
  • JDK 8 Release Notes can be found here.  It covers – what’s new in JDK8, Compatibility info, known issues, troubleshooting guide etc.
  • Here is another article from techempower.com, which contains a summary of the developer faced changes introduced in java 8.
  • The motive for removing permanent generation is mentioned at openjdk.java.net
Read More

How To Change Default Port number of Tomcat Server?

Posted by on Feb 18, 2014 in Java, Tomcat | 0 comments

Red race track from an old Olympic stadium
 
Tomcat by default runs on port number 8080, but many times other Java application also uses 8080 and starting tomcat may result in java.net.BindException. In order to avoid this exception you can change default port of tomcat from 8080 to some other port
 
Do read
What is a Memory Leak and Garbage Collector in java?
Java Heap Dump Analysis using Eclipse Memory Analyzer (MAT)
How to find the JDK target version from a .class file?
 

Steps in changing the Tomcat Port

 
Go to the installation directory of Tomcat Server and then open folder named “conf” and locate the file server.xml and Search for the entry shown bellow:
 

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

 
Change the value of attribute port to your desire value and restart the Tomcat Server.
 

<Connector port="8089" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

 

Read More

Displaytag export option is not working- Solution

Posted by on Feb 18, 2014 in Java, Struts 1, Struts-2 | 3 comments

 
In this article we will learn to resolve the issue which occurs while exporting the file using display tag,

 
In order to enable export functionality in display tag, configure display tag in JSP as shown below.
 

<display:table name="studentList" pagesize="5" export ="true" requestURI="/displaytag.jsp">
<display:column property="rollNo" title="Roll No"/>
<display:column property="studentName" title="Student Name"/>
<display:column property="department" title="Department"/>
<display:column property="rank" title="Rank"/>
</display:table>

 
Here export=”true” option enable export functionality in displaytag.
 

Displaytag Export Issue

 

After enabling displaytag export functionality, though it was working in test program, export option on display tag for CSV, Excel, XML and PDF was not working on actual project.
 
** UPDATE: Struts 2 Complete tutorial now available here.
 
My Application throws the following exception and error message, when user clicks on export button :
org.displaytag.exception.BaseNestableJspTagException Exception: [.TableTag] Unable to reset response before returning exported data. You are not using an export filter. . . . configure the export filter.
 

Cause of Displaytag export problem:

 
This issue occurs when you have declared filter in web.xml
 

Solution :

 
To fix displaytag export issue the following steps are needed to be followed:
 
Add ResponseOverrideFilter filter as shown below.
 

<filter>
        <filter-name>ResponseOverrideFilter</filter-name>
        <filter-class>org.displaytag.filter.ResponseOverrideFilter</filter-class>
</filter>
<filter-mapping>
       <filter-name>ResponseOverrideFilter</filter-name>
       <url-pattern>*.*</url-pattern>
</filter-mapping>

 
Make sure that display tag’s ResponseOverrideFilter placed as first filter in web.xml
 
Related Questions:

How to make Struts 2 tag work inside Display tag?
How to get checkbox values from displaytag using struts2
 

Read More

What is a Memory Leak and Garbage Collector in java?

Posted by on Feb 11, 2014 in Java, JVM | 0 comments

 

A memory leak is the gradual loss of available memory when a program/application repeatedly fails to free memory that it has occupied for temporary use.
 
Memory Leak in java

 

Consider that you have 10 glasses at your house. Whenever these glasses are used they will be cleaned up and kept ready in the stand for future use by the servant when he comes. If you have placed one of the glasses in a room where the servant is not permitted to enter or you have not consumed the juice completely and it is still left in the glass, the servant might think that the glass is still in use and hence he will not clean them.
If this scenario continues, one day you will find all the glasses are uncleaned, so you call the servant to clean all of them.

In java this servant is known as Garbage Collector.
 

Read More