Monday, August 31, 2015

Grep files from file list

Following Linux script can be used to grep files from file list. Suppose you have file name list in "test2" file. Then, you can use following script to grep files in current location and copy these files to "/home/sujith/files/.

for k in `cat /home/sujith/test2`; 
do echo $k; 
cp *$k /home/sujith/files/.;

String Substring Linux

Suppose file "test" has strings with "-" separator. If you need to get the second part of the string into "test2" file, you can use the following command.




cat test |awk -F"-" '{print $2}' > test2

Monday, August 24, 2015

Insert URL in PDF using iReport

You can add a URL into your PDF file using following iReport xml file. PDF will show "Click Here!" text and URl will redirect to "".

<textField hyperlinkType="Reference" hyperlinkTarget="Blank">
<reportElement x="5" y="5" width="200" height="15"/>
<textFieldExpression class="java.lang.String"><![CDATA["Click Here!"]]></textFieldExpression>

Access Referenced Managed Bean in JSF

Following code can be used to reference one manged bean from another.

public class UsingBean 
    private NeededBean neededBean;

    public NeededBean getNeededBean()
    return neededBean;

    public void setNeededBean(NeededBean neededBean)
    this.neededBean = neededBean;

Now, you can use the following methods to call referenced managed bean property in JAVA class.

ELContext elContext = FacesContext.getCurrentInstance().getELContext();
NeededBean neededBean 
    = (NeededBean) FacesContext.getCurrentInstance().getApplication()
    .getELResolver().getValue(elContext, null, "neededBean");

---------------------------------- or --------------------------------------------

FacesContext facesContext = FacesContext.getCurrentInstance();
NeededBean neededBean
    = (NeededBean)facesContext.getApplication()

Reference site :-

Thursday, June 11, 2015

Restful Web Service Parameters


The @PathParam annotation binds the value of a path segment to a resource method parameter. For example, the following method would intercept an HTTP GET like http://server:port/login/12345 and convert the PathParam "12345" into the String "id"

public class LoginService
  public String login(@PathParam("zip") String id) {
   return "Id is " +id;
As for @FormParam, you can embed the @PathParam declaration at class level, if you prefer.


The @QueryParam annotation binds the value of a path segment to a resource method parameter. For example, the following method would intercept an HTTP GET like http://server:port/login?zip=12345 and inject the query parameter "zip" into the method parameter "zip"

public class LoginService
  public String login(@QueryParam("zip") String zip) {
   return "Id is " +id;
QueryParam can be convenientely used with the DefaultValue annotation so that you can avoid a null pointer exception if no query parameter is passed.

public String login(@DefaultValue("11111") @QueryParam("zip") String zip) {
 return "Id is " +id;
As for @FormParam, you can embed the @PathParam declaration at class level, if you prefer.


The @HeaderParam annotation extracts information from the HTTP header and binds it to a method parameter. Example:

public String callService(@HeaderParam("User-Agent") String whichBrowser) {


The @CookieParam annotation reads an information stored as a cookie and binds it to a method parameter. Example:

public String callService(@CookieParam("sessionid") String sessionid) {



The @MatrixParam annotation can be used to bind an expression containing several property=value to a method parameter. For example, supposing you were to invoke an URL like http://server:port/login;name=francesco;surname=marchioni

public String callService(@MatrixParam("name") String name,
                                @MatrixParam("surname") String surname) {

Reference :-

Wednesday, May 27, 2015

Set default JAVA path

Sometimes you need to use several JAVA versions in a single machine. But you can't change default running JAVA version. Changing PATH variable doesn't affect for this. You can remove system32 JAVA exe file and reset JAVA path for new version to run newly installed JAVA version.

exe running directory :- C:\Windows\System32
application exe name :- java.exe

Now, try version in command prompt using java -version command. This will show new JAVA version.

Tuesday, May 26, 2015

Set email priority in JAVA

Following code can be used to set priority in email server. This is tested and worked fine.JAVA mail API is used for the development.

// Setting the priority of an email is simply a matter of setting the X-Priority header field.
// Values of 1 through 5 are acceptable
// 1 = highest priority, 3 = normal, and 5 = lowest priority.

// Set the email's priority to high:

Tuesday, May 19, 2015

CURL Commands

Here are some valuable curl commands.

GET JSON Request
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource

GET XML Request
curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource

POST data
curl --data "param1=value1&param2=value2" http://hostname/resource

For file upload
curl --form "fileupload=@filename.txt" http://hostname/resource

curl -X POST -d @filename http://hostname/resource

For logging into a site (auth)
curl -d "username=admin&password=admin&submit=Login" --dump-header headers http://localhost/Login
curl -L -b headers http://localhost/

Reference Site :-

Wednesday, May 13, 2015

SFTP client with private key authentication

SFTP ( FTP over SSH ) is a secure way for file transferring. In JAVA application, you can use the following code to build a SFTP client with private key authentication. Here instead of password pass phase is used.

For this code, JSch JAVA library is used. Following jar files also used for code building.

  • jsch-0.1.51.jar
  • commons-vfs2-2.0.jar
  • commons-logging-1.2.jar

         JSch jSch = new JSch();
        final byte[] privatekey = readMyPrivateKeyFromFile(); // Private key must be byte array
        final byte[] passPhrase = "pass phase".getBytes();

            "myusername",    // String userName
            privatekey,          // byte[] privateKey 
            null,            // byte[] publicKey
            passPhrase  // byte[] passPhrase
        Session session = jSch.getSession("myusername", "hostname", 22);
        UserInfo ui = new UserInfoImpl(); // UserInfoImpl implements UserInfo
        Channel channel = session.openChannel("sftp");
        ChannelSftp sftp = (ChannelSftp) channel;

// Now you can execute any SFTP command for the sftp channel.
        final Vector files =".");
        for (Object obj : files) {

Wednesday, May 6, 2015

SFTPconnection using RSA public key

Password encrypted RSA public key file can be used to authenticate user access to FTP server. Following SFTP command describes how to create secure connectivity using RSA public key with SFTP server.

sftp -o IdentityFile=/home/sujith/.ssh/id_rsa_pub_key sujith@

I tried this on Linux server and worked fine.

Wednesday, February 18, 2015

Copy directory from server to server in Linux

You can use following scp command to copy directory with files from one server to another.

scp -rp directory sujith@

Above command will copy the 'directory" directory into home folder of sujith at

Thursday, January 22, 2015

Hadoop installation in Ubuntu (Single node)

Following steps can be used to install Hadoop file system in Ubuntu 14.04.1 64bit version.

I have used following software versions.

Ubuntu 14.04.1 64 bit
JDK1.7 64 bit
Hadoop 2.5.2

For the demo purpose, it is used Oracle Virtual Machine for installing OS.

1. Install Ubuntu in Oracle VM

2. Install JAVA in Ubuntu local path /usr/local/java

3. Install Hadoop in Ubuntu local path /usr/local/hadoop

4. Create a user group for haoop execution
     sudo addgroup hadoop
     sudo adduser --ingroup hadoop hduser
     sudo chown -R hduser:hadoop /usr/local/hadoop

5. logging as "hduser" created above
          su - hduser

6. Now, install ssh for remote access to hadoop file system and authorized public key for ignore password prompt.
          sudo apt-get update
          sudo apt-get install ssh
          sudo apt-get install rsync
          sudo ssh-keygen -t dsa -P ~/.ssh/id_dsa
          cat ~/.ssh/ >> ~/.ssh/authorized_keys

7. Check ssh using following command
          ssh localhost

8. Now, Configure JAVA and Hadoop PATH variables.
          vi .bashrc
          add following variables to end of .bashrc file
                export JAVA_HOME=/usr/local/java
                export HADOOP_PREFIX=/usr/local/hadoop
                export PATH

9. Config Hadoop xml files located in /usr/local/hadoop/etc/hadoop/


export JAVA_HOME=/usr/local/java

vi hdfs-site.xml 


vi core-site.xml


vi mapred-site.xml


vi yarn-site.xml 


10. Format the namenode using following command

          hdfs namenode -format

11. Set the file system partition
         hdfs dfs -mkdir -p /home/hduser/

11. Start services for hadoop file system

12. Testing file system

Create test directory
hadoop fs -mkdir /home/hduser/test

List files in test directory
hadoop fs -ls -R /home/hduser/test

Touch file in test directory
hadoop fs -touchz /home/hduser/test/test1.txt

Copy file from local system
hadoop fs -cp testlocal.txt /home/hduser/test/testlocal.txt

Cat file in hadoop
hadoop fs -cat /home/hduser/test/testlocal.txt

NOTE:- In some cases, you have to change IPv6 using following commands.

sudo vi /etc/sysctl.conf


Monday, January 12, 2015

Check sever status using Socket in JAVA

In some cases, you have to check server status whether server is running or not using telnet command before executing commands on the server application. This code can be used to check whether there is a running something on the given IP and Port.


public class Telnet {

    public static void main(String args[]) {
        try {
            String ip =  "192.168.*.*";           
            int port = 23;
            Socket s1 = new Socket(ip, port);
            InputStream is = s1.getInputStream();
            DataInputStream dis = new DataInputStream(is);
            if (dis != null) {
                System.out.println("Connected IP : " + ip + ", Port : " + port);
            } else {
                System.out.println("Connection Invalid.");


        } catch (Exception e) {
            System.out.println("Not Connected, check Ip and Port.");