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

纯Bash命令行编译Spring @Configuration类及依赖Jar查找方法

解决Spring @Configuration注解命令行编译问题 & 本地Jar包定位指南

Got it, let's walk through this properly since you're sticking to the command line for Spring development—no IDE needed, totally respect that. The core issue here is missing the right JARs for Spring's annotation processing, so let's break this down step by step, including how to systematically find the JARs you need.

1. First: Which JAR has @Configuration?

The @Configuration annotation lives in the Spring Context module, with the full class name org.springframework.context.annotation.Configuration. The main JAR you need is spring-context-<your-spring-version>.jar, but it relies on a few foundational Spring JARs to work:

  • spring-core-<version>.jar: Spring's core utility classes
  • spring-beans-<version>.jar: For bean definition and management
  • spring-expression-<version>.jar: Supports Spring Expression Language (SpEL)

2. Find these JARs in your local Maven repositories

You've got two repository paths: /root/.m2/repository and /usr/share/maven/ref/repository/. Use Bash's find command to hunt down the JARs quickly:

Search for the spring-context JAR

find /root/.m2/repository /usr/share/maven/ref/repository -name "spring-context-*.jar"

Verify the JAR contains @Configuration

Once you have a path, confirm it has the annotation class with jar tf (list contents of a JAR):

jar tf /path/to/spring-context-xxx.jar | grep "org/springframework/context/annotation/Configuration.class"

If you see the class path printed, you've found the right JAR.

3. Compile AppConfig.java directly from the command line

With all required JAR paths in hand, use javac with the -cp (classpath) flag to include them:

javac -cp "/path/to/spring-context-xxx.jar:/path/to/spring-core-xxx.jar:/path/to/spring-beans-xxx.jar:/path/to/spring-expression-xxx.jar" AppConfig.java

Pro tip: In Bash, separate multiple classpath entries with a colon (:) (use semicolons if you were on Windows).

To avoid typing long paths every time, store the JARs in a variable:

SPRING_JARS=$(find /root/.m2/repository /usr/share/maven/ref/repository -name "spring-context-*.jar" -o -name "spring-core-*.jar" -o -name "spring-beans-*.jar" -o -name "spring-expression-*.jar" | tr '\n' ':')
javac -cp "$SPRING_JARS" AppConfig.java

4. The minimalist Maven approach (less manual work)

Since you already have a local Maven repository, a tiny pom.xml can handle dependencies automatically—no more hunting for JARs manually. Here's the leanest setup:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.your.project</groupId>
    <artifactId>spring-cli-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- Just add spring-context; Maven pulls all its dependencies automatically -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>your-spring-version</version> <!-- e.g., 5.3.24 -->
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- Set your Java version to avoid compilation mismatches -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source> <!-- Match your installed Java version -->
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Save this in the same directory as AppConfig.java, then run:

mvn compile

Maven will pull dependencies from your local repo and compile your code into the target/classes folder.

If you still want the classpath for manual javac runs, use Maven's dependency plugin to generate it:

mvn dependency:build-classpath -Dmdep.outputFile=classpath.txt
javac -cp "$(cat classpath.txt)" AppConfig.java

General trick: Find any JAR by class/annotation name

For future issues, use this workflow to locate the right JAR:

  1. Look up the full class name of the annotation/class you're missing (e.g., org.springframework.context.annotation.Configuration for @Configuration).
  2. Use this combined find and jar tf command to search your repositories:
find /root/.m2/repository /usr/share/maven/ref/repository -name "*.jar" -exec jar tf {} \; | grep "org/springframework/context/annotation/Configuration.class"
  1. If you need to narrow it down, grep for the package instead of the full class (e.g., org/springframework/context/annotation/).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:28:42