Linux | 配置Java环境并实现hello world的输出

  • 内容
  • 相关

之前在Linux环境下使用Cpython实现了hello world的输出,现在需要用Java进行实现。系统默认环境是没有java的编译环境,所以我们还需要先进行java环境的配置,才能去实现java程序的编译运行。 

一、Java环境的配置

1.切换到root用户

[root@fishyoung ~]# whoami

root

 

2.检测当前Linux中是否有Java的环境:

[root@fishyoung ~]# java  -version

注:如果没有回馈信息,则没有Java的编译环境

 

3. 到官网或者百度下载一个jdk/jre文件

JDK is a superset of JRE , and contains everything that is in JRE , plus tools such as the compilers and debuggers necessary for developing applets and applications. JRE provides the libraries, the Java Virtual Machine (JVM), and other components to run applets and applications written in the Java programming language. Note that the JRE includes components not required by the Java SE specification, including both standard and non-standard Java components.

上面这段话意思是说:JDKJRE的超集,也就是只要JRE有的JDK都有,同时JDK还提供了java程序开发过程中的编译器和调试器。JRE提供java基础类库,java虚拟机(JVM)等,能运行java编程语言编写的应用程序。……

[root@fishyoung ~]# ls jdk-8u161-linux-x64.rpm

jdk-8u161-linux-x64.rpm

 

4. 安装jdk文件

[root@fishyoung ~]# rpm -ivh jdk-8u161-linux-x64.rpm

[root@fishyoung ~]# cd /usr/java/jdk1.8.0_161/

[root@fishyoung jdk1.8.0_161]# ls

bin             jre          release

……………

[root@fishyoung jdk1.8.0_161]#

 

5. 设置环境变量

打开/etc/profile文件进行修改/添加

[root@fishyoung ~]# vi /etc/profile

export JAVA_HOME=/usr/java/jdk1.8.0_161

export PATH=$JAVA_HOME/bin:$PATH

 

6. 执行profile文件

source /etc/profile

 

7. 检查安装结果

[root@fishyoung ~]# java -version

java version "1.8.0_161"

Java(TM) SE Runtime Environment (build 1.8.0_161-b12)

Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)

[root@fishyoung ~]#

这样我们就配置好Java环境了

 

二、使用Java输出helloworld

1.建立并打开一个.java文件(需要以.java为后缀名)

[root@fishyoung ~]# vi hello.java

注:写入如下内容

public class hello

{

  public static void main(String[] args)

  {

    System.out.println("hello world!");

  }

}

[root@fishyoung ~]#

2. 编译运行

编译Java文件的方式是“javac hello.java”,会产生一个hello.class文件,然后用“java  hello”命令就可以运行输出结果,具体情况如下:

[root@fishyoung ~]# javac hello.java

[root@fishyoung ~]# java hello

hello world!

[root@fishyoung ~]#

 您阅读这篇文章共花了:

上一篇:Linux | 实现用C和python输出hello world

下一篇:Hadoop | NameNode和SecondaryNameNode的关系

本文标签:    

版权声明:本文依据CC-BY-NC-SA 3.0协议发布,若无特殊注明,本文皆为《fishyoung》原创,转载请保留文章出处。

本文链接:Linux | 配置Java环境并实现hello world的输出 - http://www.fishyoung.com/post-279.html