Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect java version getting picked up #224

Closed
2598Nitz opened this issue Dec 21, 2022 · 6 comments · Fixed by #225
Closed

Incorrect java version getting picked up #224

2598Nitz opened this issue Dec 21, 2022 · 6 comments · Fixed by #225
Assignees
Labels

Comments

@2598Nitz
Copy link

Seems like vscode-spring-initializr defaults to latest java version, which is 17 as of today. I had selected java version 8. Since, jdk on my local machine is of java 8 and compiled class downloaded by vscode-spring-initialize is of java 17, I was not able to run SpringBootApplication class.

@Eskibear
Copy link
Member

and compiled class downloaded by vscode-spring-initialize is of java 17

This extension doesn't download compiled classes at all. It simply generates a spring boot project.

<java.version>8</java.version>

Check whether java.version is set to 8 in your pom.xml, after the project is created by this extension. If so, then nothing is wrong with this extension.

@Eskibear Eskibear self-assigned this Dec 21, 2022
@2598Nitz
Copy link
Author

java.version is 8 in pom.xml file.

Here's the error that I get when I run spring boot application:

/usr/bin/env /usr/lib/jvm/java-8-openjdk-amd64/bin/java -cp /tmp/cp_cspnamolxupwt2er09kr4mtub.jar com.example.demo.DemoApplication
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:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at com.example.demo.DemoApplication.main(DemoApplication.java:10)

I didn't get this error, when I manually downloaded project from spring initializr website.

@2598Nitz
Copy link
Author

This problem seems to be due to combination of spring boot version 3.0.0 and java version 8 being incompatible.
https://stackoverflow.com/questions/73679679/java-cannot-access-org-springframework-boot-springapplication-bad-class-file

When I downloaded manually with same combination, the downloaded starter project had java version overriden to 17 which explains why I was able to run the application.

Potentially, vscode-spring-initializr can have a feature to detect incompatible spring boot and java version.

@Eskibear
Copy link
Member

When I downloaded manually with same combination, the downloaded starter project had java version overriden to 17 which explains why I was able to run the application.

Confirmed the behavior. We use the same endpoint to download the project. Looks like the params are different.

javaVersion=1.8 vs javaVersion=8

From start.spring.io

Request URL: https://start.spring.io/starter.zip?type=maven-project&language=java&bootVersion=3.0.0&baseDir=demo&groupId=com.example&artifactId=demo&name=demo&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.demo&packaging=jar&javaVersion=1.8

From extension:

private get downloadUrl(): string {
const params: string[] = [
`type=${this.projectType}`,
`language=${this.metadata.language}`,
`javaVersion=${this.metadata.javaVersion}`,
`groupId=${this.metadata.groupId}`,
`artifactId=${this.metadata.artifactId}`,
`name=${this.metadata.artifactId}`,
`packaging=${this.metadata.packaging}`,
`bootVersion=${this.metadata.bootVersion}`,
`dependencies=${this.metadata.dependencies.id}`,
];
const targetUrl = new URL(this.metadata.serviceUrl);
targetUrl.pathname = "/starter.zip";
targetUrl.search = `?${params.join("&")}`;
return targetUrl.toString();

targetUrl.toString()
'https://start.spring.io/starter.zip?type=maven-project&language=java&javaVersion=8&groupId=com.example&artifactId=demo&name=demo&packaging=jar&bootVersion=3.0.0&dependencies='

@Eskibear
Copy link
Member

image

when constructing the url, we should use the "id" 1.8 instead of the "name" 8.

@Eskibear
Copy link
Member

it will be available in next nightly build. You can switch to pre-release version then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants