Skip to main content

Posts

Showing posts from 2011

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 fra…

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 executable – …

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="META-…

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 "http://java.sun.com/products/javacomm", there is also a third party Open Source implementation available at "http://rxtx.qbang.org/wiki/index.php/Main_Page". 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 "http://rxtx.qbang.org/wiki/index.php/Download" 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 rxtxParallel.dll to JDK_HOME\jre\bin…

Calculating Domain URL in Java Web Applications

When you are developing web applications, its obvious that you’ll write code to generate paths for resources like images, stylesheets, scripts, etc. You can do this by simply using relative paths. In your JSP application, you can do this using ‘getContextPath()’ method of ‘HttpServletRequest’ object.
Lets see what this method gives you. It returns a String value, the returned string will always have a leading ‘/’ character and will never have a trailing ‘/’. This method returns an empty string when you deploy your application as ROOT context and returns the subdomain context when deployed as subdomain.
For example, if you name your WAR file as ‘myExample.war’ and deploy it in TOMCAT [by placing the WAR file in ‘webapps’ directory]. The ‘getContextPath()’ method will return ‘/myExample’.
In your JSP page, if you’ve a image tag pointing to an image resource and you want it to work even when deployed as subdomain you can do that by using this ‘getContextPath()’ method as used below.
&l…