Set up Eclipse and Tomcat 7 on Linux to create Java RESTful Web Services with Jersey

  1. Install Eclipse and Tomcat 7 by running in a terminal:
    sudo apt-get install eclipse tomcat7 -y
  2. assign the tomcat7 group to your user. Assuming your username is pippo, run:
    sudo adduser pippo tomcat7

    Note: you need to log out and log back in for the changes to take effect.

  3. stop the system-wide Tomcat by running:
    /usr/share/tomcat7/bin/shutdown.sh
  4. prevent the system-wide Tomcat from automatically starting at boot by running:
    sudo update-rc.d tomcat7 disable
  5. install WTP (Web Tools Platform) by running Eclipse and then:
    1. from then menu Help select Install New Softare…
    2. next to Work with: open the drop down menu and select the update site of your Eclipse version
    3. select Web, XML, Java EE Development and OSGi Enterprise Development, press Next and complete the installation;
  6. to configure WTP to use Tomcat 7:
    1. select Windows -> Preferences -> Server -> Runtime Environments;
    2. press Add…;
    3. select “Apache Tomcat v7.0″;
    4. enter “/usr/share/tomcat7″ into the “Tomcat installation directory” field;
    5. press Ok;
  7. create a new “Dynamic Web Project”
    • as Target Runtime select “Apache Tomcat v7.0″;
    • press twice Next;
    • select the “Generate the web.xml deployment descriptor” option at the final dialog;
    • press Finish;
  8. open the Servers view:
    • go to Window->Show View->Other…;
    • choose the Servers under the Server category;
  9. click on new server wizard;
    • choose Apache / Tomcat v7.0 Server and press Next;
    • enter “/usr/share/tomcat7″ into the “Tomcat installation directory” field;
    • press Next;
    • select your project on the left pane under “Available” and press Add> to move it to the right pane under “Configured”;
    • press Finish”;
  10. download the zip of Jersey;
  11. put the the jersey jars into the folder “WEB-INF/lib” and add them to the project build path;
  12. create your first RESTful Webservice by following this tutorial by Lars Vogel.

Your web services will be automatically deployed in the following folder:

~/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/

Your web service will be accessible through the following URI:

http://localhost:8080/UrlPattern/ClassPath/methodPath

Where:

  1. UrlPattern = the root of your service set inside the tag url-pattern in the web.xml;
  2. ClassPath = the path set for your class
  3. methodPath = the path set for your method

All the files you put in your WebContent folder will be accessible via http through the following URI:

http://localhost:8080/UrlPattern/yourfile

Note: make sure the tag url-pattern in the web.xml is set to something like this:

<url-pattern>/rest/*</url-pattern>

and not something like this:

<url-pattern>/*</url-pattern>

Troubleshooting

  • to fix the error “Cannot create a server using the selected type” assign the tomcat7 group to your user. Assuming your username is pippo, run:
    sudo adduser pippo tomcat7
    cd ~/workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/    
    rm org.eclipse.jst.server.tomcat.core.prefs    
    rm org.eclipse.wst.server.core.prefs

    Alternatively you can try this other solution.

  • to fix the error “Could not load the Tomcat server configuration at /usr/share/tomcat7/conf. The configuration may be corrupt or incomplete. /usr/share/tomcat7/conf/catalina.policy (No such file or directory)” assign the tomcat7 group to your user. Assuming your username is pippo, run:
    sudo adduser pippo tomcat7
  • to fix the error “Could not load the Tomcat server configuration at /Servers/Tomcat v7.0 Server at localhost-config. The configuration may be corrupt or incomplete” assign the tomcat7 group to your user. Assuming your username is pippo, run:
    sudo adduser pippo tomcat7
About these ads

40 thoughts on “Set up Eclipse and Tomcat 7 on Linux to create Java RESTful Web Services with Jersey

  1. am not getting the ‘server’ option at Windows -> Preferences -> Server -> Runtime Environments;
    help me please

    1. go to help->install new software->all available sites and install web option’s web tool in platform by selecting WST Server Adapters.( ull get in one minites)

  2. Thank you for this great Guide. This is absolutely straightforward and it was easy to follow and repeat each step. It solved instantly my issues with connecting Eclipse to Tomcat using Ubuntu 12.04 LTS. In step 7 and 8 I got no errors, though I could not connect. After trying your terminal commands in step seven to eight it finally worked. Thanks again. =)

  3. Thanks a lot , but could you pleas eprovide, why do we need to set sudo chmod -R 777 /usr/share/tomcat7/conf for conf, is it security safety?

  4. enter “/usr/share/tomcat7″ into the “Tomcat installation directory” field;
    press Next;…after this i cant go with NEXT button…pls help me

  5. Thanks a lot! I spent an hour trying to configure the server on Eclipse. Your link approach worked. Cheers.

  6. This error keeps popping up each time I try to start the server from Eclipse: “Could not load the Tomcat server configura… …tion may be corrupt or incomplete”. I copied /etc/tomcat7 contents into the Eclipse’s Server’s configuration directory (the one usually in ~/workspace/Servers/Tomcat v7.0…..) and I make myself owner of /etc/tomcat7 and /usr/share/tomcat7 recursively. What am I missing? Thanks

  7. Hey, I am still getting the “Could not load the Tomcat server configuration at /usr/share/tomcat7/conf. The configuration may be corrupt or incomplete.
    /usr/share/tomcat7/conf/catalina.policy (No such file or directory)” problem when I start the new server wizard. I have added my username to the tomcat7 group. Any thoughts?

    1. If you have installed both tomcat 6 and 7 then uninstall tomcat6 and then try to run .. it should work

    2. Also do the following steps :
      1) go to the directory through terminal : /usr/share/tomcat7
      2) sudo ln -s /var/lib/tomcat7/conf conf
      3) sudo ln -s /etc/tomcat7/policy.d/03catalina.policy conf/catalina.policy
      4) sudo chmod -R 777 /usr/share/tomcat7/conf

    3. I had the same problem. But I fixed with this:

      1) go to the directory through terminal : /usr/share/tomcat7
      2) sudo ln -s /var/lib/tomcat7/conf conf
      3) sudo ln -s /etc/tomcat7/policy.d/03catalina.policy conf/catalina.policy
      4) sudo chmod -R 777 /usr/share/tomcat7/conf

  8. Hi, All the steps worked except the “press Finish” ]on step 9… Bummer. I’ve tried all the little steps suggested as a fix to it. Still fails :-(

  9. This was really helpful. I suggest you be more explicit in step 9 about how to launch the wizard, though. Also, there didn’t seem to be any downloads available at the site you linked to in step 10. But thanks!

  10. Hi, I followed all steps mentioned above steps but still getting error in web page
    Please assist me i am facing this problem from one week but didn’t any solution

    ””

    HTTP Status 404 – /Demo/

    type Status report

    message /Demo/

    description The requested resource (/Demo/) is not available.
    Apache Tomcat/7.0.26

  11. Hi,
    enter “/usr/share/tomcat7″ into the “Tomcat installation directory” field;
    above step is not working for me. Its showing invalid directory. But the path is correct

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s