Skip to content

Instantly share code, notes, and snippets.

@ozooxo
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozooxo/f87c5cb4d3028cc1bdf7 to your computer and use it in GitHub Desktop.
Save ozooxo/f87c5cb4d3028cc1bdf7 to your computer and use it in GitHub Desktop.
Setup Tomcat7 + Eclipse Luna (ubuntu 14.04)

Tomcat7

Setup tomcat7 is easy. Just do

sudo apt-get install tomcat7

and http://localhost:8080/ will work already. The default source files are in /usr/share/tomcat7, while the default path is in /var/lib/tomcat7/webapps/ROOT.

Eclipse Luna

First install those five packages from Help > Install New Software... > (Work with) Luna > Web, XML, Java EE and OSGi Enterprise development.

Eclipse Java EE Developer Tools
Eclipse Java Web Developer Tools
Eclipse Web Developer Tools
JST Server Adapters
JST Server Adapters Extensions

Goes to Windows > Performances > Server > Runtime Environment and add the one just installed, with Tomcat installation directory as /usr/share/tomcat7. It is also possible to install a local version of tomcat7 using download and install and specify folder path; however, what is installed is just identical duplications and there's not much differences.

Goes to File > New > Others > Server > server and add a new Tomcat v7.0 server, choose the server runtime environment we just set up. If we setup/remove several times, sometimes it happens that the Server Name is blank, and there's no way to type anything in it. It is a known bug in Eclipse. Just do

cd ~/workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/    
rm org.eclipse.jst.server.tomcat.core.prefs    
rm org.eclipse.wst.server.core.prefs

then everything is reset.

The Java EE (rather than Java) prospective is needed to be switched to.

Change the privacy policy of /usr/share/tomcat7 so Eclipse (and other normal users) can use it.

cd /usr/share/tomcat7
sudo ln -s /var/lib/tomcat7/conf conf
sudo ln -s /etc/tomcat7/policy.d/03catalina.policy conf/catalina.policy
sudo ln -s /var/log/tomcat7 log
sudo chmod -R 777 /usr/share/tomcat7/conf

Copy all files in /usr/share/tomcat7/conf (inside folders seems unnecessary because they are backups) to ~/workspace/Servers/Tomcat v7.0 Server at localhost-config. Then either stop the original Tomcat by

sudo service tomcat7 stop
sudo update-rc.d tomcat7 disable

Or choose another port for the Ecplise instance (I am choosing this way) by changing 8080 to 8181 (or whatever unused port) and 8005 to 8115 in ~/workspace/Servers/Tomcat v7.0 Server at localhost-config/server.xml.

The Servers view can be shown from Windows > Show View > Others > Servers, and right now it should be started successfully. Right now http://localhost:8181/ will gives 404 errors.

Test Servlet Project

Just use File > New > Others > Web > Dynamical Web Project wizard to start a new servlet project. Put whatever servlet code into /src folder by adding File > New > Others > Web > Servlet class. Eclipse is complaining import errors. Go to project Properties > Java Build Path > Libraries > Add External Jars... and add all the things in /usr/share/tomcat7/lib in it. Then Run on Server will work, and the page can be viewed from http://localhost:8181/project_name/ClassName.

在eclipse下,對external package/library的setup也是類似。

對於eclipse Java EE的dynamical web project,去project的Properties > Project Facets,確認Dynamical Web Module以被勾選(這個應該是直接被系統設好了),並版本是3.0(或如果2.5,就要用web.xml而不是寫@WebServlet("/cell"))。對於external library,不妨把.jar全部放在WebContent/WEB-INF/lib,然後加入Java Build Path > Libraries。把其它要引用的Eclipse project加入Java Build Path > Projects。如此source code上面的紅色虛線就會消失。對於執行,去Deployment Assembly並手動修改確認其它project到WEB-INF/lib/*.jar以及WebContent/WEB-INF/lib下的內容都到WEB-INF/lib/

eclipse Java EE的dynamical web project這邊,對wizard一走完就能跑(如果沒有複雜的外部lib設置)的支援比較好。相比如果用maven就沒有那麼好弄。但是由於這邊不太是需要生成一個.jar(而最終是要把那些file放在server對應的位置),所以最終也不能用maven的.m2下面的library搞定。由此,其實maven也沒有太多優勢。

對maven,選New > Others > New Maven Project然後用maven-archetype-webapp那個。然後一開始不轉,像Facets那種都要手改。大概要改/確認的,還是前面說的那些項。網上有人說要去執行

$ mvn eclipse:eclipse -Dwtpversion=2.0

但似乎那邊會手動改Facets下面還有別處的一堆版本號,而那些號碼要全部對上才能執行成功。反正很麻煩就是了。

P.S

此外如果lib下缺libraries,start Tomcat是沒有問題,但是brower執行的時候就會報一海票錯誤。這邊debugging很不方便。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment