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

如何通过CMD启动Spring项目?编译代码时遇报错求助

Fixing Compilation Error When Trying to Start Spring Boot App via CMD with javac

Hey there! Let's break down why you're running into issues compiling your EasyTripApplication.java directly with the javac command—and how to fix it properly.

The Root Problem

Spring Boot projects rely on dozens of external dependencies (like Spring Core libraries, Boot starters, etc.). When you run javac EasyTripApplication.java directly, the compiler has no idea where to find all those required jar files. It only looks at the current directory, so it throws errors because it can't resolve imports like org.springframework.boot.SpringApplication.

Spring Boot is built to work with build tools like Maven or Gradle, which handle dependency management, compilation, and packaging automatically. Here's how to use them:

1. If Your Project Uses Maven

  • First, confirm your project root has a pom.xml file (this is Maven's config file that defines all your dependencies).
  • Open CMD, navigate to your project's root directory (where pom.xml lives), and run:
    • To compile the project: mvn clean compile
    • To start the app directly: mvn spring-boot:run
    • Or, package it into an executable jar and run it:
      1. mvn clean package (this creates a jar in the target folder)
      2. java -jar target/easytrip-<version>.jar (replace <version> with your project's version from pom.xml)

2. If Your Project Uses Gradle

  • Check for a build.gradle (or build.gradle.kts for Kotlin) file in your project root.
  • Open CMD, go to the project root, and run:
    • To compile: gradlew.bat build (Windows) or ./gradlew build (Linux/Mac)
    • To start the app: gradlew.bat bootRun (Windows) or ./gradlew bootRun (Linux/Mac)
    • To run the packaged jar:
      1. Build first with the command above
      2. java -jar build/libs/easytrip-<version>.jar

If you really need to use javac directly (it's tedious!), you'll need to:

  • Download all your project's dependency jars (grab them from Maven Central or copy them from your build tool's cache) and put them in a folder like lib in your project root.
  • Compile with the classpath pointing to all those jars:
    javac -cp "lib/*;." com/easytrip/EasyTripApplication.java
    
    (Note: Use : instead of ; if you're on Linux/Mac.)
  • To run the compiled class, you'll also need to specify the classpath:
    java -cp "lib/*;." com/easytrip/EasyTripApplication
    

Again, this is not practical for Spring Boot projects—stick with Maven/Gradle to save yourself a headache.

内容的提问来源于stack exchange,提问作者Roman Shmandrovskyi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 08:09:00