0

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.

2 Answers 2

16

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?

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Matt, Thanks for your reply. what is difference between system properties and environment variables? Thanks!
1

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

2 Comments

Hi Satya, Thanks for your reply. which one is irrespective of OS> System.getenv() or System.getProperty()? Thanks!
@user1016403 they both are. Java is meant to be completely OS-independent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.