I have downloaded Spring Tool Suite4 IDE, Java17 Came Along with that IDE. But i want to work on Java8.
I installed only java8 in my system and started working on to print just "Hello Spring Boot".
When i run the sample project the code is being compiled with java17 and trying to run with java8(which i have given in execution environments and build path)
I got error like below.
Exception in thread "main" java.lang.UnsupportedClassVersionError: org/springframework/boot/SpringApplication has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:473)
at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at com.aditya.springboot.HelloSpringBootProjectApplication.main(HelloSpringBootProjectApplication.java:10)
I changed the compiler compliance from 17 to 1.8 in windows-\>preferences-\>Compiler-\>JDK Compliance..,
I pointed the Installed JREs to from jre 17(default) to jdk 1.8 and
I changed the compiler compliance from 17 to 1.8 in project-\>properties-\>java compiler also.
I tried uninstalling the STS4 and reinstalling it, but still i am getting same error.
As i am using maven project i tried changing \<java.version\> to 1.8 from 17 in pom.xml file too.
Can anyone help me from where STS is taking java17 inorder to compile my code, i haven't installed java17 in my system also. I have pointed JAVA_HOME in enviroment variables(user level variable but not system level varaiable) to java8 only.
CodePudding user response:
Let me give the conclusion first, I can use JDK 17 to compile, and then execute it in the JDK 8 environment. I will give my steps and details.
Create Example Project
Open https://start.spring.io/
- Project: Maven
- Lanague: Java
- Spring Boot: 2.7.8
- Packaging: Jar
- Java: 8
- Dependencies: add Spring Web
Project Metadata:
- Group: com.example
- Artifact: demo
- Name: demo
- Description: Demo projet for Spring Boot
- Package name: com.example.demo
Click GENERATE
Compile Example
unzip demo.zip
cd demo
mvn clean package
Test Run
Check JVM Version
$ java -version
openjdk version "1.8.0_362"
OpenJDK Runtime Environment (Temurin)(build 1.8.0_362-b09)
OpenJDK 64-Bit Server VM (Temurin)(build 25.362-b09, mixed mode)
Run Spring Boot Jar
$ cd target
$ java -jar demo-0.0.1-SNAPSHOT.jar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.8)
2023-02-01 16:31:43.356 INFO 23733 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication v0.0.1-SNAPSHOT using Java 1.8.0_362 on
...
2023-02-01 16:31:44.431 INFO 23733 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2023-02-01 16:31:44.438 INFO 23733 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.406 seconds (JVM running for 1.771)
Ctrl C
to exit Spring Boot app.
Open Your STS
File
-> Import
-> Maven
-> Existing Maven Projects
Click Next
Root Directory: Click Browse
Select unzip demo folder
Click Finish
Check STS Package Expolrer
demo tree: JRE System Library [JavaSE-1.8]
default is config JDK 8
STS Menu : Windows
-> Show View
-> Project Explorer
Select demo (Project)
Popup menu -> Run As
-> Spring Boot App
In Console Window Can see start and run...
Because you didn't provide enough steps and information, so I can only use the simplest steps, and the steps you can repeat, to provide the assistance I can provide.
Your requirements: STS 4, using JDK8.
Hope these steps can assist you to reproduce the same result on your machine.
CodePudding user response:
I think you might need to adjust your pom.xml
if you are using it to configure the java version. However, you need to make sure that the spring boot version that you are using is compatible with java 8.
In your pom.xml
add the following:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version> // your spring boot version
</parent>
............
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source> // the version you desire
<target>1.8</target> // the version you desire
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
for gradle:
plugins {
java
}
group = "com.group"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8 // desired version
java.targetCompatibility = JavaVersion.VERSION_1_8 // desired version