Redirect process output to std output when you fork one process from another process.

In the situation where you want to redirect input / output of one process to the std input output of parent process (from which you are starting first process). You can make use of inheritIO() method of a ProcessBuilder. ProcessBuilder pb = new ProcessBuilder("command", "argument); pb.directory(new File(<directory from where you want to run the command>)); pb.inheritIO(); Process p = pb.start(); p.waitFor(); It may not be necessary to call waitFor() method in above code. [Read More]

Java: Retreive ByteArray from Direct ByteBuffer and convert it to String

I assume you understand what is Direct ByteBuffer. I was trying to retrieve byte[] from Direct ByteBuffer

ByteBuffer buffer = ByteBuffer.allocateDirect(10000);
.....
.....
buffer.flip();
byte[] data = buffer.array();

and I was getting this error

Exception in thread "main" java.lang.UnsupportedOperationException
 at java.nio.ByteBuffer.array(ByteBuffer.java:959)

I was able to solve this as follows

ByteBuffer buffer = ByteBuffer.allocateDirect(10000);
.....
.....
buffer.flip();
byte[] data = new byte[buffer.remaining()];
buffer.get(data);
// Here you hava data populated inside data[] array
String token = new String(data);

Java: Add shutdown hook invoked even with CTRL + C

Add Shutdown hook with JVM, which will be invoked even when application is killed by pressing CTRL + C Create a separate class which extends java.lang.Thread class or implements java.lang.Runnable interface public class AppShutDownHook extends Thread { @Override public void run() { // TODO: Complete your shutdown activity here. } } Register this thread with Java Runtime in your code where your application is getting initialised. Most probably inside main. [Read More]

Install ZeroMQ on ubuntu with Java Binding

First install all required tools to build zeromq library sudo apt-get install libtool autoconf automake uuid-dev build-essential pkg-config Once you have required tools install download zeromq source code and make a build #Download latest zermoq source code http://zeromq.org/area:download #I tried it with zeromq-3.2.4.tar.gz (http://download.zeromq.org/zeromq-3.2.4.tar.gz) $ zeromq-3.2.4.tar.gz $ tar -zxvf zeromq-3.2.4.tar.gz $ cd zeromq-3.2.4 $ ./configure $ make $ sudo make install $ cd ../ This will install zeromq libraries at location /usr/local/lib you may like to set this path with LD_LIBRARY_PATH so that the libraries will be used at the time of execution. [Read More]

Download Oracle JDK / JRE from console / command prompt

Many a times we have to download JDK/JRE on remote machine through console. In most of the cases we tend to download it on our desktop and then upload it to the remote machine through SCP as the download link from Oracle website are not directly accessible. You need to first accept the agreement so that you get the download link. The link is not usable to download it using wget directly. [Read More]
Java  JDK  wget 

Compare more than two war files and repackage them excluding common jars

Some times you need to deploy more than one war files under same servlet container. In those war files there might have some common jars across them, which you want to deploy directly to servlet containers class path. So that container won’t load those jars separately in the context of each war application. Which in turn will reduce the utilisation of permgen memory space. Following code snippet will help you to compare more than two war files, extract common shared libraries inside separate folder and repackage those wars excluding common jar files. [Read More]
java  war 

MySql JPA toplink Auto increment id generation.

To configure JPA entity to make use of auto increment feature of MySql for primary key generation you can make use of IDENTITY strategy ref below code. @Entity public class Customer { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; . . . public long getId() { return id; } } But problem with this way of generating ids with MySql and JPA combination is when you persist new entity and try to retrieve id assigned to newly persisted entity using getId() method. [Read More]
jpa  mysql  ORM  Java 

Tomcat 7 onwards set additional JAVA_OPTS options

To set JAVA_OPTS options which you want to set additionally while running the tomcat should be set inside <tomcat home>/bin/setenv.sh if this file is not present then create one and set variable JAVA_OPTS="-Dsomething $JAVA_OPTS". If you have to set some variables while only starting the tomcat then you need to set those variables to CATALINA_OPTS rather than JAVA_OPTS.

Jersey REST retrieve List of custom object

It took me while to understand how one require to retrieve generic object from ClientResponse. I had an array of objects in Json format as a response of an REST api, which I was trying to deserialize it into List<CustomObject> refer below sample. ClientResponse rsp = webresource.path(id.toString()).type(MediaType.APPLICATION_JSON).get(ClientResponse.class); List<CustomObject> list = rsp.getEntity(new GenericType<List<CustomObject>>(CustomObject.class)); But I kept on getting following Error: Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of test.CustomObject out of START_ARRAY token at [Source: sun. [Read More]

Access file (JAVA)

Access resource / file packaged inside a jar or a located on classpath. Two ways to access the file located inside jar or located on classpath. Use getResourceAsStream("filename") on ClassLoader, one can retrieve the class loader instance from class instance of any class or Thread.currentThread().getClassLoader(). When you try to load the file using class loader, file will be always searched inside root folder on classpath. e.g. Thread.currentThread().getClassLoader().getResourceAsStream("config.properties") and Thread.currentThread().getClassLoader().getResourceAsStream("/config.properties") either way class loader will try to search file from root folder which is on class path or from root folder inside jar. [Read More]