博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ubuntu下面的Java开发环境的搭建
阅读量:6912 次
发布时间:2019-06-27

本文共 11710 字,大约阅读时间需要 39 分钟。

hot3.png

一、Java运行环境的搭建

从网上搜索“jdk”,从官网上下载最新版本的Linux的jre的二进制包,解压就能用的。不用多说,自己查百度就能知道了。提取出来的jre文件夹建议放在/etc/文件夹下面,/etc/是许多Linux下的软件包部署的环境。下面就是关键的搭建关键变量。

配置系统环境变量,编辑/etc/profile文件,在文件的末尾添加一下信息:

export JAVA_HOME=/etc/jdk-8     export JRE_HOME=$JAVA_HOME/jre export CLASSPATH=.:$CLASSPATH:$JAVA_HOME/lib:$JRE_HOME/libexport PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin

解释一下,上面几行代码:

export JAVA_HOME=/etc/jdk-8                                   是配置jdk的主目录export JRE_HOME=$JAVA_HOME/jre                                是配置jre的目录export CLASSPATH=.:$CLASSPATH:$JAVA_HOME/lib:$JRE_HOME/lib    是配置的CLASSPATH目录export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin                将jdk的可执行文件目录添加到系统系统环境目录中

如下图所示:

在命令窗口中执行下面的命令,使得编辑生效:

source /etc/profile

在terminal中验证一下环境是否成功:

二、部署Maven

在/etc/profile下添加环境变量

# Maven_Homeexport	M2_HOME=/etc/apache-maven-3.5.0export	PATH=$M2_HOME/bin:$PATH

重新启动系统,或者在终端下输入,使得刚才的编辑生效:

source /etc/profile

验证环境变量是否存在:

echo $M2_HOMEecho $PATH

验证Maven配置是否成功:

不修改配置的话,下载的文件都会保存到本地仓库~/.m2/repositoryMaven中。也可以自己指定代码的仓库。

另外,国内下载最好指定阿里云的Maven仓库。

在maven的settings.xml 文件里配置mirrors的子节点,添加如下mirror。

nexus-aliyun
*
Nexus aliyun
http://maven.aliyun.com/nexus/content/groups/public

三、Tomcat环境的搭建

将解压的文件夹部署到/etc/文件夹下,在tomcat的文件夹下面的RUNNING.txt文件里面讲解了部署的流程。

Apache Tomcat 7.0 requires a Java Standard Edition RuntimeEnvironment (JRE) version 6 or later.=============================Running With JRE 6 Or Later=============================(1) Download and Install a Java SE Runtime Environment (JRE)(1.1) Download a Java SE Runtime Environment (JRE),      release version 6 or later, from      http://www.oracle.com/technetwork/java/javase/downloads/index.html(1.2) Install the JRE according to the instructions included with the      release.      You may also use a full Java Development Kit (JDK) rather than just      a JRE.(2) Download and Install Apache Tomcat(2.1) Download a binary distribution of Tomcat from:      http://tomcat.apache.org/(2.2) Unpack the binary distribution so that it resides in its own      directory (conventionally named "apache-tomcat-[version]").      For the purposes of the remainder of this document, the name      "CATALINA_HOME" is used to refer to the full pathname of that      directory.NOTE:  As an alternative to downloading a binary distribution, you cancreate your own from the Tomcat source code, as described in"BUILDING.txt".  You can either  a)  Do the full "release" build and find the created distribution in the      "output/release" directory and then proceed with unpacking as above, or  b)  Do a simple build and use the "output/build" directory as      "CATALINA_HOME".  Be warned that there are some differences between the      contents of the "output/build" directory and a full "release"      distribution.(3) Configure Environment VariablesTomcat is a Java application and does not use environment variables directly.Environment variables are used by the Tomcat startup scripts. The scripts usethe environment variables to prepare the command that starts Tomcat.(3.1) Set CATALINA_HOME (required) and CATALINA_BASE (optional)The CATALINA_HOME environment variable should be set to the location of theroot directory of the "binary" distribution of Tomcat.The Tomcat startup scripts have some logic to set this variableautomatically if it is absent, based on the location of the startup scriptin *nix and on the current directory in Windows. That logic might not workin all circumstances, so setting the variable explicitly is recommended.The CATALINA_BASE environment variable specifies location of the rootdirectory of the "active configuration" of Tomcat. It is optional. Itdefaults to be equal to CATALINA_HOME.Using distinct values for the CATALINA_HOME and CATALINA_BASE variables isrecommended to simplify further upgrades and maintenance. It is documentedin the "Multiple Tomcat Instances" section below.(3.2) Set JRE_HOME or JAVA_HOME (required)These variables are used to specify location of a Java RuntimeEnvironment or of a Java Development Kit that is used to start Tomcat.The JRE_HOME variable is used to specify location of a JRE. The JAVA_HOMEvariable is used to specify location of a JDK.Using JAVA_HOME provides access to certain additional startup options thatare not allowed when JRE_HOME is used.If both JRE_HOME and JAVA_HOME are specified, JRE_HOME is used.The recommended place to specify these variables is a "setenv" script. Seebelow.(3.3) Other variables (optional)Other environment variables exist, besides the four described above.See the comments at the top of catalina.bat or catalina.sh scripts forthe list and a description of each of them.One frequently used variable is CATALINA_OPTS. It allows specification ofadditional options for the java command that starts Tomcat.See the Java documentation for the options that affect the Java RuntimeEnvironment.See the "System Properties" page in the Tomcat Configuration Reference forthe system properties that are specific to Tomcat.A similar variable is JAVA_OPTS. It is used less frequently. It allowsspecification of options that are used both to start and to stop Tomcat as wellas for other commands.Note: Do not use JAVA_OPTS to specify memory limits. You do not need muchmemory for a small process that is used to stop Tomcat. Those settingsbelong to CATALINA_OPTS.Another frequently used variable is CATALINA_PID (on *nix only). Itspecifies the location of the file where process id of the forked Tomcatjava process will be written. This setting is optional. It will enable thefollowing features: *  better protection against duplicate start attempts and *  allows forceful termination of Tomcat process when it does not react to    the standard shutdown command.(3.4) Using the "setenv" script (optional, recommended)Apart from CATALINA_HOME and CATALINA_BASE, all environment variables canbe specified in the "setenv" script. The script is placed either intoCATALINA_BASE/bin or into CATALINA_HOME/bin directory and is namedsetenv.bat (on Windows) or setenv.sh (on *nix). The file has to bereadable.By default the setenv script file is absent. If the script file is presentboth in CATALINA_BASE and in CATALINA_HOME, the one in CATALINA_BASE ispreferred.For example, to configure the JRE_HOME and CATALINA_PID variables you cancreate the following script file:On Windows, %CATALINA_BASE%\bin\setenv.bat:  set "JRE_HOME=%ProgramFiles%\Java\jre6"  exit /b 0On *nix, $CATALINA_BASE/bin/setenv.sh:  JRE_HOME=/usr/java/latest  CATALINA_PID="$CATALINA_BASE/tomcat.pid"The CATALINA_HOME and CATALINA_BASE variables cannot be configured in thesetenv script, because they are used to locate that file.All the environment variables described here and the "setenv" script areused only if you use the standard scripts to launch Tomcat. For example, ifyou have installed Tomcat as a service on Windows, the service wrapperlaunches Java directly and does not use the script files.(4) Start Up Tomcat(4.1) Tomcat can be started by executing one of the following commands:  On Windows:      %CATALINA_HOME%\bin\startup.bat    or      %CATALINA_HOME%\bin\catalina.bat start  On *nix:      $CATALINA_HOME/bin/startup.sh    or      $CATALINA_HOME/bin/catalina.sh start(4.2) After startup, the default web applications included with Tomcat will be      available by visiting:      http://localhost:8080/(4.3) Further information about configuring and running Tomcat can be found in      the documentation included here, as well as on the Tomcat web site:      http://tomcat.apache.org/(5) Shut Down Tomcat(5.1) Tomcat can be shut down by executing one of the following commands:  On Windows:      %CATALINA_HOME%\bin\shutdown.bat    or      %CATALINA_HOME%\bin\catalina.bat stop  On *nix:      $CATALINA_HOME/bin/shutdown.sh    or      $CATALINA_HOME/bin/catalina.sh stop==================================================Advanced Configuration - Multiple Tomcat Instances==================================================In many circumstances, it is desirable to have a single copy of a Tomcatbinary distribution shared among multiple users on the same server.  To makethis possible, you can set the CATALINA_BASE environment variable to thedirectory that contains the files for your 'personal' Tomcat instance.When running with a separate CATALINA_HOME and CATALINA_BASE, the filesand directories are split as following:In CATALINA_BASE: * bin  - Only the following files:           * setenv.sh (*nix) or setenv.bat (Windows),           * tomcat-juli.jar          The setenv scripts were described above. The tomcat-juli library          is documented in the Logging chapter in the User Guide. * conf - Server configuration files (including server.xml) * lib  - Libraries and classes, as explained below * logs - Log and output files * webapps - Automatically loaded web applications * work - Temporary working directories for web applications * temp - Directory used by the JVM for temporary files (java.io.tmpdir)In CATALINA_HOME: * bin  - Startup and shutdown scripts          The following files will be used only if they are absent in          CATALINA_BASE/bin:          setenv.sh (*nix), setenv.bat (Windows), tomcat-juli.jar * lib  - Libraries and classes, as explained below * endorsed - Libraries that override standard "Endorsed Standards"              libraries provided by JRE. See Classloading documentation              in the User Guide for details.              By default this "endorsed" directory is absent.In the default configuration the JAR libraries and classes both inCATALINA_BASE/lib and in CATALINA_HOME/lib will be added to the commonclasspath, but the ones in CATALINA_BASE will be added first and thus willbe searched first.The idea is that you may leave the standard Tomcat libraries inCATALINA_HOME/lib and add other ones such as database drivers intoCATALINA_BASE/lib.In general it is advised to never share libraries between web applications,but put them into WEB-INF/lib directories inside the applications. SeeClassloading documentation in the User Guide for details.It might be useful to note that the values of CATALINA_HOME andCATALINA_BASE can be referenced in the XML configuration files processedby Tomcat as ${catalina.home} and ${catalina.base} respectively.For example, the standard manager web application can be kept inCATALINA_HOME/webapps/manager and loaded into CATALINA_BASE by usingthe following trick: * Copy the CATALINA_HOME/webapps/manager/META-INF/context.xml   file as CATALINA_BASE/conf/Catalina/localhost/manager.xml * Add docBase attribute as shown below.The file will look like the following:  
See Deployer chapter in User Guide and Context and Host chapters in theConfiguration Reference for more information on contexts and webapplication deployment.

无奈英语看起来比较吃力,在/etc/apache-tomcat-7.0.81/bin目录下的startup.sh里面加上几行代码,设置变量:

# tomcat on ubuntuJAVA_HOME=/etc/jdk-9JRE_HOME=$JAVA_HOME/jreCLASSPATH=.:$CLASSPATH:$JAVA_HOME/lib:$JRE_HOME/libPATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/binTOMCAT_HOME=/etc/apache-tomcat-7.0.81

对应的图片如下:

还要修改下文件的权限,才能执行,我偷懒了,文件夹下面都设置为777,最后得到的结果如下:

另外,补充一点,如果想要用termianl命令行的方式去关闭tomcat服务器,还要在shutdown.sh文件里面增加上面的环境变量,补充上去即可,否则会出现这样的提示。

问题1:

tomcat刚刚安装启动失败出现的问题提示:

Neither the JAVA_HOME nor the JRE_HOME environment variable is definedAt least one of these environment variable is needed to run this program

解决问题的思路:

tomcat/bin/catalina.sh (根据你自己的jdk路径进行修改) 在文件的正文开头,即正式代码前,大概在99行添加如下代码:

export JAVA_HOME=/usr/local/jdkexport JRE_HOME=/usr/local/jdk/jre

启动成功了!

问题2:

在/etc/profile中添加环境变量后,是使用source /etc/profile编译后只能在当前终端生效

重新开启一个终端后,该环境变量失效。

解决问题的思路:

重启系统:reboot,问题解决

转载于:https://my.oschina.net/feanlau/blog/1544440

你可能感兴趣的文章
Loadrunner监控Centos
查看>>
转:问题解决:The project cannot be built until build path errors are resolved
查看>>
Python--day25--面向对象之多态
查看>>
submit
查看>>
数据结构-----树状数组
查看>>
那些年我们一起追逐的多线程(Thread、ThreadPool、委托异步调用、Task/TaskFactory、Parallerl、async和await)...
查看>>
测试DOM0级事件和DOM2级事件的堆叠
查看>>
违章查询源码分享
查看>>
mac终端下svn常用命令
查看>>
C++的lambda表达式
查看>>
新手学习python(十六)封装redis
查看>>
vue移动端弹框组件
查看>>
vuex
查看>>
vux 全局使用 loading / toast / alert
查看>>
面向对象数组操作
查看>>
【杂题】[LibreOJ #6608] 无意识的石子堆【容斥原理】【FFT】
查看>>
jq与原生js实现收起展开效果
查看>>
JS开发中自定义调试信息开关
查看>>
p3302 [SDOI2013]森林(树上主席树+启发式合并)
查看>>
我的 FPGA 学习历程(06)—— 二进制转格雷码
查看>>