Top 25 Unix Commands With Examples

Hello Friends,

In this tutorial,you will learn top Unix commands which are handy in day to day work,specially from Java developer point of view.



1. How to create a new file in Unix

    touch <fileName>

    Example : touch abc.txt

2. How to see content of file in Unix(without able to edit)

    cat <FileName>

    Example : cat abc.txt

3. How to open file in editor mode

    vi <File Name>

   Example : vi abc.txt

4. How to edit File and add content in File in Unix

    a) Open file with following command
     
        vi abc.txt

    b)   Press I ,it will bring file in edit mode.

    c)   Type whatever you enter in file.

    d)   Type :wq! and enter.This will save content you entered in file.

5. How to find a File/Directory with name starting with particular character in Unix

    ls -ltr | grep <character>*

   Example : Find file/directory starting with name "ab"

   ls -lrt | grep ab*   

6. How to give/change permissions of File or Directory.

    File permissions in numeric format and their meaning :

   0 – no permissions
   1 – execute only
   2 – write only
   3 – write and execute
   4 – read only
   5 – read and execute
   6 – read and write
   7 – read, write and execute

  By default,when we create a File in Unix ,it is created with permission 666(read/write).

  By default,when we create a Directory in Unix,it is created with permission  777(read/write/execute).

Along with numeric notation,Unix permissions can also be represented by following characters:

Reference       Class      Description
u                     user        the owner of the file
g                     group     users who are members of the file's group
o                     others     users who are not the owner of the file or members of the group
a                     all           all three of the above, is the same as ugo
r                     read         read a file or list a directory's contents
w                   write        write to a file or directory

x                    execute    execute a file

6.1. How to make File read only in Unix
 
    chmod  <permission> <fileName>
 
    Example : chmod 400 abc.txt

6.2. How to give File Read/Write Permissions in Unix
  
    chmod <Permission> <FileName>

    Example : chmod 777 abc.txt

7. How to find list of all the links in a directory in Unix

    ls -lrt | grep "^l"

   ^ - This signifies start of each line
   l  - Each symbolic link has "1'' in the beginning of line like below :

   lrwxrwxrwx  1 gg99a weblog 3 Dec  9 22:20 latest -> 1.3

  So this command prints all lines from result of ls -lrt,which starts with "l" and all symolic link's lines start with "l",hence the result.

8. How to see content of current directory

   ls

  This will not give details about Files or directories.

9. How to see content of current directory with details such as 
   - the exact size of the file
   - who owns the file
   - who has the right to look at it
   - when it was last modified.

   ls -lrt

10. How to see all files  including the ones whose filenames begin in a dot

    ls -a 

   This however does not give details as mentioned in point 9.

11. How to see List of all background process

    Jobs

12. How to bring a background process to foreground

   fg

13. How to kill a process
  
  kill <ProcessId>

14. How to see current date in Unix

   date

15. How to see History of commands executed 

   history 
  
   Using only "history" will give you all the commands executed in current session.

   If you want to see specific type of command,use grep along with history as below :

  For example ,if you have executed command to create directory multiple times,you can  execute history command as below.It will list mkdir command all the times you have executed previously in current session.
  
history | grep  mk

16. How to clear the console in Unix

    clear

17. How to check ,how much space is left on the current drive in Unix

    df -h

18. How to see which process is taking how much time in Unix

    top

    Press shift + O to get all options by which you can sort.For example ,to sort by PID,press 'a' and then press any key.It will sort all the processes by PID.

19. How to copy File from one host to another in Unix

     When you are sending files from current machine to remote machine

    scp <current machine path to file,which we want to send to remote machine> user@remote machine:/remote path

    Example : scp /var/log/was/abc.txt GA99@test.com:/var/log/remoteDirectory

    When you want to retrieve file(s) from remote machine

    scp user@remote machine:/remote path to file  <current machine path where we want to download the file>
    Example : scp user@remote machine:/var/log/remoteDirectory/abc.txt  /var/log/was

20. How to find ,how many times a word is there in File
  
    grep -c  <word to find> <FileName>

    Example : grep -c Hi abc.txt

    Above command will give count of word Hi in File abc.txt.

21. How to execute previously executed command i nUnix

      !<Previous Command>

     This is really handy when your command includes lots of parameters and options.You need not type all those parameters and options again with this shorthand.

Example : Say I executed following command:

 ls -lrt | grep abc

Now I want to execute this command again,I just have to type following to execute above command again :

!ls

22. How to find all the files with certain content in Unix

   find . –name <type of files to be searched> | xargs grep <content to be matched>

   Example : 

   find . -name "*.txt" | xargs grep "Hi"

23. How to find file(s) which have been modified since last day,today or more than 1 day

  23.1)Find all files in current directory and sub directories which has been modified exactly 1 day back.

  find . -mtime 1

23.2) Find all files in current directory and sub directories which has been modified more than 1 day before

 find . -mtime +1

23.3) Find all files in current directory and sub directories which has been modified less than 1 day before.

 find . -mtime -1

24. How to create Soft link in Unix

   ln -s <SoftLink name> <Directory which soft link will point to>

  Example : ln -s latest 1.0

  So here you are pointing latest softlink to directory 1.0

24. How to update Soft link in Unix

  ln -nsf <New directory> <Soft link>

  Example : Say you want to point latest now to new directory 1.1,then you will execute following command :

 ln -nsf 1.1 latest

25.How to remove Soft link in Unix

 rm <Softlink name>


That's it for today.

How to see maven dependency hierarchy of an artifact in Eclipse

Hello Friends,

In this post,we will learn that how to see maven dependency hierarchy of an artifact in Eclipse.



Step 1 : Open pom.xml which have all dependencies specified in Eclipse.




Step 2 :  Open Dependency Hierarchy tab as can be seen in bottom of above screenshot.




As you can see in above screenshot,here we can see all jars on which a given jar is dependent upon.
For example :
jersey-container-servlet-core : 2.23.2 has dependency on jars as can be seen in following screenshot for compilation :




Now say you want to see ,at which all places a particular Jar is being used ,you can just put name of jar in Filter box and it will show you all Jars which are using given jar as dependency.

For example : Let us try to find where all hk2-locator is being used :



That's it.With this we learnt How to see dependency hierarchies of artifacts in eclipse.

Thanks for reading.Let me know,if you face any issue following above tutorial in comments section.


How to Setup Maven on Mac

Hello Friends,

In this post,we will learn how to setup maven on Mac machine.




I am using :

Mac OS Version 10.11.6
Maven 3.3.9
JDK : 1.8 (minimum 1.7 version required with maven 3.3.9)


Step 1 :

Go to Apache Maven's official web site http://maven.apache.org/download.cgi

Step 2:

Download maven at any convenient location on your system from following link.It comes in compact form in tar.gz format.

https://archive.apache.org/dist/maven/maven-3/3.3.9/binaries/

Step 3:

Extract by executing following command

tar -xvf apache-maven-3.3.9-bin.tar.gz

Step 4:

Open terminal utility and execute following command :

vim ~/.bash_profile

Step 5 :

Press 'I'  key,which will bring .bash_profile in edit  mode.

Step 6 :

Set environment variable M2_HOME as location of maven folder you extracted in  step 3,by adding following line in .bash_profile file  :

export M2_HOME = /Users/Aakarsh/Downloads/apache-maven-3.53.9

Step 7 :

Set PATH environment variable as below by adding following line in .bash_profile file,so that you can execute maven commands from any directory.

export PATH = $PATH:$M2_HOME/bin


Step 7 :

Set environment variable JAVA_HOME as location of JDK on your system.If you don't have JDK installed ,you should install that first.

Here is the path where you can find jdk installed :
/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home

export JAVA_HOME = /Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home


Step : To know if maven is installed successfully,execute following command and you will be able to see installed version.

mvn -version




Step : To know the location of maven ,execute following command

which mvn 



Thats it.So with this we learnt that how to set up maven on mac machine.

Thanks for reading.Let me know if you face any issue in setting up in comments section.



How to create new Git Repository and host it in Bitbucket


What is Git ?




While working in a team which involves multiple developers,it is very much necessary to use a Version Control System.

Version Control System is basically a Software using which

- Multiple developers can put their code in(Check In).
- Take (Check Out) the code of fellow Software developers.
- See the version history of the files(Who committed what code on what date for what purpose)
- Compare the changes in two versions.
- and much more....

Git is basically a Version Control System, Which is open source Software(project) which was originally developed by Linus Torvalds,who was the face behind Linux operating system kernel as well.

Git differs from other famous version control systems like SVN or CVS in the way that it is distributed version control system.So in Git rather than having only one single place for the full version history of the software as common in version control systems like CVS or  SVN, every developer's working copy of the code is also a repository that can contain the full history of all changes.

If you have worked on SVN,you would know that to setup repository in SVN ,we need to know the server hosting the repository and you should have right privileges to check in/check out.But in Git,you do not need any server to start with.You just do git init and you are ready with your repository.But note that this repository is only on your local machine,so if you are single developer working on project,it is fine to have repository on your local machine.but in case code needs to be accessed by multiple developers then you need to host your Git repository.We will see later in this post,how we can host Git repositories on  server using repository hosting application bitbucket(GitHub is another alternative though).


How to Install Git 

1) Download Git

    For Windows from following link :

    https://git-for-windows.github.io/

    For Mac from following link :
 
     https://sourceforge.net/projects/git-osx-installer/files/

2) Install Git by running exe file for Windows or dmg file for Mac and following installation steps.

3) Once all installation steps done,you can verify if Git is installed successfully or not  by executing following command on command prompt :


Create a Project Which you want to add in git repository

4) Create a Java project in Eclipse and a file TestGitBitbucket.java with a main method in a package nl.javaSolutionsGuide.



 Create new git Repository and add/commit code to it

5) Go to command prompt and go to directory containing project.
   

6) Execute command git init
 
git init command creates a new Git repository.

- Using this command,we can create new empty repository or we can convert an existing un versioned project to Git repository.

- Also git init creates  .git sub directory in the current working directory from where you executed git init command.This .git directory contains all of the necessary Git metadata for the new repository.This .git directory contains one HEAD file which points to the currently checked out commit.

You need to execute this  command only once when setting up repository for the first time.However executing again will not  harm and it will not override the existing configuration.

- If you want to keep this .git file at separate location than the project folder,then you can use following command while initializing repository :

git init <directory path where you want your .git directory to be created>


        




7) Add file TestGitBitbucket.java to the repository that we initialized in step 6 by executing git add command

git add C:\Users\N54448\BlogWorkSpace\testGitBitbucketProject\src\nl\javaSolutionsGuide\TestGitBitbucket.java



 - git add command adds file(s) to the staging area of Git from working directory.So after git add file(s) are still not committed to repository.

 - So you might have made changes in 10 files in your working directory(in your project),but if you want to commit only 5 files in Git repository,you can add only 5 files in staging area and then git commit will commit only these 5 files.This helps you committing files in logical groups.For example,if you have worked on two bug fixes and you want to commit all files belonging to bug fix 1 first ,then you will put all files changed for bug fix 1 to staging area and then commit those and then you can add changed files for bug fix 2 to staging area and commit those.

- Please  note the file(s) in staging area after adding by git add command will not have version history yet.


8)Commit the files in Git repository

git commit -m " committing to Git"



- git commit commits the staged changed (using git add) to the git repository.
- use of -m suggests git that while committing the staged changes,instead of launching an editor,use <message> as the commit message
- if -m will not be used then text editor will be launched,where commit comments can be provided.
- git commit commits the staged changes always in the local repository.This is different from what you see in SVN,where when you commit changes,changes are directly committed to the central repository

Create a remote central repository in bitbucket and push code from local git repository to bitbucket

What is Bitbucket?

Bitbucket provides hosting service for Git repositories.It can also host repositories created with Mercurial,but mercurial is not in scope of this post,so we  will consider only Git repositories.

In simple words,you create Git repository on your local machine using git  init and then add code ,commit code to this local Git repository,but if you are working in a team of multiple developers,other developers could not be able to get your code ,because it is lying on your machine only.So there has to be some central place,where all developers in team could push their  code and similarly they could check out the code done by other developers.

Bitbucket is a tool or application ,wherein you can create central or remote repository with .git extension and then push git repository to this central repository.

It is free hosting service,where in you can create multiple private repositories as well. A private repository is a repository which will be visible/accessible only to to you and to the people whom you give access to.

Let us see how to create Bitbucket repository ,connect existing Git repo to Bitbucket and push code from Git repo to bit bucket.

9) Go to https://bitbucket.org/ and Create account.


10) Login with the credentials created in Step1 Or you can login with gmail id.




11) Create a repository in bit bucket by clicking create Repository button (+ sign).


12) Give any suitable name to the repository.


Once you will click on Create repository button,a new repository will be created in bitbucket(testbitbucketrepo.git) and you can see the Url on the page which opens after clicking on Create repository button.Also  as "Access Level" is ticked ,this repository will be visible only to you.If you will un-tick it,repository will be visible/accessible to everyone having Url.

Private Repositories - Accessible to you and people whom you give access to.People must  have bitbucket account to access private repositories.
Public Repositories  - Accessible to everyone having Url of repsoitory.People need not have bitbucket
account to access public repositories.

In my case it is as below :

https://Gauravbhrdwj@bitbucket.org/Gauravbhrdwj/testbitbucketrepo.git

13)From the Overview page of the repository you created in Bitbucket, choose I have an existing project.

14) Switch to your repository's directory.

 cd C:\Users\N54448\BlogWorkSpace\testGitBitbucketProject



15) Connect your existing repository to Bitbucket by executing following command from existing repo directory folder.

git remote add origin https://Gauravbhrdwj@bitbucket.org/Gauravbhrdwj/testbitbucketrepo.git




-Above command creates alias for whole Url   https://Gauravbhrdwj@bitbucket.org/Gauravbhrdwj/testbitbucketrepo.git with name origin.
-Alias name can be any name instead of origin.
-So basically,now if you want to push to remote repository,you will not need to type whole Url again,as can be seen in next step.Just use alias name i.e. origin.
-In case you get error as below,executing above command,you can use www.bitbucket.org instead of bitbucket.org.
"Fatal: unable to access 'https://Gauravbhrdwj@bitbucket.org/Gauravbhrdwj/testbitbucketrepo.git/': Could not resolve host: bitbucket.org".

16) Push code from local repository to remote repository created in Bit bucket by executing following command

      git push -u origin master



   

   - Pushes the commits in the local branch named master to the remote named origin.
   - After pushing your changes to remote,it can be pulled by other developers on their machine.

17) To checkout the code from remote bitbucket repository to local,you need to execute following     command on your command prompt.

git clone <repo> <directory>

git clone https://Gauravbhrdwj@bitbucket.org/Gauravbhrdwj/testbitbucketrepo.git C:/users/checkoutCodehere

Hope this was helpful.Leave your comments in comments section,if you face any issue while following this tutorial.Thanks for reading.

References 


How to create Soap Web Service in Java with Example

Hello Friends,

In this tutorial,we will learn how to create Soap based Web Services in Java language.



Before we deep dive into that,let us understand what Web Service is actually.

                  Web Service = Web + Service

Web Service as its name implies is service provided over web.

After this definition, immediate question that comes to mind is that we have lot of web sites available which provides services to its clients/users over web,so are all these web sites which provides services over web/Internet are Web Services?

Answer is NO.

Web services are created to be consumed by the applications i.e. there will be some application code which will call(consume) web service(some method of web service) to use the functionality of the web service.

Example : Say I have my web application and on the home page of my application,I want to display Weather conditions(Temperature) ,then I can call web service which expose operation(method) to fetch Weather specific data from my web application and display the data on my web application.

Web applications are for human consumption via browser.

Example : Facebook,LinkedIn,Yahoo.

Let us see below steps to create Soap based web Service :

1) Create dynamic Web Project in Eclipse by right clicking in Project explorer and then select New-> Dynamic Web Project



2)
2.1) Give any name to project ,for example "FirstWebService".
2.2) Select Target Runtime as Apache Tomcat v8.0.
Note : You can use some other version of Tomcat as well or some other Servlet container as well.
2.3) Click Finish.



3) Below is how project structure will look like in Eclipse.



4)
4.1 Create a package within src folder with name nl.blogspot.javasolutionsguide.service by right clicking on src folder and then select New -> Package.


 

4.2 Within this package create a interface with name CalculatorService by right clicking on package name and then selecting New-> Interface

package nl.blogspot.javasolutionsguide.service;

/**
 * @author JavaSolutionsGuide
 *
 */
public interface CalculatorService {

}


4.3 Add methods in CalculatorService ,which will be the operations exposed by this web service or in other words these will be the operations which client/consumer of this web service can expect from this web service.

package nl.blogspot.javasolutionsguide.service;

/**
 * @author JavaSolutionsGuide
 *
 */
public interface CalculatorService {
 
 public int add(int a,int b);
 
 public int subtract(int a,int b);
 
 public int multiply(int a, int b);
 
 public int division(int a,int b);

}



4.3 Create another package within src folder with name nl.blogspot.javasolutionsguide.service.impl
4.4 Create class with name CalculatorServiceImpl which should implement CalculatorService interface.

/**
 * @author JavaSolutionsGuide
 *
 */
public class CalculatorServiceImpl implements CalculatorService{

 @Override
 public int add(int a, int b) {
  return a + b;
 }

 @Override
 public int subtract(int a, int b) {
  return a - b;
 }

 @Override
 public int multiply(int a, int b) {
  return a * b;
 }

 @Override
 public int division(int a, int b) {
  return a/b;
 }

}

5) Right click on CalculatorServiceImpl file and select Web Services -> Create Web Services




6) You will see following wizard.

As you can see,in the "web service type",we have chosen "Bottom up Java bean Web Service",which means we will create Java code having implementation first and from that we will generate contract i.e. WSDL,which then can be shared with consumer of the web service.In another approach which is "Top down Java bean Web Service" approach ,first WSDL is created and then Implementation is created.

In the Service Implementation,give the name of CalculatorServiceImpl class which we created above.
Also make sure that In the web Service section and Client type section,slider is completely on left ,which means we want to get all steps performed for web service from Develop Service -> Assemble Service -> Deploy Service ->Install Service ->Test Service  and all following steps for web service client from Develop Client -> Assemble Client -> Deploy Client -> Install Client-> Start Client ->Test Client.

Click Next.



7) You will get following screen,wherein you can select which methods of CalculatorServiceImpl class you want to expose.By default all the methods within class will be selected as can be seen in below screenshot.Click on Next,as here we will expose all methods of CalculatorServiceImpl class.



8) You will get following screen.Click on Start Server.



9) You can see message "Currently server is started" in below screen now. Click on Finish.




10).As you can see in below screenshot,a new Web Service Client project with name "FirstWebServiceClient" has been automatically created(because in Client type section in step 6 ,we had moved slider to left most ,which means we want to get client created automatically (to test web service using that client) and deployed on Tomcat server along with Web service "FirstWebService".


 and as we moved slider till "Test Client" in step 6 ,browser with TestClient.jsp from Client project "FirstWebServiceClient"  is opened to test Web Service,as can be seen in below screenshot.




11)Also You will see that eclipse has added WSDL,Axis and few more jars.Here Axis is nothing but one of the implementation of java Web Services specifications.

Now What is this WSDL.

WSDL stands for Web Service Description Language.

As its name implies,this file describes web service.It tells what all methods are there in web service (calculatorServiceImpl class in our case) and what type of input parameters they expect and what are there return types.

The Service creator needs to share this WSDL to service consumer,so that service consumer will get to know which methods of service can be called and what type of input parameter consumer needs to send and what return type they can expect.Accordingly consumer will call service methods of exposed web service.Here for example purpose,we have created both Web Service and Consumer,but in real life ,normally ,we will either be provider or consumer of the web service at a time.

12) To test web service,subtract functionality,click on subtract link in above screenshot.we will get following screen,where we can enter our inputs.


13) Enter any input to test in two text boxes and click on invoke.This will invoke web service and we will be able to see result in "Result" section as can be seen in  below screenshot.


14) If we want to debug the code of our web service,we can restart the server in Debug mode and put breakpoint in CalculatorServiceImpl's method which we  want to debug and can invoke subtract again from TestClient.jsp


15) Click on Yes,and you will be in Subtract method for debugging as below :


Thats it.With this we saw how to create Soap Web Service in Java.

Thanks for reading.Please let me know,if you face any issue during implementation.

How to migrate from Jersey 1 to Jersey 2 on Websphere

Hello Friends,

In this tutorial,we will see how to migrate from Jersey 1 to Jersey 2.

Here I am taking example of Jersey 1 version 1.18.3 and Jersey 2 version 2.23.2.

Jersey 1.18.3 dependencies 

                     <dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.18.3</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-spring</artifactId>
<version>1.18.3</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.18.3</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-xc</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.18.3</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>

</dependency>

Jersey 2.23.2 dependencies :

                       <dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.23.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.23.2</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.23.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.23.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-entity-filtering</artifactId>
<version>2.23.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.23.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>2.23.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.bundles.repackaged</groupId>
<artifactId>jersey-guava</artifactId>
<version>2.23.2</version>
</dependency>
<dependency>
 <groupId>org.glassfish.jersey.media</groupId>
 <artifactId>jersey-media-json-jackson</artifactId>
 <version>2.23.2</version>
 <exclusions>
 <exclusion>
 <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-annotations</artifactId>
 </exclusion>
 </exclusions>
</dependency>
<dependency>
 <groupId>com.fasterxml.jackson.core</groupId>
 <artifactId>jackson-annotations</artifactId>
 <version>2.5.4</version>
</dependency>
<dependency>
   <groupId>org.glassfish.jersey.media</groupId>
   <artifactId>jersey-media-multipart</artifactId>
   <version>2.23.2</version>
</dependency>

<dependency>
   <groupId>org.jvnet</groupId>
   <artifactId>mimepull</artifactId>
   <version>1.6</version>

</dependency>

Jersey 1.18.3 Web.xml configuration 

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>
      com.sun.jersey.spi.spring.container.servlet.SpringServlet
    </servlet-class>
    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>com.test.rest</param-value>
    </init-param>
    <init-param>
      <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
      <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>

  <listener>
    <listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
  </listener>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    classpath:applicationContext.xml
</param-value>
  </context-param>

 <resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/myAppName</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

</web-app>

Jersey 2.23.2 Web.xml configuration 

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.jsg.resource.initializer.RestResourceInitializer</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping> '
     
       <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
      <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>jdbc/myAppName</res-ref-name>
       <res-type>javax.sql.DataSource</res-type>
       <res-auth>Container</res-auth>
   </resource-ref>
</web-app>

RestResourceIntializer.java

package com.jsg.resource.initializer;

import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;

public class RestResourceInitializer extends Application {

/**
* Gets the classes.
*
* @return the classes
*/
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
 
// Resources
classes.add(org.glassfish.jersey.jackson.JacksonFeature.class);
classes.add(org.glassfish.jersey.server.spring.scope.RequestContextFilter.class);
classes.add(org.glassfish.jersey.media.multipart.MultiPartFeature.class);

               //Rest classes within Application.
                classes.add(com.jsg.rest.AbcRestService.class);

classes.add(com.jsg.rest.CdeRestService.class);
return classes;
}

}

Now if we would try to deploy this code with Jersey 2 Changes on Websphere(tried on 8.8.5),we will get following exception :

Caused by: java.lang.NoSuchMethodError: javax/ws/rs/core/Application.getProperties()Ljava/util/Map; at org.glassfish.jersey.server.ApplicationHandler.(ApplicationHandler.java:287) at org.glassfish.jersey.servlet.WebComponent.(WebComponent.java:311)

Reason for above exception is that,Websphere supports JAX-RS 1 implementation,however we are deploying Jersey 2 code which is Jax-rs 2 implementation.

How to resolve above exception 

So basically what we have to do is to force WebSphere to pick our Jersey 2 jars instead of default Jax-rs 1.We need to follow following steps for that 

1) Disable in built JAX-RS by setting following JVM property to true

    com.ibm.websphere.jaxrs.server.DisableIBMJAXRSEngine=true

   This property can be set through admin console of WebSphere by going to Servers->All Server ->  <ServerName> ->Server Infrastructure -> Java and Process Management ->Process Deifinition ->Additional Properties-> Java Virtual Machine ->Additional Properties-> Custom Properties




2) Create Isolated Shared Library having the Jersey 2 Jars and Spring 4 Jars
    Isolated shared library can be created through admin Console of Websphere by going to 
    Environment-> Shared Libraries ->New

    


   In the classpath box,we need to enter path of the folder on the server,where we have placed all        Jersey 2 and Spring 4 Jars

/var/was/server2/jerseyLib1/spring-context-4.3.4.RELEASE.jar
/var/was/server2/jerseyLib1/spring-core-4.3.4.RELEASE.jar
/var/was/server2/jerseyLib1/spring-beans-4.3.4.RELEASE.jar
/var/was/server2/jerseyLib1/spring-aop-4.3.4.RELEASE.jar
/var/was/server2/jerseyLib1/spring-web-4.3.4.RELEASE.jar
/var/was/server2/jerseyLib1/spring-expression-4.3.4.RELEASE.jar
/var/was/server2/jerseyLib1/spring-bridge-2.5.0-b05.jar
/var/was/server2/jerseyLib1/hk2-locator-2.5.0-b05.jar
/var/was/server2/jerseyLib1/hk2-api-2.5.0-b05.jar
/var/was/server2/jerseyLib1/hk2-utils-2.5.0-b05.jar
/var/was/server2/jerseyLib/javax.inject-2.5.0-b05.jar
/var/was/server2/jerseyLib1/javax.annotation-api-1.2-b03.jar
/var/was/server2/jerseyLib1/javax.ws.rs-api-2.0.1.jar
/var/was/server2/jerseyLib1/jersey-client-2.23.2.jar
/var/was/server2/jerseyLib1/jersey-spring3-2.23.2.jar
/var/was/server2/jerseyLib1/jersey-container-servlet-core-2.23.2.jar
/var/was/server2/jerseyLib1/jersey-server-2.23.2.jar
/var/was/server2/jerseyLib1/jersey-common-2.23.2.jar

/var/was/server2/jerseyLib1/jersey-guava-2.23.2.jar


Also in class loading section ,select "use an isolated class loader for this shared library"

and then finally click on Apply and Ok and we are done with creation of isolated shared library.



3. Bind this isolated shared library with your application war file as follows in admin Console

  a)  Application -> All Applications -> Click on your application name

 


  b) Go to References -> Shared Library References -> Reference Shared Libraries ->select your application war(Not ear) and click ok.


  c) Select the library that we created in Step 2 in "Available" combo box on left side and put it on right side in "Selected" combo box and click ok.



With this we have associated the isolated shared library with application war file.

4. Restart Server and application should be up and running.

Thanks for reading.Please let me know,if you have any comments,feedback or query or suggestion on this post.