How to improve performance of java using nailgun server

There are various ways to improve “java” performance.One of the way is use of Nailgun.

Nailgun is a client, protocol, and server for running Java programs from the command line without incurring the JVM startup overhead. Programs run in the server (which is implemented in Java), and are triggered by the client (written in C), which handles all I/O.

It means when java is called every time JVM is loaded so execution time of any application increases due to JVM startup time.To overcome this we have to write java application that listen to particular address and port so each time JVM is not loaded and application can listen on that port and gives expected result. Nailgun help us to achieve this.

Download Nailgun Package

You can download nailgun from sourceforge.Download nailgun src/zip package which containing nailgun-VERSION.jar.

If it is linux operating system you can do “make” which gives ng binary.

If possible put ng binary into your system path.

After Download do following steps:

Start Nailgun Server

  • Set classpath environment variable if not set
  • Copy nailgun-VERSION.jar to JAVA_HOME/lib directory.
  • Now to start nailgun server

java -server com.martiansoftware.nailgun.NGServer

OR

we can start nailgun without copying into lib folder of JAVA_HOME


java -jar /your/path/to/nailgun-VESION.jar

We can start server with different address “–nailgun-server address” and different port “–nailgun-port port no” by passing to above command as a parameter.

By default server address is “127.0.0.1” and port no is “2113”


java -jar /your/path/to/nailgun-VESION.jar 127.0.1.1:2313

Start Application With Nailgun

There are various ways to configure application with nailgun.

ng ng-cp

Above command displays system classpath.

ng ng-cp path/javaclass

Above command add javaclass to nailgun environment.

If javaclass containing main then it works fine. Path should be absolute to execute application absolute path is required which can be reduced by creationg alias of that class.

ng ng-alias

Displays all the alias present in nailgun.

ng ng-alias alias name  class name

It will add alias name for class name so on executing we just call with alias name.

Run Application using following command.

ng alias name or classname [command line arguments]

Stop Nailgun server

ng ng-stop [--server-port 2313 --server-address 127.0.0.1]

Tips :

If application containing dependent jar file which is not in JAVA_HOME/lib you can add those jar file by using following command.

ng ng-cp  /absolute_jar_path/mainclass

If jar file does not containg main class then we have to add those jar file on classpath so when nailgun server start those jar file already in class path.As per my opinion is create one jar for application,that helps us  to run application easily and no need to include multiple jar file individually .You can combine your application and dependent jar in one jar using Netbeans.

More info related to nailgun you can found here.

Leave a reply:

Your email address will not be published.

Site Footer