如何在Visual Studio Code中编译并运行Java代码(已完成环境配置)
How to Compile & Run Your Java Hello World in VS Code
Hey there! Since you've already got VS Code set up with the official Java Extension Pack and JDK 1.8.0_161 configured correctly, running your test program is totally hassle-free. I'll walk you through two simple methods:
Method 1: Use VS Code's Built-in Run Feature (Easiest!)
- First, make sure your
.javafile is saved (VS Code auto-saves by default, but hittingCtrl+S(Windows/Linux) orCmd+S(Mac) is a safe check) - Look for the green triangular Run button in the top-right corner of the editor window—click it, and VS Code will automatically compile and run your code. You'll see the output in the Terminal panel at the bottom of the window.
- Alternatively, right-click anywhere in the editor area and select
Run Javafrom the context menu—this does the exact same thing.
Method 2: Compile & Run via Terminal
If you prefer using command-line steps, here's how:
- Open VS Code's integrated terminal: Use the shortcut
Ctrl+`(that's a backtick), or go to the top menu bar → Terminal → New Terminal. - Navigate to the folder where your
.javafile is located (if the terminal isn't already there). Use thecdcommand, e.g.:- Windows:
cd C:\Users\YourName\Documents\JavaProjects - Mac/Linux:
cd /Users/YourName/Documents/JavaProjects
- Windows:
- Compile the code: Run this command, replacing
HelloWorldwith your actual filename:
This will generate ajavac HelloWorld.javaHelloWorld.classfile (the compiled bytecode) in the same folder. - Run the compiled program: Execute this command (note: no
.classsuffix needed):
You should immediately see your "Hello World" output in the terminal.java HelloWorld
A quick note: If you run into "command not found" errors, double-check that your JDK 1.8 bin path is properly added to your system's PATH environment variable—but since you followed the extension's docs, this should already be sorted!
内容的提问来源于stack exchange,提问作者hgd6o
相关产品推荐
相关产品推荐

