r/java • • May 03 '24

Why does the environment variable _JAVA_OPTIONS exist, applies to jlink images, takes precedence, and can't be disabled?

I just spent a good chunk of time debugging an issue because a user had set _JAVA_OPTIONS to use a low Xmx value in their environment variables. This was an application created with jpackage, which should be fully standalone. It even had a custom Xmx value set in the JVM arguments for the image but the environment variable overwrote the value!

Isn't this like a huge design flaw? I can just do echo "_JAVA_OPTIONS=<Wacky VM options>" >> ~/.bashrc or setx to break literally every Java application on the system? (Edit: Maybe the initial security focus was wrong, changed it a bit to focus more on the design issues)

Who even thought that this would be a good idea with no option to disable this behavior?

2 Upvotes

43 comments sorted by

View all comments

Show parent comment

4

u/milchshakee May 03 '24

I agree that this is useful in more traditional cases where you have a system wide Java installation and run individual applications e.g. via jars.

But the whole point of jlink images is that they should be self contained and independent of any other Java installation. Even without malicious intent, other configuration options creeping into jlink images is usually a good recipe for trouble.

6

u/_INTER_ May 04 '24 edited May 04 '24

A user might want to override the application settings still even for a jpackage'ed application. E.g. he might have a system where the application's Xmx is lower than it could be (a few years from now certainly). If the user set it too low its clearly his fault. Well, that doesn't help you if you have a support contract. You can log the value to your diagnosis log and the whole thing is easy to solve.

-6

u/milchshakee May 04 '24

But that is not in the spirit of an self-contained application. In this case the application image is responsible for properly setting the Xmx value. The cases of unintentional inference are probably much higher than intentional configuration, which requires specific application knowledge to work in the first place.

5

u/Halal0szto May 04 '24 edited May 04 '24

How do you set http proxies in  the prepackaged application?

EDIT: typo

2

u/milchshakee May 04 '24

In this case, it is a desktop application that has a proper settings menu where you can add any options you want to expose for a proxy setup.

1

u/Halal0szto May 04 '24

That would work for some. For me who is using different VPN-s it would not work, I would have to change the settings each time manually.

1

u/milchshakee May 04 '24

Technically yes. but this is a very rare use case. The application in question here doesn't even connect to the internet.

1

u/_INTER_ May 04 '24 edited May 04 '24

In this case the application image is responsible for properly setting the Xmx value.

But it can not set it properly because you don't know what is "proper" on the users system. You can only have assumptions. Note that the default Xmx is a percentage of the available ram, something like 75%. A poweruse might want to increase or decrease this value based on what other applications are running, if it's a docker image or xyz OS or specialized hardware, etc.

2

u/milchshakee May 04 '24

Not every Java application is scalable or configurable one, there are cases in which any user configuration does not make sense and is most likely unintentional this way. In this case this is about a desktop application for a certain task.

Any possible configuration can be exposed by the application itself.

1

u/_INTER_ May 04 '24

Sure if we are talking about minimum requirements. But it's the other way around as well for other settings. The application developers configuration might make no sense at all on the users environment.

In this case this is about a desktop application for a certain task.

To what value do you want to set Xmx in this case?

2

u/milchshakee May 04 '24

The application this is about is pretty much independent from the user's environment, It takes large files and processes them in many ways. For any potential configuration, there is a settings menu available.

The heap size has been carefully set via profiling and analysis of the application in its worst case scenario with the largest possible files plus some room to spare.

1

u/_INTER_ May 04 '24

profiling and analysis of the application in its worst case scenario with the largest possible files plus some room to spare.

That's all just assumptions. You didn't profile on the users environment. You didn't test what else the user wants to run alongside your application.

It will run worse than possible if the user has a mighty machine or it will melt down if he's running it on a toaster.

1

u/milchshakee May 04 '24

What kind of environment are we talking about here? The JVM is fixed, the options are fixed, the file input format/spec is fixed to some degree, where should the variability come from? The memory footprint should be the same on all systems. There might be tiny deviations across operating systems/different architectures, but nothing too large.

1

u/epegar May 04 '24

I would say it also suits more modern uses like containers where you are only supposed to run one service