beryx / badass-jlink-plugin

Create a custom runtime image of your modular application
https://badass-jlink-plugin.beryx.org
Apache License 2.0
381 stars 25 forks source link

String.format with locale option does not work after being jlinked. #154

Open RonaldAtWork opened 4 years ago

RonaldAtWork commented 4 years ago

String.format(locale, "%.1f", 10.1) should produce 10,1 (with a comma) for German or Dutch locale. It does when run in Netbeans IDE or when compiled with javac and run with java from command line. java TestStringFormatLocale.Main Output: Not localized: 10.1 en_US: 10.1 de: 10,1 nl_NL: 10,1

But the result is wrongly output as 10.1 (with a point) after being jlinked. This is very strange and a showstopper for distributing my code. build\image\bin\TestStringFormatLocale Output: Not localized: 10.1 en_US: 10.1 de: 10.1 nl_NL: 10.1

My code for testing: //================================================================= Main.java //============================================================== package TestStringFormatLocale; import java.util.Locale; public class Main { public static void main(String[] args) { double x = 10.1; Locale locale = Locale.getDefault();
System.out.println("Not localized: " + String.format("%.1f", x)); System.out.println(locale.toString() + ": " + String.format(locale, "%.1f", x)); locale = Locale.GERMAN; System.out.println(locale.toString() + ": " + String.format(locale, "%.1f", x)); locale = new Locale("nl", "NL"); System.out.println(locale.toString() + ": " + String.format(locale, "%.1f", x)); }
} //================================================================= module-info.java //================================================================= module TestStringFormatLocale { exports TestStringFormatLocale;
} //================================================================= build.gradle //================================================================= plugins { id 'java' id 'jacoco' id 'application' id 'org.beryx.jlink' version '2.22.0' } application { mainModule = 'TestStringFormatLocale' // name defined in module-info.java mainClass = "TestStringFormatLocale.Main" }

repositories { jcenter() }

dependencies { testImplementation 'junit:junit:4.13' }

jlink { options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages'] launcher { name = 'TestStringFormatLocale' } } //=================================================================

siordache commented 4 years ago

Add requires jdk.localedata; to your module-info.java.

siordache commented 4 years ago

Or, if you don't want to change your module-info.java, add the jdk.localedata module to the jlink options:

jlink {
    options = ['--add-modules', 'jdk.localedata', '--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
    launcher {
        name = 'TestStringFormatLocale'
    }
}
RonaldAtWork commented 4 years ago

Serban,

This does the trick. Thank you.

How could I find this missing requires myself?

Regards,

Ronald

From: "Serban Iordache" notifications@github.com To: "beryx/badass-jlink-plugin" badass-jlink-plugin@noreply.github.com Cc: "RonaldAtWork" ronald.verbeek@dunlopcb.com, "Author" author@noreply.github.com Date: 29-09-2020 12:20 Subject: Re: [beryx/badass-jlink-plugin] String.format with locale option does not work after being jlinked. (#154)

Add requires jdk.localedata; to your module-info.java. — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub [github.com], or unsubscribe [github.com].

Confidentiality & Disclaimer Notice: This email and any attached files are private and confidential, intended solely for the use of those to whom it is addressed. If you are not the intended recipient, you may not copy, forward, use, rely on or distribute this message or any attachments. Unauthorised disclosure of any information contained in this communication is prohibited. Please inform the sender if you have received this email in error.
Nothing in this email amounts to a contractual or other legal commitment on our part unless confirmed expressly in writing by an authorised Officer of the Company. The views expressed in this email may not necessarily reflect the views of the Company.

WARNING: The recipient should check this email and any attachments for the presence of viruses. We do not accept responsibility for any virus transmitted by this email.

siordache commented 4 years ago

I don't have a good answer to this question. Locales other than English are made available via a service, for which an implementation is provided by the jdk.localedata module. This is a rather obscure thing and it's not easy to find that this module is missing because Java silently falls back to the default locale.

To avoid similar issues, you can tell jlink to automatically bind all JDK modules that provide a service:

options = ['--bind-services', '--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']

The downside is that you may end up with an image that includes some unused JDK modules.

RonaldAtWork commented 4 years ago

Again thank you very much for your help. My applications works now like expected.

From: "Serban Iordache" notifications@github.com To: "beryx/badass-jlink-plugin" badass-jlink-plugin@noreply.github.com Cc: "RonaldAtWork" ronald.verbeek@dunlopcb.com, "Author" author@noreply.github.com Date: 29-09-2020 16:23 Subject: Re: [beryx/badass-jlink-plugin] String.format with locale option does not work after being jlinked. (#154)

I don't have a good answer to this question. Locales other than English are made available via a service, for which an implementation is provided by the jdk.localedata module. This is a rather obscure thing and it's not easy to find that this module is missing because Java silently falls back to the default locale. To avoid similar issues, you can tell jlink to automatically bind all JDK modules that provide a service: options = ['--bind-services', '--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']

The downside is that you may end up with an image that includes some unused JDK modules. — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub [github.com], or unsubscribe [github.com].

Confidentiality & Disclaimer Notice: This email and any attached files are private and confidential, intended solely for the use of those to whom it is addressed. If you are not the intended recipient, you may not copy, forward, use, rely on or distribute this message or any attachments. Unauthorised disclosure of any information contained in this communication is prohibited. Please inform the sender if you have received this email in error.
Nothing in this email amounts to a contractual or other legal commitment on our part unless confirmed expressly in writing by an authorised Officer of the Company. The views expressed in this email may not necessarily reflect the views of the Company.

WARNING: The recipient should check this email and any attachments for the presence of viruses. We do not accept responsibility for any virus transmitted by this email.

kromar777 commented 2 years ago

It would be nice to have the plugin understand jlink's --include-locales option to get a bit smaller image with only required locales.