知识库 知识库
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)

Jim FuckPPT

Java小学生
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)
  • Hyperskill - Java

    • Java basic

    • Java OOP

    • 应知

      • Theory:Functional decomposition
      • Theory:Paradigms
      • Theory:Overloading
      • Theory:Write, compile, and run
      • Theory:Annotations basics
      • Theory:JVM, JRE, and JDK
      • Theory:Jave Archive
      • Theory:Running programs on your computer
        • Installing Java on your computer
        • Writing a program
        • Compiling and running a program
      • Theory:Enums in Java
      • Theory:Fields and methods in enum
      • Theory:StringBuilder
      • Theory:Immutability
      • Theory:Boxing and unboxing
      • Theory:Introduction to generic programming
      • Theory:Generics and Object
      • Theory:What are collections
      • Theory:The collections Framework overview
      • Theory:ArrayList
      • Theory:The List interface
      • Theory:Comparable
      • Theory:Processing strings
      • Theory:Initialization blocks
      • Theory:Introduction to API
      • Theory:Generic methods
    • 扩展

    • IO & Stream

    • Error & Exception

    • Algorithm & Data structure

    • Design pattern

    • Web

    • Spring boot

  • 练习题

  • Frank - Java与生活

  • Frank - Java API进阶

  • 学习笔记

  • Java
  • Hyperskill - Java
  • 应知
Jim
2022-08-07
目录

Theory:Running programs on your computer

# Installing Java on your computer

In this topic, you will compile and run the simplest Hello World program on your computer. There is one prerequisite: you need to install a JDK to develop Java applications. Please, download the up to date Java version (opens new window). Our projects now support Java 17. Just follow the installation instructions given for your operating system.

To check that the installation has been completed, let's check the version of Java by typing the following command in a terminal:

java -version
1

It outputs the version of Java that is installed on your computer. If it does not work correctly, open the installation instructions and try to set the path variable in your operating system.

If you get stuck with Javac not being recognizable, try to watch this video (opens new window).

# Writing a program

Let's write a simple program and then start it on your computer. To do that we will use a terminal.

Step 1. Create a file named Main.java using any text editor (such as TextPad or NotePad++ for Windows; jEdit or gedit for Mac OS X; gedit for Ubuntu; or something else) and save it in a folder.

Step 2. Paste the following source code into this file:

public class Main {

    public static void main(String[] args) {
        System.out.println("Hello, Java");
    }
}
1
2
3
4
5
6

The public class name must be the same as the file name.

提示

Note, we assume you already understand how the Hello World program works. If not, see here (opens new window) and then come back.

# Compiling and running a program

To run the program, we will use a terminal installed in your OS. All the following commands need to be executed from within the same folder that the .java file is created in.

Step 3. Compile the program using the following command in the terminal:

javac Main.java
1

The javac command asks the compiler to translate the source code into bytecode. The result of this command is a file named Main.class.

Step 4. Run the compiled program (make sure that your terminal is open in the same directory as your source file):

java -cp . Main
1

The java command starts a Java application. It does this by starting a JRE and invoking the main method inside the Main class.

The -cp parameter (classpath) specifies the location of user-defined classes and packages. The dot . means the current terminal directory. We will consider it in detail in the next topics.

提示

Note: you should not specify the .class extension when running a program.

The program should output the following text:

Hello, Java
1

Below is an animation that executes all these steps.

img

Congratulations! You have just started a simple program on your computer. Try to change this program, if you would like to get more interesting results.

提示

Since Java 11 it is possible to compile and run Java source code file using a single command java Main.java. It will compile the file in-memory, so it does not produce a .class file. Many developers don't know this small but interesting feature.

编辑 (opens new window)
#Java#Programming
上次更新: 2022/09/26, 16:55:15
Theory:Jave Archive
Theory:Enums in Java

← Theory:Jave Archive Theory:Enums in Java→

最近更新
01
《挪威的森林》
04-14
02
青钢影
04-14
03
Processing strings
02-18
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Jim Frank | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式