How can I find a complete list of platform-specific hooks in the JDK, such as timezone, coding, line endings, etc.?

We always want our development / testing environment to be the same as ours, but we can often fall into the trap of using JDK functions that do not reflect the environment too much (for example, the parameter of the required method) or can be subtle for detect and keep abreast. For instance:

  • public String(byte bytes[]) uses default encoding
  • new Date()/Instant.now() uses sytem timezone
  • System.out.printf("%n") uses the end of the platform line

Some of them can be controlled by JVM parameters, such as -Dfile.encoding=UTF-8.

But how to find all such errors?

+4
source share
3

getchas .

, java:

import java.awt.GraphicsEnvironment;
import java.util.Map;
import java.util.Properties;

public class Sof39189179 {

    public static void main(String[] args) {

        Map<String, String> sysenv = System.getenv();

        for(String key: sysenv.keySet())
            System.out.println( key +  ": " + sysenv.get(key));

        Properties properties = System.getProperties();

        for(Object key: properties.keySet())
            System.out.println(key + ": " + properties.get(key));

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
        System.out.println("headless: " + ge.isHeadless());
    }
}

, ( ):

M2: $M2_HOME/bin
JAVA_HOME: /opt/local/jdk-1.8.0_51
LANG: en_US.UTF-8
CATALINA_HOME: /opt/local/tomcat-8.0.24

...

---------------------
java.runtime.name: Java(TM) SE Runtime Environment
sun.boot.library.path: /opt/local/jdk-1.8.0_51/jre/lib/amd64
java.vm.version: 25.51-b03
java.vm.vendor: Oracle Corporation
java.vendor.url: http://java.oracle.com/
path.separator: :
java.vm.name: Java HotSpot(TM) 64-Bit Server VM
file.encoding.pkg: sun.io
user.country: US
sun.java.launcher: SUN_STANDARD
sun.os.patch.level: unknown
java.vm.specification.name: Java Virtual Machine Specification
user.dir: /home/rudolf/workspace/neon/sof39189179
java.runtime.version: 1.8.0_51-b16
java.awt.graphicsenv: sun.awt.X11GraphicsEnvironment
java.endorsed.dirs: /opt/local/jdk-1.8.0_51/jre/lib/endorsed
os.arch: amd64
java.io.tmpdir: /tmp
line.separator: 

java.vm.specification.vendor: Oracle Corporation
os.name: Linux
...

java.awt.printerjob: sun.print.PSPrinterJob
file.encoding: UTF-8
java.specification.version: 1.8
java.class.path: /home/rudolf/workspace/neon/sof39189179/bin
user.name: rudolf
java.vm.specification.version: 1.8
java.home: /opt/local/jdk-1.8.0_51/jre
sun.arch.data.model: 64
user.language: en
java.specification.vendor: Oracle Corporation
awt.toolkit: sun.awt.X11.XToolkit
java.vm.info: mixed mode
java.version: 1.8.0_51
java.ext.dirs: /opt/local/jdk-1.8.0_51/jre/lib/ext:/usr/java/packages/lib/ext
sun.boot.class.path: /opt/local/jdk-1.8.0_51/jre/lib/resources.jar:/opt/local/jdk-1.8.0_51/jre/lib/rt.jar:/opt/local/jdk-1.8.0_51/jre/lib/sunrsasign.jar:/opt/local/jdk-1.8.0_51/jre/lib/jsse.jar:/opt/local/jdk-1.8.0_51/jre/lib/jce.jar:/opt/local/jdk-1.8.0_51/jre/lib/charsets.jar:/opt/local/jdk-1.8.0_51/jre/lib/jfr.jar:/opt/local/jdk-1.8.0_51/jre/classes
java.vendor: Oracle Corporation
file.separator: /
java.vendor.url.bug: http://bugreport.sun.com/bugreport/
sun.io.unicode.encoding: UnicodeLittle
sun.cpu.endian: little
sun.cpu.isalist: 
headless: false

, , . , , .

System.out.printf( "% n" )

, .. , - :

String newline = (String) System.getProperties().get("line.separator");
System.out.println("newline: " +  newline);
+2

, -XX:+PrintFlagsFinal -XshowSettings:all . (, /os ) jvm env/system.

os, , / os, , ..

+2

: , .

, let ignore strict math . , API.

  • Start a new topic? Depending on OS limitations.
  • Want to plan a task accurate to the nanosecond? Given the accuracy of the timer available on the platform.
  • File names? Depends on what the file system supports.
  • Open a socket? Available port ranges are configurable by the OS.
  • ...

As they usually advise: read the API documents you use and find out which OS features support them.

+2
source

Source: https://habr.com/ru/post/1652745/


All Articles