user1016403
user1016403

Reputation: 12621

getting environment variable's value in java?

I have set an environment variable in windows by name "sample". Can I get this environment variable's value using below code?

System.getProperty("sample");

in UAT environment I have LINUX OS. Can I set the same environment variable in LINUX OS and retrieve the same using System.getProperty("sample")?

I mean does System.getProperty("sample"); retrieves irrespective of the operating system?

Also, I would like to get the environment variable's value in JSP.I know it is not recommended to keep logic in jsp but still, I need to use it. Can I use System.getProperty("sample"); in jsp? Please help me.

Upvotes: 0

Views: 11488

Answers (2)

Matt Ball
Matt Ball

Reputation: 360046

Use System.getenv() to retrieve environment variables, not System.getProperty(). The latter is for (as the name implies) system properties.

What's the difference? Java system properties and environment variables


If you need to access an environment variable in a JSP, you're doing something wrong. At the very least, use a servlet. More reading: How to avoid Java code in JSP files?

Upvotes: 16

Satya
Satya

Reputation: 8881

yes this is irrespective of OS and you can use it in JSP.

Upvotes: 1

Related Questions