栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

linux 配置java编译环境

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

linux 配置java编译环境

yum方式下载安装

1、查找java相关的列表

yum -y list java*

[root@10-60-140-218 tools]# yum -y list java*
Loaded plugins: fastestmirror, langpacks, versionlock
Loading mirror speeds from cached hostfile
Excluding 1 update due to versionlock (use "yum versionlock status" to show it)
Installed Packages
java-1.8.0-openjdk.x86_64                                        1:1.8.0.312.b07-1.el7_9                     @updates
java-1.8.0-openjdk-devel.x86_64                                  1:1.8.0.312.b07-1.el7_9                     @updates
java-1.8.0-openjdk-headless.x86_64                               1:1.8.0.312.b07-1.el7_9                     @updates
javapackages-tools.noarch                                        3.4.1-11.el7                                @base   
Available Packages
java-1.6.0-openjdk.x86_64                                        1:1.6.0.41-1.13.13.1.el7_3                  base    
java-1.6.0-openjdk-demo.x86_64                                   1:1.6.0.41-1.13.13.1.el7_3                  base    
java-1.6.0-openjdk-devel.x86_64                                  1:1.6.0.41-1.13.13.1.el7_3                  base    
java-1.6.0-openjdk-javadoc.x86_64                                1:1.6.0.41-1.13.13.1.el7_3                  base    
java-1.6.0-openjdk-src.x86_64                                    1:1.6.0.41-1.13.13.1.el7_3                  base    
java-1.7.0-openjdk.x86_64                                        1:1.7.0.261-2.6.22.2.el7_8                  base    
java-1.7.0-openjdk-accessibility.x86_64                          1:1.7.0.261-2.6.22.2.el7_8                  base    
java-1.7.0-openjdk-demo.x86_64                                   1:1.7.0.261-2.6.22.2.el7_8                  base    
java-1.7.0-openjdk-devel.x86_64                                  1:1.7.0.261-2.6.22.2.el7_8                  base    
java-1.7.0-openjdk-headless.x86_64                               1:1.7.0.261-2.6.22.2.el7_8                  base    
java-1.7.0-openjdk-javadoc.noarch                                1:1.7.0.261-2.6.22.2.el7_8                  base    
java-1.7.0-openjdk-src.x86_64                                    1:1.7.0.261-2.6.22.2.el7_8                  base    
java-1.8.0-openjdk.i686                                          1:1.8.0.312.b07-1.el7_9                     updates 
java-1.8.0-openjdk-accessibility.i686                            1:1.8.0.312.b07-1.el7_9                     updates 
java-1.8.0-openjdk-accessibility.x86_64                          1:1.8.0.312.b07-1.el7_9                     updates 
java-1.8.0-openjdk-demo.i686                                     1:1.8.0.312.b07-1.el7_9                     updates 
java-1.8.0-openjdk-demo.x86_64                                   1:1.8.0.312.b07-1.el7_9                     updates 
java-1.8.0-openjdk-devel.i686                                    1:1.8.0.312.b07-1.el7_9                     updates 
java-1.8.0-openjdk-headless.i686                                 1:1.8.0.312.b07-1.el7_9                     updates 
java-1.8.0-openjdk-javadoc.noarch                                1:1.8.0.312.b07-1.el7_9                     updates 
java-1.8.0-openjdk-javadoc-zip.noarch                            1:1.8.0.312.b07-1.el7_9                     updates 
java-1.8.0-openjdk-src.i686                                      1:1.8.0.312.b07-1.el7_9                     updates 
java-1.8.0-openjdk-src.x86_64                                    1:1.8.0.312.b07-1.el7_9                     updates 
java-11-openjdk.i686                                             1:11.0.13.0.8-1.el7_9                

或者

yum search jdk

[root@10-60-140-218 tools]# yum search jdk
Loaded plugins: fastestmirror, langpacks, versionlock
Loading mirror speeds from cached hostfile
Excluding 1 update due to versionlock (use "yum versionlock status" to show it)
================================================= N/S matched: jdk ==================================================
copy-jdk-configs.noarch : JDKs configuration files copier
java-1.6.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.6.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.6.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.6.0-openjdk-javadoc.x86_64 : OpenJDK API documentation
java-1.6.0-openjdk-src.x86_64 : OpenJDK Source Bundle
java-1.7.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.7.0-openjdk-accessibility.x86_64 : OpenJDK accessibility connector
java-1.7.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.7.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.7.0-openjdk-headless.x86_64 : The OpenJDK runtime environment without audio and video support
java-1.7.0-openjdk-javadoc.noarch : OpenJDK API documentation
java-1.7.0-openjdk-src.x86_64 : OpenJDK Source Bundle
java-1.8.0-openjdk.i686 : OpenJDK Runtime Environment 8
java-1.8.0-openjdk.x86_64 : OpenJDK 8 Runtime Environment
java-1.8.0-openjdk-accessibility.i686 : OpenJDK accessibility connector
java-1.8.0-openjdk-accessibility.x86_64 : OpenJDK accessibility connector
java-1.8.0-openjdk-demo.i686 : OpenJDK Demos 8
java-1.8.0-openjdk-demo.x86_64 : OpenJDK 8 Demos
java-1.8.0-openjdk-devel.i686 : OpenJDK Development Environment 8
java-1.8.0-openjdk-devel.x86_64 : OpenJDK 8 Development Environment
java-1.8.0-openjdk-headless.i686 : OpenJDK Headless Runtime Environment 8
java-1.8.0-openjdk-headless.x86_64 : OpenJDK 8 Headless Runtime Environment
java-1.8.0-openjdk-javadoc.noarch : OpenJDK 8 API documentation
java-1.8.0-openjdk-javadoc-zip.noarch : OpenJDK 8 API documentation compressed in a single archive
java-1.8.0-openjdk-src.i686 : OpenJDK Source Bundle 8
java-1.8.0-openjdk-src.x86_64 : OpenJDK 8 Source Bundle
java-11-openjdk.i686 : OpenJDK Runtime Environment 11

2、安装jdk

yum install java-1.8.0-openjdk.x86_64

3、完成安装后验证

java -version

[root@10-60-140-218 jvm]# java -version
openjdk version "1.8.0_312"
OpenJDK Runtime Environment (build 1.8.0_312-b07)
OpenJDK 64-Bit Server VM (build 25.312-b07, mixed mode)

4、通过yum安装的默认路径为:/usr/lib/jvm

[root@10-60-140-218 tools]# cd /usr/lib/jvm
[root@10-60-140-218 jvm]# ll
total 0
lrwxrwxrwx 1 root root  26 Dec 23 09:49 java -> /etc/alternatives/java_sdk
lrwxrwxrwx 1 root root  32 Dec 23 09:49 java-1.8.0 -> /etc/alternatives/java_sdk_1.8.0
lrwxrwxrwx 1 root root  40 Dec 23 09:49 java-1.8.0-openjdk -> /etc/alternatives/java_sdk_1.8.0_openjdk
drwxr-xr-x 7 root root 135 Dec 23 09:49 java-1.8.0-openjdk-1.8.0.312.b07-1.el7_9.x86_64
lrwxrwxrwx 1 root root  34 Dec 23 09:49 java-openjdk -> /etc/alternatives/java_sdk_openjdk
lrwxrwxrwx 1 root root  21 Dec 22 16:22 jre -> /etc/alternatives/jre
lrwxrwxrwx 1 root root  27 Dec 22 16:22 jre-1.8.0 -> /etc/alternatives/jre_1.8.0
lrwxrwxrwx 1 root root  35 Dec 22 16:22 jre-1.8.0-openjdk -> /etc/alternatives/jre_1.8.0_openjdk
lrwxrwxrwx 1 root root  51 Dec 22 16:22 jre-1.8.0-openjdk-1.8.0.312.b07-1.el7_9.x86_64 -> java-1.8.0-openjdk-1.8.0.312.b07-1.el7_9.x86_64/jre
lrwxrwxrwx 1 root root  29 Dec 22 16:22 jre-openjdk -> /etc/alternatives/jre_openjdk

5、将jdk的安装路径加入到JAVA_HOME

vi /etc/profile

在文件最后加入:

#set java environment
JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk
PATH=$PATH:$JAVA_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools-1.8.0.jar
export JAVA_HOME CLASSPATH PATH

修改/etc/profile之后让其生效

. /etc/profile (注意 . 之后应有一个空格)

这时候随意创建 一个  Test.java文件

public class Test {
    public static void main(String args[]) {
        System.out.println("A new jdk test !");
    }
}

执行javac编译时 出现

[root@10-60-140-218 home]# javac /home/tools/Test.java
bash:javac:command not found

这种情况

①可能是 没有 tools.jar 包

我选择的是这个  1.8.0.jar   

tools-1.8.0.jar.zip-Linux文档类资源-CSDN下载

可能在之前的配置中能看到 最终版本  vi /etc/profile

 

②还有就是  

安装开发环境

不安装则无法使用javac

[root@10-60-140-218 home]# yum install java-1.8.0-openjdk-devel.x86_64
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package java-1.8.0-openjdk-devel.x86_64 1:1.8.0.292.b10-1.el7_9 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================================================================================================================================================================
 Package                                                            Arch                                             Version                                                             Repository                                         Size
=================================================================================================================================================================================================================================================
Installing:
 java-1.8.0-openjdk-devel                                           x86_64                                           1:1.8.0.292.b10-1.el7_9                                             updates                                           9.8 M

Transaction Summary
=================================================================================================================================================================================================================================================
Install  1 Package

Total download size: 9.8 M
Installed size: 40 M
Is this ok [y/d/N]: y
Downloading packages:
java-1.8.0-openjdk-devel-1.8.0.292.b10-1.el7_9.x86_64.rpm                                                                                                                                                                 | 9.8 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 1:java-1.8.0-openjdk-devel-1.8.0.292.b10-1.el7_9.x86_64                                                                                                                                                                       1/1 
  Verifying  : 1:java-1.8.0-openjdk-devel-1.8.0.292.b10-1.el7_9.x86_64                                                                                                                                                                       1/1 

Installed:
  java-1.8.0-openjdk-devel.x86_64 1:1.8.0.292.b10-1.el7_9                                                                                                                                                                                        

Complete!

再次检查 配置 vi /etc/profile 文件 

#set java environment
JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk
PATH=$PATH:$JAVA_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools-1.8.0.jar
export JAVA_HOME CLASSPATH PATH

. /etc/profile

或者 source /etc/profile

生效配置文件;

[root@10-60-140-218 tools]# source /etc/profile

必须reboot 重启下linux系统; 不然不保证能生效.

重启后测试下 javac

[root@10-60-140-218 tools]# javac
Usage: javac  
where possible options include:
  -g                         Generate all debugging info
  -g:none                    Generate no debugging info
  -g:{lines,vars,source}     Generate only some debugging info
  -nowarn                    Generate no warnings
  -verbose                   Output messages about what the compiler is doing
  -deprecation               Output source locations where deprecated APIs are used
  -classpath           Specify where to find user class files and annotation processors
  -cp                  Specify where to find user class files and annotation processors
  -sourcepath          Specify where to find input source files
  -bootclasspath       Override location of bootstrap class files
  -extdirs             Override location of installed extensions
  -endorseddirs        Override location of endorsed standards path
  -proc:{none,only}          Control whether annotation processing and/or compilation is done.
  -processor [,,...] Names of the annotation processors to run; bypasses default discovery process
  -processorpath       Specify where to find annotation processors
  -parameters                Generate metadata for reflection on method parameters
  -d              Specify where to place generated class files
  -s              Specify where to place generated source files
  -h              Specify where to place generated native header files
  -implicit:{none,class}     Specify whether or not to generate class files for implicitly referenced files
  -encoding        Specify character encoding used by source files
  -source           Provide source compatibility with specified release
  -target           Generate class files for specific VM version
  -profile          Check that API used is available in the specified profile
  -version                   Version information
  -help                      Print a synopsis of standard options
  -Akey[=value]              Options to pass to annotation processors
  -X                         Print a synopsis of nonstandard options
  -J                   Pass  directly to the runtime system
  -Werror                    Terminate compilation if warnings occur
  @                Read options and filenames from file

[root@10-60-140-218 home]# javac /home/tools/Test.java
[root@10-60-140-218 home]# java /home/tools/Test.java
Error: Could not find or load main class .home.tools.Test.java

出现:Error: Could not find or load main class .home.tools.Test.java 不用慌

[root@10-60-140-218 home]# cd tools/
[root@10-60-140-218 tools]# ll
total 16
drwxr-xr-x 7 root root 244 Dec 20 15:21 1
drwxr-xr-x 2 root root  46 Dec 23 09:19 jar
-rwxrwxrwx 1 root root 315 Dec 22 09:10 nginx_start.sh
-rw-r--r-- 1 root root 418 Dec 23 09:53 Test.class
-rw-r--r-- 1 root root 127 Dec 22 16:35 Test.java
[root@10-60-140-218 tools]# java Test
A new jdk test !

这是有网络的情况下 可以完成.

无网环境还没尝试过 有机会恢复机器 试一次

参考资料进行操作:

linux安装java步骤 - 海岛拾贝 - 博客园

centOS7 yum install java-1.8.0-openjdk_zh452647457的博客-CSDN博客

如有侵权 请告知 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/678011.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号