Spring Boot应用运行报错,Java应用启动但功能异常求助
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
mainClassNamevalue exactly matches your class's full package path (case-sensitive!). - Your custom
jarconfiguration might interfere with the Spring Boot plugin's ability to create a runnable JAR. If you need the custom output path, keep thejarblock but rely on thebootJartask (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-parentdependency (the BOM independencyManagementhandles version alignment automatically) - Swapped unstable
Finchley.BUILD-SNAPSHOTfor stableFinchley.RELEASE - Removed duplicate
com.auth0:java-jwtentry - Used modern Gradle
implementationinstead ofcompile(fully compatible with Spring Boot) - Eliminated conflicting older Spring Boot/Spring Cloud versions from your previous attempts
3. Final Steps to Apply Fixes
- Clean and Rebuild: Run these commands to clear cached dependencies and rebuild:
./gradlew clean ./gradlew build --refresh-dependencies - 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
- Go to
- 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
- First run via command line to rule out IDE issues:
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

