简易SpringBoot应用在Eclipse可运行,Unix终端启动抛异常如何解决
Hey there! Let's troubleshoot this step by step—since it works in Eclipse, the issue is almost definitely related to how you're building/running it in the Unix terminal. Here are the most common fixes to get your app up and running:
Eclipse handles builds behind the scenes, but in the terminal you need to explicitly build the executable JAR that includes all dependencies. Run these commands from your project root directory:
- Clean any leftover build artifacts:
./gradlew clean - Build the full executable JAR (this is the key command for Spring Boot apps):
./gradlew bootJarNote: If you're using an older Spring Boot version (pre-2.x), you might need
./gradlew buildinstead, butbootJaris the standard for modern versions. - After the build finishes, your JAR file will live in the
build/libs/directory. Look for a file named likeyour-app-name-0.0.1-SNAPSHOT.jar—the exact name depends on thegroupandversionsettings in yourbuild.gradle.
Once you have the built JAR, launch it with the standard java -jar command:
java -jar build/libs/your-app-name-0.0.1-SNAPSHOT.jar
- Double-check the path and filename—typos here are super common! If you get a "file not found" error, navigate to
build/libs/withcd build/libsand list the files withlsto confirm the JAR exists.
Eclipse might be using a different JDK than your Unix terminal, which can cause runtime errors. Let's verify:
- In your terminal, run
java -versionandjavac -versionto see what JDK is active. - In Eclipse, go to
Window > Preferences > Java > Installed JREsto check which JDK it's using.
Make sure both are using the same major version (e.g., Java 17 for Spring Boot 3.x, Java 8 or 11 for Spring Boot 2.x). If they don't match, set your terminal'sJAVA_HOMEenvironment variable to point to Eclipse's JDK, or install the correct version on your Unix machine.
build.gradle Configuration Double-check that your build.gradle has the necessary Spring Boot plugin to create an executable JAR. Here's a minimal working example for reference:
plugins { id 'org.springframework.boot' version '3.2.0' id 'io.spring.dependency-management' version '1.1.4' id 'java' } group = 'com.example' version = '0.0.1-SNAPSHOT' java { sourceCompatibility = '17' } repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter' testImplementation 'org.springframework.boot:spring-boot-starter-test' } tasks.named('test') { useJUnitPlatform() }
- If the Spring Boot plugin is missing, add it—without it, the JAR won't be configured to run as a standalone Spring Boot app.
If you get a "permission denied" error, make sure you have read access to the JAR file. Fix this with:
chmod +r build/libs/your-app-name-0.0.1-SNAPSHOT.jar
(You don't need execute permissions here since we're running the JAR via the Java runtime, but read access is mandatory.)
If none of the above works, share the full error output from the terminal. Common issues we can spot include missing dependencies (if you didn't use bootJar), classpath conflicts, or configuration errors that only surface in a standalone runtime.
内容的提问来源于stack exchange,提问作者nagy.zsolt.hun

