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

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

Open
RonaldAtWork opened this issue Sep 29, 2020 · 6 comments
Open
Labels
enhancement New feature or request

Comments

@RonaldAtWork
Copy link

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
Copy link
Member

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

@siordache
Copy link
Member

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
Copy link
Author

RonaldAtWork commented Sep 29, 2020 via email

@siordache
Copy link
Member

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
Copy link
Author

RonaldAtWork commented Sep 29, 2020 via email

@kromar777
Copy link

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

@airsquared airsquared added the enhancement New feature or request label Oct 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants