Skip to main content


Creating java.awt.Color instance using hex color codes

Most of you are familiar with java.awt.Color constructor which takes the RGB values to create its instance, but java.awt.Color class also allows one to create its instances using hex color codes through very less known static method ‘ Color.decode(String) ’.   Color obj = Color.decode("#006699");   Very simple isn’t it.
Recent posts

Changing Style tag content using JQuery & IE8

Generally you might not need/want to do this, in fact you’ll rarely change elements of ‘head’ tag using Java script. But this is for those who are left with no other choice. It is really simple, JQuery provides a methods ‘ text() / html() ’ which will allow you to change content of any DOM element. To change the ‘Style’ tag content we can use ‘ text() ’ method, sample code is provided below. <html> <head> <style id="myStyles" type="text/css"> .header { color: blue; } </style> <script type="text/javascript"> $("#myButton").click(function(e) { $("#myStyles").text(".header { color: green }"); }); </script> </head> <body> <h2 class="header">My Page Heading</h2> <input type="button" id="myButton" value="Click Me&

File Copy Using Java NIO

I wanted to post this small piece of code for those who are looking for it. Copying file contents using Streams is old way of doing, Java New IO is the way to go; using Java NIO is better in terms of performance. Following is the code to do file copy using Java NIO. public static void copyFile(File src, File dest) throws IOException {     if (!src.exists()) {         // either return quietly OR throw an exception         return;     }     if (!dest.exists()) {         dest.createNewFile();     }     FileChannel source = new FileInputStream(src).getChannel();     try {         FileChannel destination = new FileOutputStream(dest).getChannel();         try {             source.transferTo(0, source.size(), destination);             // destination.transferFrom(source, 0, source.size());         } finally {             if (destination != null) {                 destination.close();             }         }     } finally {         if (source != null) {             source.close();      

JVM Heap Fragmentation

Recently I read about this topic and wanted to share it, I couldn’t recollect the source where I read it first to give a reference link, so just writing it here. When JVM requires memory, Operating System always allocates continuous memory blocks. When objects get created and destroyed frequently, JVM allocates memory where ever available. Think of a situation where 1000 [each of size 5bytes] objects got allocated in some ‘x’ time span and only 500 of them are killed/de-allocated. Memory occupied by these 500 objects might not be continuous, the de-allocated memory might me scattered across heap memory. Each de-allocated block is of size 5bytes – so, you might be having free memory block of size 5bytes across heap, if JVM wants to allocate a 10byte memory block it cannot use these 5bytes blocks, it has to find a continuous 10byte free space in heap and allocate it. This situation is called as " Heap Fragmentation " [same as ‘ Disk Fragmentation ’], where the heap memory is

Ant Script to make Executable JAR

Recently I observed search patterns about Ant script to make executable JAR files, many end up on wrong pages of my blog. This blog post is for those who are searching for it. Its fairly easy and simple to use Ant Script, to make an executable JAR file, see the script below. <jar destfile="_dist/my-executable-jar.jar">     <fileset dir="_bin" />     …     <manifest>         <attribute name="Main-Class" value="com.mypackage.ClassWithMain" />     </manifest> </jar> As you can see, we usee the ‘jar’ ant task to create a JAR file, we want to create the JAR file with the name ‘my-executable-jar.jar’ – which is given as ‘destfile’ attribute value of ‘jar’ task and we wanted to include all the compiled classes which are available at ‘_bin’ directory – which is provided using ‘fileset’ tag in the second line of the script. Then comes the important part – the ‘manifest’ tag. If you want to make a JAR file execu

JAR Dependency and Ant Jar Task [Merging Archives]

Ever had a situation where you are using Ant to build a JAR file and this JAR uses classes from other JAR OR needs few other JAR files to be in class path. Placing dependent JARs in class path manually is one way of doing it, but what if you want to generate/deliver/use only one JAR [may be executable] which includes all the dependent JARs in it. ‘ zipfileset ’ attribute of the ‘ jar ’ ANT task is the one to use. Say, you are trying to generate a JAR file with name ‘ my-application.jar ’, which needs/depends on another JAR with the name ‘ some-dependency.jar ’. Assuming all the compiled classes are available under ‘_bin’ directory, the dependent JAR is available under ‘lib’ directory and you want to place the generated JAR under ‘_dist’ directory, ANT script might look as given below <jar destfile="_dist/my-application.jar">     <fileset dir="_bin" />     <zipfileset src="lib/some-dependency.jar" includes="**/*.class" excludes=

Java Communications API – Configuration How To

Java Communication API is an extension from Sun to program for Serial/Parallel ports. Sun’s implementation of this specification can be found at " ", there is also a third party Open Source implementation available at " ". Java is platform Independent, but you need some platform specific implementation to access Serial/Parallel ports, and that is why you need to do some extra work to use Java Communication API. First download the API binaries from " " page. Extract it and follow the steps given below for Windows platform. If you are just trying execute programs already written using just JRE, then Copy rxtxParallel.dll to JRE_HOME\bin\ Copy rxtxSerial.dll to JRE_HOME\bin\ Copy RXTXcomm.jar to JRE_HOME\lib\ext\ If you are writing you own applications/programs, compiling and executing them, then Copy rxtxParalle