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

[packagers] Broken java-archive launcher in windows #1634

Closed
jvalkeal opened this issue Apr 9, 2024 · 9 comments · Fixed by #1635
Closed

[packagers] Broken java-archive launcher in windows #1634

jvalkeal opened this issue Apr 9, 2024 · 9 comments · Fixed by #1635
Assignees
Labels
bug Something isn't working released Issue has been released
Milestone

Comments

@jvalkeal
Copy link
Contributor

jvalkeal commented Apr 9, 2024

Originally in spring-projects/spring-cli#178 but noticed same issue in jreleaser as well. spring-cli is using fatjar while jreleaser uses exploded jar but both ways fail with:

Error: Could not find or load main class
Caused by: java.lang.ClassNotFoundException:

Problem is in

which is causing extra argument with space.

I took jreleaser-standalone-early-access-windows-x86_64.zip and enabled "echo" in bat file:

C:\Users\jannev\Downloads\jreleaser-standalone-early-access-windows-x86_64\jreleaser-standalone-early-access-windows-x86_64\bin>set JAVA_OPTS=" "

C:\Users\jannev\Downloads\jreleaser-standalone-early-access-windows-x86_64\jreleaser-standalone-early-access-windows-x86_64\bin>""C:\Users\jannev\Downloads\jreleaser-standalone-early-access-windows-x86_64\jreleaser-standalone-early-access-windows-x86_64"\bin\java"  " " -cp """C:\Users\jannev\Downloads\jreleaser-standalone-early-access-windows-x86_64\jreleaser-standalone-early-access-windows-x86_64\jars"\*"" org.jreleaser.cli.Main
Error: Could not find or load main class
Caused by: java.lang.ClassNotFoundException:

In spring-cli it looks like:

C:\Users\jannev\Downloads\spring-cli-standalone-0.9.0-SNAPSHOT-windows-x86_64>set JAVA_OPTS=" "

C:\Users\jannev\Downloads\spring-cli-standalone-0.9.0-SNAPSHOT-windows-x86_64>""C:\Users\jannev\Downloads\spring-cli-standalone-0.9.0-SNAPSHOT-windows-x86_64"\bin\java"  " " -cp ""C:\Users\jannev\Downloads\spring-cli-standalone-0.9.0-SNAPSHOT-windows-x86_64\jars"\*" -jar ""C:\Users\jannev\Downloads\spring-cli-standalone-0.9.0-SNAPSHOT-windows-x86_64\jars"\spring-cli-0.9.0-SNAPSHOT.jar"
Error: Could not find or load main class
Caused by: java.lang.ClassNotFoundException:

You get same error in java by just doing:

$ java " "
Error: Could not find or load main class  
Caused by: java.lang.ClassNotFoundException:  

I could try to PR a fix?

@jvalkeal jvalkeal added the bug Something isn't working label Apr 9, 2024
@jvalkeal
Copy link
Contributor Author

jvalkeal commented Apr 9, 2024

There is a good info in this old scala issue scala/bug#4858. They also had issues with quotes.

@aalmiray
Copy link
Member

aalmiray commented Apr 9, 2024

We'd very much welcome a PR, thank you 😅

@aalmiray
Copy link
Member

FWIW I think this could be fixed by using mustache itself, like so

{{#distributionJavaOptions}}
 set JAVA_OPTS="%JAVA_OPTS% {{.}}" 
{{/distributionJavaOptions}}

@jvalkeal
Copy link
Contributor Author

That was something what I was thinking as well. However if you read that scala issue quoting is problematic itself if it'd done blindly as it breaks if existing JAVA_OPTS itself contain quotes. That's what I was planning to experiment that when this expansion actually blows up.

@aalmiray
Copy link
Member

AFAICT package processors should ensure the value of JAVA_OPTS is properly quoted before sending it to the Mustache template engine.

@jvalkeal
Copy link
Contributor Author

Actually there's additional issues for quoting -cp value. Looking older jreleaser versions, 1.9.0 is a last good release. 1.10.0 works if I remove JAVA_OPTS from jreleaser.bat. 1.11.0 fails with wrong quotes even if I remove JAVA_OPTS.

This was in my origin comment but I didn't pay attention(three quotes where one after jars messes things up):

-cp """C:\Users\jannev\Downloads\jreleaser-standalone-early-access-windows-x86_64\jreleaser-standalone-early-access-windows-x86_64\jars"\*""

I didn't realise this with spring-cli as while while -cp is wrongly quoted it doesn't matter as it's using fatjar.

@jvalkeal
Copy link
Contributor Author

For JAVA_OPTS I think we need to remove quoting as it will always break things up if there's more than one option(or one option with extra space).

{{#distributionJavaOptions}}
 set JAVA_OPTS=%JAVA_OPTS% {{.}}
{{/distributionJavaOptions}}

If we want to be pedantic:

{{#distributionJavaOptions}}
if not defined JAVA_OPTS (
  set JAVA_OPTS={{.}}
) else (
  set JAVA_OPTS=%JAVA_OPTS% {{.}}
)
{{/distributionJavaOptions}}

As bat interpreter doesn't remove quotes when using set these would fail:

java "-Xms1g -Xmx1G"
java "-Xms1g "

you can use quotes but those needs to immediately surround options like:

java "-Xms1g" "-Xmx1G"

And this same story goes with -cp.

@aalmiray
Copy link
Member

I see. Individual elements may need to be quoted but not the full set as a whole (all entries seen as a single argument).

jvalkeal added a commit to jvalkeal/jreleaser that referenced this issue Apr 12, 2024
- This commit fixes various errors in windows launcher templates
  for java-archive and jlink.
- JAVA_OPTS as whole cannot be surrounded with quotes as windows
  shell would then treat that as single argument and jvm
  blows up. Quotes could be used with individual JAVA_OPTS arguments
  but that is then solely left to user.
- If java options are coming from distribution settings(ie jreleaser.yml)
  then we simply append to JAVA_OPTS as is without quoting.
- In java-archive template java.exe is changed to backslash like
  it already was in jlink template.
- Running java command also contained mixed double quoting and is fixed by
  removing quotes from JAVACMD.
- Classpath contained mixed triple quoting and is fixed by
  removing quotes from JARSDIRS and CLASSPATH

Fixes jreleaser#1634
aalmiray pushed a commit that referenced this issue Apr 12, 2024
- This commit fixes various errors in windows launcher templates
  for java-archive and jlink.
- JAVA_OPTS as whole cannot be surrounded with quotes as windows
  shell would then treat that as single argument and jvm
  blows up. Quotes could be used with individual JAVA_OPTS arguments
  but that is then solely left to user.
- If java options are coming from distribution settings(ie jreleaser.yml)
  then we simply append to JAVA_OPTS as is without quoting.
- In java-archive template java.exe is changed to backslash like
  it already was in jlink template.
- Running java command also contained mixed double quoting and is fixed by
  removing quotes from JAVACMD.
- Classpath contained mixed triple quoting and is fixed by
  removing quotes from JARSDIRS and CLASSPATH

Fixes #1634
@aalmiray aalmiray added this to the v1.12.0 milestone Apr 12, 2024
@aalmiray aalmiray added the released Issue has been released label May 1, 2024
@aalmiray
Copy link
Member

aalmiray commented May 1, 2024

🎉 This issue has been resolved in v1.12.0 (Release Notes)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released Issue has been released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants