Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Java read its default settings from the system

Tags:

java

Java is reading the locale, timezone and encoding information (and perhaps more) from the system it is installed on.

This often brings bad surprises (brought me one just yesterday). Say your development and production servers are set to have TimeZone GMT+2. Then you deploy on a production server set to GMT. a 2-hour shift may not be easy to observe immediately. And although you can pass a TimeZone to your calendars, APIs might be instantiating calendars (or dates) using the default timezone.

Now, I know one should be careful with these settings, but are easy to miss, hence make programs more error-prone.

So, why doesn't Java have its own defaults - UTF-8, GMT, en_US (yes, I'm on non-en_US locale, but having it as default is fine). Applications could read the system settings via some API, if needed.

Thus programs would be more predictable.

So, what is the reason behind this decision?

like image 399
Bozho Avatar asked Apr 18 '10 06:04

Bozho


People also ask

What is the default for system in Java?

The console is the default destination for output written to System. out or System. err and the default source of input for System.in . On most platforms the console is the command-line environment from which the Java program was initially launched, perhaps an xterm (Figure 1.1) or a DOS shell window (Figure 1.2).

What is system in read in Java?

in. read(byte[]) System is a class in java. lang package. in is a static data member in the system class of type input stream class.

Where are system properties stored in Java?

Starting in the source code of the getProperty method in the java. lang. System class, we see that the system properties are held in a private instance variable called props, and its values are filled by a native method called initProperties .


2 Answers

This isn't unique to Java. Many systems default to the system time zone. After all, what else can they do?

Time zones are a thorny issues, particularly when the application needs to deal with several time zones. That's why sites such as this one put everything in UTC.

As for your situation, it's hard to comment because the description is rather vague but it sounds like this is your error. If you save a date (without time zone) in one place at GMT+2 and then load it another at GMT then you've done something wrong.

like image 101
cletus Avatar answered Sep 24 '22 10:09

cletus


I don't see why having an extra set of defaults would make this easier. Surely those defaults would still have to be read from somewhere - so they'd presumably default from the operating system.

If you want to influence the defaults, there are usually system properties you can set when you launch your app, e.g.

java -Duser.timezone=Europe/London

etc.

Personally I think the problem isn't with the choice of default - it's the fact that it's used so easily. Even Joda Time (which I love in many, many respects) makes it too easy to accidentally use the default time zone. The same is true with encodings etc.

EDIT: Another option is to use a main method (or other early-initialization module) which calls

TimeZone.setDefault(TimeZone.getTimeZone("Etc/UTC"));

and likewise for other system-specific defaults.

like image 41
Jon Skeet Avatar answered Sep 20 '22 10:09

Jon Skeet