Discrepancies between Java 11 and Java 8 DecimalFormat behaviour - java

I've got a weird issue with Java 11. I'm porting a huge project, and I have some tests which use DecimalFormat failing on the 11 build, but not on the 8 build.
DecimalFormat in Java 11 gives me a comma decimal separator, and on Java 8 it gives me a dot.
Here is a minimal reproducible example:
import java.text.DecimalFormat;
public class Test {
public static void main(String[] args) {
DecimalFormat format = new DecimalFormat("#0.00");
System.out.println(format.format(1.02));
}
}
which outputs 1.02 on Java 8, and 1,02 on Java 11.
Here is the entire terminal session of building and running the example with both versions:
~ » sdk use java 8.0.282.hs-adpt
Using java version 8.0.282.hs-adpt in this shell.
~ » javac -version
javac 1.8.0_282
~ » javac Test.java
~ » java -version
openjdk version "1.8.0_282"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_282-b08)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.282-b08, mixed mode)
~ » java Test
1.02
~ » sdk use java 11.0.10.hs-adpt
Using java version 11.0.10.hs-adpt in this shell.
~ » javac -version
javac 11.0.10
~ » javac Test.java
~ » java -version
openjdk version "11.0.10" 2021-01-19
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.10+9)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.10+9, mixed mode)
~ » java Test
1,02
Has anyone had similar issues? Were there any changes to relevant classes between 8 and 11 which would cause this?

Yes, one of the changes was updating Locale Data to Unicode CLDR v33 (see https://www.oracle.com/java/technologies/javase/jdk-11-relnote.html)
This change is relevant for locale-specific formatting, which impacts the usage of the utils like DecimalFormat or SimpleDateFormat.
So if your code depends on locale-specific formatting, you should double check it after switching to Java 11.

Related

Should versions of java & javac be same in the system?

Should java & javac version be same in the system?
as I am having java -version
java version "1.8.0_251" Java(TM) SE Runtime Environment (build
1.8.0_251-b08) Java HotSpot(TM) 64-Bit Server VM (build 25.251-b08, mixed mode)
and javac -version
javac 1.8.0_181
Is there any issue if it's not the same version?
Should java & javac version be same in the system?
Not really, you can have as many java version as you want on one system, as long as you know which version you use when compiling or running application. If you have multiple java version in your system, be aware which version you put on your PATH environment variable to avoid a miss.
Your problem should be caused by multiple entry in your PATH environment variable that refering to java installation directory that has different version (note, java can be found in JDK and JRE installation, while javac can only be found in JDK installation).
Is there any issue if it's not the same version?
There should be no issue, as long as you don't use features that can only be found on the java version you use when compiling the program. In your case, the different is in build version, it's okay.

Running Netbeans for Java

I have a problem with running Netbeans for Java development..
When I try to run it I get
I checked the config located in <netbeans_dir>/etc/netbeans.conf and found following setting:
netbeans_jdkhome="/usr/"
So, I checked java and javac:
>>java -version
openjdk version "1.8.0_151"
OpenJDK Runtime Environment (build 1.8.0_151-8u151-b12-0ubuntu0.16.04.2-b12)
OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)
>>whereis java
java: /usr/bin/java /usr/share/java /usr/share/man/man1/java.1.gz
>>javac -version
javac 1.8.0_151
>>whereis javac
javac: /usr/bin/javac /usr/share/man/man1/javac.1.gz
So I updated netbeans_jdkhome to /usr/bin/javac. Now, when I try to run Netbeans I get
Cannot find java. Please use the --jdkhome switch.
I also tried to reinstall Java according to this instruction but once I did and changed jdkhome to /usr/lib/jvm/java-8-openjdk I still get Cannot find java.
What should I enter to run Netbeans properly?
Tools -> Java Platforms -> Add platform should display something like this:
Notice that Platform folder has small rectangle that indicates it can be chosen.

What is my real Java version on macOS?

I use macOS Sierra. When I do:
java -version
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-468-11M4833)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-468, mixed mode)
it seems that my java version is 1.6.0.
But when I look at System Preferences I find that my version is Java 8 Update 144. I want to use an R package that needs Java >=8.0. What have I done wrong?
The Java found in your command line is the one used to compile when using command line, first, try export JAVA_HOME=$(/usr/libexec/java_home -v 1.8) then check if the correct version shows up using java -version if not reinstall the newest java and run the command again

How to correctly select Java SDK in OS X?

I seem to have a bit of a mess. At the terminal, I get:
> java -version
java version "1.8.0_20"
Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)
Which is good (I'm trying to get Java 8 running). which java gives /usr/bin/java and:
> ls -al /usr/bin/java
/usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
Here I think I see where Java is actually installed.
Now my problem is that when I code a simple Java program and type:
System.out.println( System.getProperty("java.version") )
System.out.println( System.getProperty("java.vendor") )
I get the output:
1.6.0_65
Apple Inc.
So what am I doing wrong?
p.s. I am using Eclipse and while I can manually select a different JDK to use there, I would like Java 8 to be a system default. For example, I recently downloaded Eclipse Luna which does not open, giving the following error message:
Version 1.6.0_65 of the JVM is not suitable with this product.
Check the java home using:
/usr/libexec/java_home
Move to that directory:
cd /Library/Java/JavaVirtualMachines/jdk1.7.0_55.jdk/Contents/Home
cd bin
java -version
To switch the jdk 8 you will need to modify /usr/libexec/java_home to correctly point to java 8.

Java: how to ensure command-line compiling uses correct java version?

I'm transitioning from Java 1.6 to 7. It compiles, but gives an incompatible types error on my switch statement which is using a string (compiler complains that it should be an int type):
switch ("hello") {
...
}
thus, I don't think it's seeing version 7. However, when I type java -version I see:
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
When I type javac -version it shows javac 1.6.0_30. How to fix this?
Specify -source 1.7 or -target 1.7, whichever suits you best. If it isn't a 1.7 compiler, either of these will fail before compilation starts.
But I found I can switch the order by modifying my .bash_profile file above from export PATH=$PATH:$JAVA_HOME/bin to export PATH=$JAVA_HOME/bin:$PATH, which solved the issue.

Categories

Resources