杰瑞科技汇

cmd中如何运行Java class文件?

Of course! Let's break down how to compile and run a Java class from the command line (CMD). This is a fundamental skill for any Java developer.

We'll go through it step-by-step, from writing the code to running it.

Step 1: Write the Java Code

First, you need a Java source file. Let's create a simple class named HelloWorld.java.

Create a new file named HelloWorld.java and paste the following code into it:

// File: HelloWorld.java
public class HelloWorld {
    // This is the main method, the entry point of any Java application.
    public static void main(String[] args) {
        // This line prints "Hello, World!" to the console.
        System.out.println("Hello, World from CMD!");
    }
}

Key Point: The filename must exactly match the public class name, including the capitalization. So, public class HelloWorld must be saved in a file named HelloWorld.java.

Step 2: Open the Command Prompt (CMD)

  • Windows: Press Win + R, type cmd, and press Enter.
  • macOS/Linux: Open the Terminal application.

Step 3: Navigate to Your File's Directory

You need to tell the command prompt where your HelloWorld.java file is located. Use the cd (change directory) command.

For example, if you saved your file on your Desktop:

cd C:\Users\YourUsername\Desktop
  • Note: Use forward slashes even on Windows. It's more reliable. If your username has spaces, you can use quotes: "C:\Users\Your Name\Desktop".

Pro-Tip: To see the current directory, type dir (on Windows) or ls (on macOS/Linux) and press Enter. To see your current location, type pwd (on macOS/Linux) or cd (on Windows). You should see HelloWorld.java in the list of files.

Step 4: Compile the Code (Create the .class file)

Now, you'll use the Java compiler, javac, to turn your human-readable .java file into Java bytecode, which is a machine-readable .class file.

In the command prompt, type the following command and press Enter:

javac HelloWorld.java
  • javac: This is the command to invoke the Java Compiler.
  • HelloWorld.java: This is the source file you want to compile.

If the compilation is successful, you won't see any messages. But if you check the directory now (using dir or ls), you will see a new file: HelloWorld.class. This is the bytecode that the Java Virtual Machine (JVM) can execute.

Step 5: Run the Compiled Code (Execute the program)

Finally, you'll use the java command to run your compiled program. This command starts the JVM, which loads your class and executes its main method.

Important: Notice that you do not include the .class extension when running the program.

In the command prompt, type the following command and press Enter:

java HelloWorld
  • java: This is the command to invoke the Java Virtual Machine (JVM).
  • HelloWorld: This is the name of the class you want to run.

You should see the following output printed to your console:

Hello, World from CMD!

Common Problems and Solutions

Problem 1: 'javac' is not recognized as an internal or external command...

This means the Java Development Kit (JDK) is not installed or not added to your system's PATH environment variable.

Solution:

  1. Install the JDK: If you haven't, download it from the official Oracle website or an open-source alternative like Adoptium (Eclipse Temurin).
  2. Add JDK to PATH:
    • Find the JDK installation directory (e.g., C:\Program Files\Java\jdk-17).
    • Open the System Properties (Win + Pause Break, or search for "Environment Variables").
    • Click "Environment Variables...".
    • Under "System variables", find the Path variable and click "Edit...".
    • Click "New" and add the path to the JDK's bin directory (e.g., C:\Program Files\Java\jdk-17\bin).
    • Click OK on all windows and reopen your command prompt for the changes to take effect.

Problem 2: Error: Could not find or load main class HelloWorld

This is a very common error. It usually means the JVM couldn't locate your class file.

Possible Causes & Solutions:

  1. You typed java HelloWorld.class by mistake.

    • Solution: Remember to omit the .class extension. The command is java HelloWorld.
  2. You are in the wrong directory.

    • Solution: Make sure you used dir or ls to confirm you see HelloWorld.class in your current folder.
  3. Your class is part of a package (more advanced).

    • For simple programs, this isn't the issue, but it's a common pitfall. If your class has a package declaration (e.g., package com.example;), you must run it from the root of that package structure.

Summary of Commands

Goal Command (Windows) Command (macOS/Linux) Description
Compile javac MyClass.java javac MyClass.java Compiles the source file into bytecode.
Run java MyClass java MyClass Runs the compiled bytecode using the JVM.
List Files dir ls Lists files in the current directory.
Change Dir cd path\to\folder cd path/to/folder Changes the current directory.
Print Path cd pwd Prints the current working directory.
分享:
扫描分享到社交APP
上一篇
下一篇