下载及安装

下载并安装Java SE,下载对应版本的Tomcat,版本对应关系tomcat给出了文档. 我下载之后把apache-tomcat-7.0.75.zip放在Application目录下,方便起见改名为tomcat。mac应该下载tar.gz文件,zip文件会报错。
查看java版本

1
2
3
4
java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

JAVA_HOME配置

检查JAVA_HOME
在terminal中查看JAVA_HOME地址

1
2
/ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/

本机下载安装的是Java1.8.0,JAVA_HOME地址有错,可以手动修改。

1
sudo vim /etc/profile

然后按下i进入vim的插入模式,在文件尾部修改JAVA_HOME路径

1
2
3
4
JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/"
export JAVA_HOME
CLASS_PATH="$JAVA_HOME/lib"
PATH=".$PATH:$JAVA_HOME/bin"

然后:wq!保存退出。想要路径马上生效,输入

1
source /etc/profile

然后查看JAVA_HOME路径,或者cd到具体路径验证

1
cd $JAVE_HOME

运行tomcat服务

cd到tomcat的bin目录下

1
./catalina.sh run

如果JAVA_HOME有错误,会抛出如下问题。(修改方法见上文)

1
2
3
4
5
6
7
8
bin ./catalina.sh run
Using CATALINA_BASE: /Users/zuochenyu/.Trash/tomcat
Using CATALINA_HOME: /Users/zuochenyu/.Trash/tomcat
Using CATALINA_TMPDIR: /Users/zuochenyu/.Trash/tomcat/temp
Using JRE_HOME: /Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/
Using CLASSPATH: /Users/zuochenyu/.Trash/tomcat/bin/bootstrap.jar:/Users/zuochenyu/.Trash/tomcat/bin/tomcat-juli.jar
./catalina.sh: line 366: /Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home//bin/java: No such file or directory
./catalina.sh: line 366: exec: /Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home//bin/java: cannot execute: No such file or directory

如果出现错误

1
permission denied: ./catalina.sh

首先请pwd检查当前路径是否正确,然后检查tomcat的版本号是否与java版本相对应。


查看网页

tomcat默认的端口是8080,在浏览器中打开http://localhost:8080/
看到的页面应该是这样
tomcat


关闭服务

在terminal中command+c或者在tomcatbin目录下输入

1
./shutdown.sh

Reference

[1] Mac OS X 搭建Java环境
[2] How to Install Apache Tomcat 8 (on Windows, Mac OS, Ubuntu) and Get Started with Java Servlet Programming
[3] StackOverFlow: IntelliJ says ‘cannot run program ‘/path/to/tomcat/bin/catalina.sh’ error=13 permission denied