You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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 .java file is saved (VS Code auto-saves by default, but hitting Ctrl+S (Windows/Linux) or Cmd+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 Java from 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:

  1. Open VS Code's integrated terminal: Use the shortcut Ctrl+` (that's a backtick), or go to the top menu bar → Terminal → New Terminal.
  2. Navigate to the folder where your .java file is located (if the terminal isn't already there). Use the cd command, e.g.:
    • Windows: cd C:\Users\YourName\Documents\JavaProjects
    • Mac/Linux: cd /Users/YourName/Documents/JavaProjects
  3. Compile the code: Run this command, replacing HelloWorld with your actual filename:
    javac HelloWorld.java
    
    This will generate a HelloWorld.class file (the compiled bytecode) in the same folder.
  4. Run the compiled program: Execute this command (note: no .class suffix needed):
    java HelloWorld
    
    You should immediately see your "Hello World" output in the terminal.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:32:29