在ubuntu 12.04 中配置java环境(安装jdk, tomcat, maven, eclipse)

时间:2022-06-11 20:20:42

1. 安装jdk 1.7

  在ubuntu( /usr/lib/jvm/java-7-openjdk-amd64 )中默认有安装jdk 1.7

  如果没有 可下载 : http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html

  解压 : sudo tar zxvf jdk-7u5-linux-x64.tar.gz -C /usr/lib/jvm

  修改环境变量 : sudo gedit /etc/profile

         在末尾添加:     

          # for java
          export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
          export JRE_HOME=${JAVA_HOME}/jre
          export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
          export PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin:$PATH

  source /etc/profile  //使配置生效 或重启电脑

  配置默认jdk版本

  sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-7-sun/bin/java 300

  sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/java-7-sun/bin/javac 300

  sudo update-alternatives --config java    //选择所需版本

  java -version    //检测jdk是否安装成功

  java version "1.7.0_85"
  OpenJDK Runtime Environment (IcedTea 2.6.1) (7u85-2.6.1-5ubuntu0.12.04.1)
  OpenJDK 64-Bit Server VM (build 24.85-b03, mixed mode)

2. 安装tomcat 7

  a. 下载包 : http://tomcat.apache.org/

  b. 解压到/opt下

  c. 修改文件配置 sudo gedit /opt/apache-tomcat-7.0.68/bin/startup.sh , 在末尾加上:

  #添加jdk和jre环境变量
  JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
  JRE_HOME=$JAVA_HOME/jre
  PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME
  CLASSPATH=.:$JRE_HOME/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
  TOMCAT_HOME=/opt/apache-tomcat-7.0.68

  d. 修改文件配置 sudo gedit /opt/apache-tomcat-7.0.68/bin/catalina.sh , 在开始加上:

  JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
  JAVA_OPTS="-server -Xms512m -Xmx1024m -XX:PermSize=600M -XX:MaxPermSize=600m -Dcom.sun.management.jmxremote"

  e. 启动tomcat: sudo sh /opt/apache-tomcat-7.0.68/bin/startup.sh

  Using CATALINA_BASE: /opt/apache-tomcat-7.0.68
  Using CATALINA_HOME: /opt/apache-tomcat-7.0.68
  Using CATALINA_TMPDIR: /opt/apache-tomcat-7.0.68/temp
  Using JRE_HOME: /usr/lib/jvm/java-7-openjdk-amd64
  Using CLASSPATH: /opt/apache-tomcat-7.0.68/bin/bootstrap.jar:/opt/apache-tomcat-7.0.68/bin/tomcat-juli.jar
  Tomcat started.

  f. 浏览器进入 : http://localhost:8080

  g. 关闭tomcat: sh /opt/apache-tomcat-7.0.68/bin/shutdown.sh

3. 安装maven    http://www.linuxidc.com/Linux/2013-05/84489.htm

  a. 下载: wget http://apache.fayea.com/apache-mirror/maven/maven-3/3.2.3/binaries/apache-maven-3.2.3-bin.tar.gz

  b. 解压: tar -xzf apache-maven-3.2.3-bin.tar.gz 在文件夹 /usr/local/*** 下

  c. 设置环境变量: sudo gedit /etc/profile

  # set maven environment
  export M2_HOME=/usr/local/apache-maven-3.3.9
  export M2=$M2_HOME/bin
  export MAVEN_OPTS="-Xms256m -Xmx512m"
  export PATH=$M2:$PATH

  d. 使环境生效 : source /etc/profile

  e. 测试是否成功: mvn --version

  Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)
  Maven home: /usr/local/apache-maven-3.3.9
  Java version: 1.7.0_85, vendor: Oracle Corporation
  Java home: /usr/lib/jvm/java-7-openjdk-amd64/jre
  Default locale: en_US, platform encoding: UTF-8
  OS name: "linux", version: "3.13.0-71-generic", arch: "amd64", family: "unix"

4. 安装eclipse

  a. 下载: http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/mars2

  b. 解压文件

  $ sudo mv eclipse-cpp-juno-M7-linux-gtk.tar.gz /usr/lib/
  $ cd /usr/lib/
  $ sudo tar -zxvf eclipse-cpp-juno-M7-linux-gtk.tar.gz
  $ sudo rm ./eclipse-cpp-juno-M7-linux-gtk.tar.gz

  c. 创建启动快捷方式  sudo gedit /usr/share/applications/eclipse.desktop

  内容如下:
  [Desktop Entry]
  Type=Application
  Name=Eclipse
  Comment=Eclipse Integrated Development Environment
  Icon=/usr/lib/eclipse/icon.xpm
  Exec=/usr/lib/eclipse/eclipse
  Terminal=false
  Categories=Development;IDE;Java;

  d: 配置jdk : windows-->preferences-->java-->Installed

  e. 配置tomcat: windows-->preferences-->server