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

Spring Boot应用运行报错,Java应用启动但功能异常求助

Fixing Maven-to-Gradle Migration Issues in Spring Boot

Alright, let's work through your problems step by step—both the main class not found error and the broken OAuth2/Zuul functionality stem from two key issues: incorrect main class syntax and incompatible dependency versions.

1. Fix the "Could not find or load main class" Error

First, let's fix your main class code—it looks like there's a syntax mistake in the snippet you provided. The @EnableZuulProxy and @SpringBootApplication annotations should be applied to a class, not directly above the main method. Here's the corrected version:

@EnableZuulProxy
@SpringBootApplication
public class DevProxyApp {
    public static void main(String[] args) {
        try {
            SpringApplication.run(DevProxyApp.class, args);
        } catch(Exception e) {
            // Add logging here to debug startup failures
            e.printStackTrace();
        }
    }
}

Next, adjust your Gradle configuration to ensure the Spring Boot plugin correctly identifies the main class:

  • Double-check that the mainClassName value exactly matches your class's full package path (case-sensitive!).
  • Your custom jar configuration might interfere with the Spring Boot plugin's ability to create a runnable JAR. If you need the custom output path, keep the jar block but rely on the bootJar task (auto-configured by the Spring Boot plugin) for executable builds.

2. Resolve Dependency Compatibility Issues

This is the root cause of your OAuth2/Zuul functionality breaking. You're mixing incompatible Spring Boot and Spring Cloud versions, plus redundant dependencies. Let's clean this up:

Key Version Mapping

Spring Boot 2.0.x requires Spring Cloud Finchley series (Edgware is designed for Spring Boot 1.5.x). Use a stable release instead of a snapshot for reliability.

Corrected build.gradle Configuration

Replace your current build script with this cleaned-up version:

buildscript {
    ext {
        springBootVersion = '2.0.2.RELEASE'
        springCloudVersion = 'Finchley.RELEASE' // Stable release matching Spring Boot 2.0.2
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

plugins {
    id 'pmd'
    id 'org.sonarqube' version '2.6.2'
    id 'java'
    id 'application'
    id 'eclipse'
    id 'org.springframework.boot'
    id 'io.spring.dependency-management'
    id 'jacoco'
}

mainClassName = 'com.siemens.mindsphere.devproxy.DevProxyApp'
group = 'mindsphere'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
    maven { url "https://repo.spring.io/snapshot" }
    maven { url "https://repo.spring.io/milestone" }
}

jar {
    baseName = 'sdk-devproxy'
    destinationDir = file("$buildDir/libs/mindsphere/sdk-devproxy/$project.version/")
}

dependencies {
    // Core Spring Cloud dependencies (managed by BOM, no explicit versions needed)
    implementation 'org.springframework.cloud:spring-cloud-starter-oauth2'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-zuul'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    
    // Third-party dependency (removed duplicate entry)
    implementation 'com.auth0:java-jwt:3.3.0'
    
    // Test dependencies
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.security:spring-security-test'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

What Changed:

  • Removed redundant spring-cloud-starter-parent dependency (the BOM in dependencyManagement handles version alignment automatically)
  • Swapped unstable Finchley.BUILD-SNAPSHOT for stable Finchley.RELEASE
  • Removed duplicate com.auth0:java-jwt entry
  • Used modern Gradle implementation instead of compile (fully compatible with Spring Boot)
  • Eliminated conflicting older Spring Boot/Spring Cloud versions from your previous attempts

3. Final Steps to Apply Fixes

  1. Clean and Rebuild: Run these commands to clear cached dependencies and rebuild:
    ./gradlew clean
    ./gradlew build --refresh-dependencies
    
  2. Reimport the Project in STS:
    • Go to File > Import > Gradle > Existing Gradle Project
    • Select your project directory and ensure STS uses the updated Gradle configuration
  3. Test the Application:
    • First run via command line to rule out IDE issues: ./gradlew bootRun
    • If that works, try running as a Spring Boot app in STS again

Why This Works

  • Correcting the main class syntax ensures the JVM can find and execute your entry point
  • Aligning Spring Boot and Spring Cloud versions eliminates classpath conflicts that break OAuth2 and Zuul functionality
  • Cleaning up redundant dependencies removes unnecessary bloat and version mismatches

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:44:58