Ubuntu + Eclipse + Tomcat 配置 Java Web 開發環境填坑筆記
前言
從 Eclipse 官網 上下載到的 Eclipse Neon 是不帶外掛的, 要配置出一個 Java Web 開發環境, 尚需各種折騰. 把踩到的坑記錄一下備忘.
Eclipse 基本配置:
使用 shadow sock 科學上網,需要配置代理: Window -> Preferences -> General -> Network Connections
Help -> Install New Software, 反選 “Contact all update sites during install to find required software”
Eclipse 外掛:
Help -> Install New Software 如下地址:
http://download.eclipse.org/releases/neon/
選擇安裝如下外掛:
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 WST Server Adapters m2e-wtp - Maven Integration for WTP
Preference 中沒有 Server -> Runtime Environments 選項
需要安裝 JST Server Adapters
和 JST Server Adapters Extentions
. 步驟:
- Help -> Install New Software
- 選擇
Neon - http://download.eclipse.org/releases/neon
- Expand “Web, XML, and Java EE Development”
- 找到並勾選 JST Server Adapters
配置 Server -> Runtime Environments
- Windows → Preferences -> Server -> Runtime Environments
- 點選 “Add”, 選擇要執行的 tomcat 版本, Next
- 配置 tomcat 路徑. 用
apt-get install
安裝的 tomcat 這個路徑配置/usr/share/tomcat7/
- “OK”, “Finish”
Eclipse 底部沒有 Servers 視窗
手動新增:
Windows -> Show View -> Other -> 查詢 servers -> OK
Ubuntu Tomcat 路徑
如果是用 apt-get install tomcat7
安裝, 重要路徑有:
/etc/tomcat{X} for configuration
/usr/share/tomcat{X} for runtime, called CATALINA_HOME
/usr/share/tomcat{X}-root for webapps
/var/lib/tomcat{X}
如果 tomcat 在 eclipse 中始終無法啟動, 嘗試如下命令:
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 logs
sudo ln -s /var/lib/tomcat7/webapps webapps
sudo chmod -R 777 /usr/share/tomcat7/conf
可用如下命令檢視 tomcat 的安裝目錄:
dpkg -L tomcat7
經過如上配置, Eclipse 中的 Windows -> Preferences -> Tomcat 設定中可將 Tomcat 路徑配置為 /usr/share/tomcat7
Eclipse Tomcat plugin 安裝
專案 web deployment assembly 配置 maven 依賴
執行專案時遇到下面問題:
SEVERE: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
解決:
- 右擊專案名稱並在彈出選單中選擇 “Properties”.
- 選擇 “Deployment Assembly”.
- 點選 “Add…”.
- 在 Directive Type 選單中選擇 “Java Build Path Entries”, 點選 “Next”.
- 在 Java Build Path Entries 選單中選擇 “Maven Dependencies”, 點選 “Finish”.
Eclipse Console Log 寫入本地檔案
Run -> Run Configurations -> Tomcat Server -> Common -> Standard Input and Output -> Output File
在輸入框內填入一個本地檔案地址即可. 比如 /tmp/my_project_console_log
未來就可以愉快的使用 tail -f /tmp/my_project_console_log
在命令列看 log 了.
禁止 tomcat 開機自啟
update-rc.d -f tomcat7 remove
Eclipse 自動程式碼提示配置
Windows -> Preferences -> Java -> Editor -> Content Assist
AutoActivation Delay 改為 20
Auto Activation triggers for java 改為 .abcdefghijklmnopqrstuvwxyz(,
Eclipse 格式化程式碼配置
Windows -> Preferences -> Java -> Code Style -> Formatter
格式化程式碼快捷鍵 Shift + Ctrl + F
Eclipse Customize Perspective StackOverflowError
想要配置 toolbar , 開啟 Windows -> Perspective -> Customize Perspective
Eclipse 提示遇到問題, 原因是 StackOverflowError.
wget 下載 JDK
寫這個是因為這裡有個坑, 用 wget 不帶引數直接下載 JDK 會失敗. 步驟:
首先到官網找到最新的 JDK 下載地址, 找到需要的 JDK 後, 記得先點 “Accept License Agreement”, 再複製下載連結才能複製的到.
有了下載地址, 使用 wget
命令下載 JDK 時需要加一些引數才可以成功:
wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u112-b15/jdk-8u112-linux-x64.rpm
把上面的 JDK 下載地址換成你想要的 JDK 地址就可以了.