Skip to main content

Posts

Showing posts with the label Java

RESTEasy Spring 4 integration

Recently when I was going through the RESTEasy documentation to find out the proper way to integrate it with Spring. There are several ways to do it. This is the first post in this series which will cover the ways to integrate RESTEasy with Spring. We will start with the one which is mentioned in the RESTEasy documentation. We will be using Maven for dependency management in our project. First of all, add the RESTEasy dependency in your project, org.jboss.resteasy resteasy-spring whatever version you are using Above code will add resteasy dependencies in your project. Next step is to define RESTEasy configuration in your web.xml.

Spring Custom Scope

Spring framework is one of the DI frameworks which is largely used to develop web applications(enterprise). It provides almost every feature which is required to develop a enterprise web application. Also it is extendable, so you can customize it the way it suits for your application. Custom bean scope Although bean scopes provided by Spring fulfills requirements of the application, some times you may need something different which is not available by default. In our application we faced the same situation. Requirement We had a requirement of the scope which uses an application value to decide which bean to use(return). The default scopes provided by Spring were not useful and was not providing the required functionality. Implementation As I said earlier, Spring is extendable. It provides an interface Scope, which you can implement to introduce custom scope in your application. To create custom scope, Scope interface needs to be implemented. Scope interface has ...

Notepad++ Compile and Run Java Programs

Notepad++ is a great file editor. It has many features. The most important feature which I like about Notepad++ is its light weight. It loads up so quickly, that's great. It also provides syntax highlighting for many languages. I use notepad++ to edit general files as well as my simple Java programs. Although notepad++ provides functionality to run external programs, I prefer NppJavaTools plugin to compile and run Java programs using notepad++. You can download NppJavaTools plugin from this page - NppJavaTools . Installation Installation of plugins in notepad++ is very simple process. All you have to do is copy plugin dll into plugins folder of notepad++ installation directory and restart the notepad++. Features This plugin provides following features, Compile and run your Java files within Notepad++ Set custom hotkeys for compiling and running Java Code Library support Compile and Run This function allows you to run your Java programs to compile and run from N...

GWT Amazing Toolkit from Google

When GWT was first launched, I read some articles and followed some basic(very basic) tutorials of GWT but left it after a few days as didn't get the time to follow it. Now after a long break I again got the chance to learn and use it again. After following first few tutorials and after watching the demo applications which I built using GWT, I have became the fan of it. It's an awesome toolkit from Google. The most amazing feature of it is that you write the code in Java and it will convert it into highly optimized JavaScript which will run on almost all the browsers!!

Long running task?? SwingWorker to help

I don't know how many people or any one now uses Swing for designing applications in Java. But while reading Java docs in my free time I came across this class called SwingWorker, now many of who work/had worked in Swing might be saying "Whats new in this class"!! Well, nothing is new in there but this class is new for me coz I never worked in Swing, but in my college days I was working on a project using Swing called Visual Java. A NetBeans kind of software, I stopped working on it when I came to know that its already there.. :) Coming back to SwingWorker. I am writing this post for this class is because this class is really a handy for those who are developing or starting to develop applications using Swing. While developing any software if there is any long running task, we put it in a separate thread, Background thread. So SwingWorker provides us the same functionality adding some methods where we can put our UI manipulation code. Both the classes provide doInBac...

Java Tools Plugin for Notepad++

This post and the plugin is outdated. Please use plugin from this link which is updated and allows customization of shortcut key mappings and much more . Notepad++ is a great free editor. I like notepad++ because its a light weight editor and loads instantly. I use notepad++ for editing many files everyday. One plugin I always wanted was a plugin which lets me compile my java files and execute them from editor itself, but I couldn't find it so I wrote a myself. This plugin can be downloaded from  this link . I am working on this plugin to make it more effective, so that if user have more than one installations of JDK or JRE then user should be able to choose which one to use etc. This is a simple plugin which has two commands- Compile - compiles a java file. Run - executes a java file. For using this plugin Java must be installed on the system.

Encrypt Decrypt data using AES in Java

AES stands for Advanced Encryption Standard. AES is an Symmetric Key Algorithm, that means key used for encrypting the data, same key will be used for  decryption of  the encrypted data. This algorithm supplants DES algorithm. This post shows how to use AES algorithm in Java to encrypt and decrypt data. Encrypting Data- import java.security.GeneralSecurityException; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; public class AESTest1 { public static void main(String[] args) throws GeneralSecurityException { KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); keyGenerator.init(128); SecretKey key = keyGenerator.generateKey(); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, key); byte encryptedData[] = cipher.doFinal(args[0].getBytes()); } } In above example we have used KeyGenerator class, which generates key for encrypting data, as we are using...

Type Erasure in Java

While reading some posts online I came across the following post - List<integer> list = new ArrayList<integer>(); List&lt;integer&gt; list = new ArrayList&lt;integer&gt;(); list.add(1); list.add(2); List list1 = new ArrayList(); list1.add(&quot;Some String&quot;); list.addAll(list1); This code compiles and executes as well, this works because of Type Erasure concept in Java. I knew about Type Erasure but while reading more about it on internet I found some posts which were questioning on Type Erasure concept. Following code makes more confusing about Type Erasure concept, if compiler removes all the information about type parameters and type arguments then how does following code works?? class A<T>{ } class B extends A<String>{ public B() { System.out.println(((ParameterizedType)getClass(). getGenericSuperclass()).getActualTypeArguments()[0]); } } public static void main(String args[]){ ...

Manipulating excel files in Java

We many times come across this situation where we want to create or edit excel files in java, although there are many open source libraries available out there which allows us to create or edit excel files in java but the most stable among them which I think is Apache Poi. Apache Poi library allows creation and editing of excel files. This library supports traditional 2003 format and 2007 format also. Apache Poi library supports excel macros, cell customization(cell color, text color, cell background color) and much more. Although Poi supports both xls and xlsx files, xls files are manipulated smoothly but xlsx file manipulating requires much more RAM and time for processing than that of xls. Poi has two classes HSSFWorkbook for xls files and XSSFWorkbook for xlsx files, both implements to Workbook interface. You can create multiple workbook sheets. In next post I will show how to use Apache Poi API to manipulate excel files.

Dependency Injection in Action

Dependency Injection , one of the most talked topic in developers world.. I discussed about Dependency Injection in my earlier post, so if you haven't read it then I suggest that you should read it first. In this post I am going to describe how dependency injection works.. Dependency injection is basically creating object and setting property of that object which are described in configuration.

Java Reflections

Java Reflections is one of the most powerful API in Java. Java Reflection API allows us to access metadata about the classes at runtime i.e. Reflections allows us to find out constructors, fields and methods from a Java class. In this post I will show how to find out constructors, fields and methods from a Java class.

Dependency Injection

Dependency Injection i.e. Inversion Of Control . I have been asked this question so many times that "What is dependency injection?" so, I felt that I should write something about it.. So what is Dependency Injection?? Dependency Injection is the process of not to create objects explicitly but to describe how they should be created. As I am working on Spring and Hibernate so this question has been asked for Spring, so how does spring uses it? In Spring, it uses XML file where we describe how beans(objects) should be created, Spring uses this XML file and creates objects and we simply use these beans in our project. Consider following scenario,

Java Annotations

A few days ago in our project we needed that the field which we are using should not be displayed to the user but relevant title should be displayed. This title will never change as this property is one of the important properties of that class. There I thought that Java Annotations might be useful for me in this case, now for those who don't know about annotations I will explain a little about them. Java Annotations is the feature provided by Java with the help of which we can add data to our class, method or fields. Now there are three different ways of annotations that we can use. Types Source  - These types of annotations are not present in class file, they are only present in the source file and they are discarded by the compiler. Class     - These types of annotations are present in class file and source file, they are not loaded by JVM. Runtime - These types of annotations are present in class file and source file and they are also loaded by...

Web Application Internationalisation

Google Transliterate is one of the awesome tool for typing in languages other than English. But how to make your web application support i18n?? Currently I work on Spring framework which has in-built support for i18n. We use messages files(i.e. Resource Bundles in Java) for i18n. The question is how to convert native characters to UTF-8 characters!! Well for that also solution is provided by Sun, JDK comes with a tool "native2ascii" which is located in your default installation directory of JDK. This tool converts native characters to utf-8 characters. Following example shows how to use this tool First of all write a file which contains text in your own language(For that you can use Google Transliterate IME with notepad) Then save this file at some handy location e.g. on c:\input.txt e.g. My text file contains - सचिन Now on command prompt navigate to jdk bin directory and type following command native2ascii -encoding utf-8 c:\input.txt c:\output.txt -encoding param...

JNI Simple Tutorial

After a long break I am posting a simple tutorial on JNI which is one of my favourites. JNI is Java Native Interface, which we can use to call the native methods written in languages other than Java. We are going to call a method written in C++. JNI is very useful where you cannot use Java such as getting system hardware information and much more, it depends up on what you need. For this tutorial you need to read my old post on creating a simple dll in VC++, though if you know how to create DLL's using VC++ then you can skip that post. Lets get started with our tutorial.. First step is to write a class which will use JNI. Well its too simple, following is the code for our Java class which will be using JNI import java.io.*; class JNITest { // native method public native static void WinMessage(String str); static { System.loadLibrary("SimpleDll"); } public static void main(String args[]) { WinMessage("Hello From JNI"); } } After writing this J...

Using JTable

JTable is the component which helps us to display the multi columned data. This post is about fetching database data and displaying it using JTable. First we will design our Frame public class MyFrame extends JFrame {  private JTable table;  private JScrollPane jsp;  public MyFrame()  {   super("MyFrame");   setSize(400,400);   setDefaultCloseOperation(EXIT_ON_CLOSE);   setLocationRelativeTo(null);   initMyFrame();   setVisible(true);  }  private void initMyFrame()  {   setLayout(new BorderLayout());   table=new JTable();   jsp=new JScrollPane(table);   add(jsp,BorderLayout.CENTER);   // database connection and remaining code will go here  }  public static void main(String args[])  {   new MyFrame();  } }

Creating Executable Jar File

Creating a Executable Jar file is a very easy task until you don't know how to do it!! If you are using Eclipse for Java Application development then its more easier than using command line, Following are the steps for creating Jar file - using Eclipse 1. Right click on your project and select Export from popup menu

New LookAndFeel in JDK7

In next release of JDK i.e. JDK 7, it contains many new features. One of them is addition of new LookAndFeel - Nimbus. Following screenshot shows the demo of Nimbus LookAndFeel . You can download the Source Code for the demo which is shown in above picture. Download -  LookAndFeelDemo For running this demo you must have JDK7 installed on your system.

Reading console output in Java

Have you ever needed to read console output from a Java program?? here is a simple code which shows how to read console output - import java.io.*; class ConsoleReader { public static void main(String args[]) throws Exception { Process p=Runtime.getRuntime().exec("ping www.google.com"); BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream())); String str=""; while(str!=null) { System.out.println(str); str=br.readLine(); } br.close(); } }