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.
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?
yes this is irrespective of OS and you can use it in JSP.
Related
I have my application config have a property like
#Value("#{new Boolean(environment['spring.profiles.active']!='dev')}")
private boolean is24;
Now in two runtime env application-prod where VM variable is set like
-Dspring.profiles.active=prod
and an env application-dev where VM variable is like
-Dspring.profiles.active=dev
but the value of is24 is always coming as true. Any idea how can I fix it?
While I posted a comment how to achieve a goal you might have, this doesn't answer your question, and so here is the answer:
Your Spring Expression is (if working correctly) checking the content of the environment variable spring.profiles.active, while you are setting the active spring profile with a Java system property. Those two do not automatically reflect each other.
You can set the Spring profile both via environment variable and System property, but this is not a general behaviour of environment variables and System properties.
Summary: Id like to alter the way .hprofs are named when automatically created to incorporate machine's hostname + process PID
Long version: According to documentation (https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/clopts001.html) you can pass an option to the VM to create heapdumps under certain conditions (HeapDumpOnOutOfMemoryError), and can further customize this behavior by specifying a different path for the resulting .hprof file. You can change the name/path. What I would like to know is if its possible to make this option (HeapDumpPath) incorporate the hostname+PID to have uniquely named .hprofs (to be saved in a centralized location, but thats another scope). Could it be done? Can HeapDumpPath accept a parameter to get the hostname?
It doesn't look like there are options to inject values into the path. Depending on your shell you should be able to at least insert the hostname. The PID may not be possible since it is created after the process starts. You may be able to use a different variable like the application name though. In bash it would look something like this:
java -XX:+HeapDumpOnOutOfMemoryError -mn256m -mx512m ConsumeHeap -XX:HeapDumpPath=/disk2/dumps/$(hostname)/${APPLICATION_NAME}
Managed to do it just by using the Windows Environment Variable COMPUTERNAME in the wrapper.conf file for that particular Java VM. The full line is:
wrapper.java.additional.32=-XX:HeapDumpPath=./%COMPUTERNAME%.hprof
Title is pretty clear for my question, suppose I deployed an java application in tomcat/glassfish now for some reason I need java path used by respective server so is there any way to get java path which is used by server?
You can get the java.home System Property, which should be exactly what you want:
System.getProperty("java.home");
This question already has answers here:
Towards the "true" definition of JAVA_HOME
(5 answers)
Closed 4 years ago.
Why does ActiveMQ provider needs JAVA_HOME env variable to be set
to a jdk location.
What does it use that variable for?
If it needs to run java command and relating commands, why bother with JAVA_HOME.
If PATH env variable is set correctly, java command is always available.
Or am I missing some points?
EDIT:
I don't think my question is a duplicate,
I'm asking pretty precise points on that variable, also in correlation to $PATH env variable and ActiveMQ.
The actual REASON for the JAVA_HOME is that many apps need to find more than just executables in the path, some java apps need to find the jar file they are supposed to use as a library -- Remember that there may be more than one java installation installed and that any given app may want a specific java version that isn't the one on the path.
Consider an app that uses a specific version of java that isn't on the path. You can specify a path to the java.exe, but how does the app know which set of libraries it's supposed to be using if it needs to interact with them in some way other than just using them? It could probably figure it out from the environment, but might be inaccurate and would definitely be platform dependent.
There is usually more than one way to set give the app this information, JAVA_HOME is one, often they don't need it at all.
If "java -jar" is run from a command line,
is there a way to set local dos variable from java program so that
after java is exited, it can still be present in the same session?
example
(cmd)
c:\java package.Class
/*then in program you do something like
'System.setVariable("name","value");'
*/
// java exited
echo %name%
value
No. Processes cannot modify their parents' environment.
The best thing you can do is cheat a little and do either of the following:
Let the Java program write out a batch file in some known location and call it afterwards to set variables. Since batch files run in the current cmd process they can alter environment variables there.
Let the program output name/value pairs or complete set commands, catch the output and set the variables yourself afterwards. Goes wrong as soon as you want or have other output, I guess.
It's possible to set environment variables, according to question 2121795. However, I've never tried these methods so can't verify if they work.
If they do work, remember that setting an environment variable will not take effect in the current session (you'd need to restart the cmd window).
Also interesting is this question which has answers explaining how to use the Preferences API to modify the registry. I guess you should be able to modify environment variables via this route (didn't check thorougly).