杰瑞科技汇

如何在Windows上高效学习Java?

Of course! Here is a comprehensive guide to setting up and using Java on Windows, tailored for everyone from complete beginners to experienced developers.

如何在Windows上高效学习Java?-图1
(图片来源网络,侵删)

We'll cover:

  1. What is Java? (A quick refresher)
  2. Step 1: Downloading and Installing Java
  3. Step 2: Verifying the Installation
  4. Step 3: Setting Up Your Development Environment (The "Hello, World!" Project)
  5. Step 4: Compiling and Running Your Code from the Command Line
  6. Using an IDE (Integrated Development Environment)
  7. Managing Multiple Java Versions (Advanced)
  8. Troubleshooting Common Issues

What is Java?

Java is a popular, class-based, object-oriented programming language designed to have as few implementation dependencies as possible. It's known for its "write once, run anywhere" (WORA) philosophy, thanks to the Java Virtual Machine (JVM).

  • JDK (Java Development Kit): This is what you need to develop Java applications. It includes the Java compiler (javac), the Java runtime environment (JRE), and other tools.
  • JRE (Java Runtime Environment): This is what you need only to run Java applications. It includes the JVM and core libraries. You don't need this if you install the JDK.
  • JVM (Java Virtual Machine): This is the engine that actually runs your Java code. It translates compiled Java bytecode into machine code that your computer can execute.

For our purposes, we will download and install the JDK.


Step 1: Downloading and Installing Java

The most common and recommended version for new projects is LTS (Long-Term Support). As of late 2025/early 2025, the latest LTS versions are Java 21 and Java 17.

如何在Windows上高效学习Java?-图2
(图片来源网络,侵删)

A. Download the JDK

  1. Go to the official Oracle Java SE Development Kit download page: https://www.oracle.com/java/technologies/downloads/

  2. You will see a list of Java versions. Click on the download link for the latest LTS version (e.g., Java 21 or Java 17).

  3. You will be asked to accept the Oracle Technology Network License Agreement. Check the box and click "Download".

  4. The page will show you different installation packages. For Windows, you want the .msi installer. It will be labeled something like:

    如何在Windows上高效学习Java?-图3
    (图片来源网络,侵删)
    • Windows x64 Installer (for 64-bit systems)
    • Windows x86 Installer (for 32-bit systems, less common today)

    How to check if your system is 64-bit:

    • Press the Windows Key, type This PC, and press Enter.
    • Right-click on This PC and select Properties.
    • Look for "System type". It will say "64-bit Operating System" or "32-bit Operating System".
  5. Click the download link for the correct .msi file. Your browser will prompt you to save the file.

B. Install the JDK

  1. Find the downloaded .msi file (e.g., jdk-21_windows-x64.msi) in your Downloads folder and double-click it to run the installer.

  2. The Windows Installer will guide you through the process.

    • Click Next.
    • You can change the installation directory if you wish, but the default is fine for most users.
    • Click Next, then Install.
  3. The installer will run for a minute or two. When it's done, click Close.


Step 2: Verifying the Installation

Now, let's make sure Windows can find Java. This is done by checking the system's PATH environment variable.

A. Check the PATH Variable

  1. Press the Windows Key, type env, and select "Edit the system environment variables".

  2. In the System Properties window, click the "Environment Variables..." button.

  3. In the new window, look at the "System variables" section at the bottom.

    • Find the variable named Path and select it, then click "Edit...".
  4. You should see a list of paths. The Java installer usually adds the JDK's bin directory for you. Look for entries that include:

    • C:\Program Files\Java\jdk-21\bin (or a similar path with your version number)

    If you see these paths, you're good to go! If not, you need to add them.

    • Click "New".
    • Paste the path to your JDK's bin directory. For example: C:\Program Files\Java\jdk-21\bin.
    • Click OK on all the windows to save your changes.

B. Verify from the Command Line

  1. Open a new Command Prompt or PowerShell. (Closing and reopening is important, as it loads the new environment variables).

  2. Type the following commands and press Enter after each:

    java -version

    This should print the Java version you installed.

    java version "21.0.1" 2025-10-17
    Java(TM) SE Runtime Environment (build 21.0.1+12-LTS-29)
    Java HotSpot(TM) 64-Bit Server VM (build 21.0.1+12-LTS-29, mixed mode, sharing)
    javac -version

    This should print the compiler version. It should match the Java version.

    javac 21.0.1

If both commands work, your Java installation is successful!


Step 3: Setting Up Your First Project ("Hello, World!")

Let's create a simple project to see everything in action.

  1. Create a Project Folder: Open File Explorer and create a main folder for your projects, for example, C:\java_projects. Inside it, create a new folder for this project, e.g., hello-world.

  2. Create the Java File:

    • Navigate to your C:\java_projects\hello-world folder.
    • Right-click in an empty space, select New > Text Document.
    • Rename the file to HelloWorld.java. Important: Make sure the .txt extension is gone. If you don't see extensions, enable them in File Explorer's "View" tab.
  3. Write the Code:

    • Right-click HelloWorld.java and choose Edit or Open with > Notepad (or a better code editor like VS Code).
    • Paste the following code into the file:
    // HelloWorld.java
    public class HelloWorld {
        public static void main(String[] args) {
            System.out.println("Hello, World!");
        }
    }

    Save the file and close the editor.


Step 4: Compiling and Running from the Command Line

This is the fundamental process of building and running a Java program.

  1. Open Command Prompt in Your Project Folder:

    • Press the Windows Key, type cmd, and open Command Prompt.
    • You need to tell the command prompt to work inside your project folder. Use the cd (change directory) command:
      cd C:\java_projects\hello-world

      (If your username is in the path, you might need to use quotes: cd "C:\Users\YourName\java_projects\hello-world")

  2. Compile the Code:

    • The Java compiler (javac) reads your .java source file and produces a .class file (bytecode).
    • In the command prompt, type:
      javac HelloWorld.java
    • If it works, you won't see any messages, but a new file named HelloWorld.class will appear in your folder.
  3. Run the Program:

    • The Java runtime (java) reads the .class file and executes it.
    • In the command prompt, type:
      java HelloWorld
    • Important Note: Notice you do not include the .class extension when running the program. You are telling the java command which class to run, not which file to execute.

    You should see the output:

    Hello, World!

Using an IDE (Integrated Development Environment)

While the command line is great for learning, professional developers use an IDE. An IDE provides a graphical interface with a code editor, debugger, and build tools all in one place.

Highly Recommended IDEs for Java on Windows:

  • Visual Studio Code (Free): A lightweight, powerful, and popular editor. You'll need to install the "Extension Pack for Java" from Microsoft.
  • IntelliJ IDEA (Free & Paid): Considered the gold standard for Java development. The "Community Edition" is free and more than enough for most projects.
  • Eclipse IDE (Free): A long-standing, open-source favorite, especially in enterprise environments.

Using an IDE simplifies the process immensely. You typically just create a "New Project," select "Java Application," and the IDE handles creating the folder structure, compiling, and running for you with a single click.


Managing Multiple Java Versions (Advanced)

Sometimes you need to work on projects that require different Java versions (e.g., Java 8 for an old project and Java 21 for a new one). The best tool for this is SDKMAN!.

  1. Install SDKMAN!: Open a new Command Prompt and run:

    curl -s "https://get.sdkman.io" | bash

    Follow the on-screen instructions. It will ask you to open a new terminal window.

  2. Install Java Versions: In your new terminal, you can now easily install and switch between versions.

    # Install Java 17 (LTS)
    sdk install java 17.0.9-tem
    # Install Java 21 (LTS)
    sdk install java 21.0.1-tem
    # List installed Java versions
    sdk list java
    # Switch the default Java version for the current terminal session
    sdk use java 17.0.9-tem
    # Set Java 17 as the default for all new terminals
    sdk default java 17.0.9-tem

    This gives you incredible flexibility without manually changing system environment variables.


Troubleshooting Common Issues

  • 'javac' is not recognized as an internal or external command...

    • Cause: The JDK's bin directory is not in your system's PATH variable.
    • Solution: Go back to Step 3A and carefully add the bin directory path to your PATH variable. Remember to close and reopen your Command Prompt after making changes.
  • Error: Could not find or load main class HelloWorld

    • Cause 1: You are in the wrong directory in the command prompt. Make sure you are in C:\java_projects\hello-world.
    • Cause 2: You typed java HelloWorld.class by mistake. Remember to omit the .class.
    • Cause 3: Your class is part of a package. For this simple example, this is not the case.
  • I see an error about a file being in use during installation.

    • Cause: Another application (like another command prompt or a virus scanner) is using a file that the installer needs to update.
    • Solution: Close all other applications and try the installation again. Disable your antivirus temporarily as a last resort.
分享:
扫描分享到社交APP
上一篇
下一篇