Setting global Java options in Bash - java

I want to pass -Dtangosol.coherence.wka=localhost to the JAVA options.
When I was trying java -Dtangosol.coherence.wka=localhost -jar myapp.jar, it works. The flag successfully passed into the application.
But, I want to use bash script to set the flag into the JAVA global variable, so I don't need to pass the flag every time running the application.
I tried:
export JAVA_OPTS="-Dtangosol.coherence.wka=localhost"
But, it seems doesn't work. Did I missing something?

Related

not able to execute shell script

I've scenario where I execute my java code using shell script.
When I run that shell script from autosys, it runs fine. But when I try to run it manually from linux box, it says Java not found error.
And when I check manually java version in linux box, using java -version it says java not there.
How its possible that, same shell script is triggered by Autosys but not manually ?
May I know please, what type of configuration is this ? I'm I missing anything ?
It is possible that the Java could be installed for the autosys user only in which case it will not be accessible by another user.
The autosys user could have had some PATH variable (perhaps in the profile) that points to the Java binary, which might not be present in your current user. You might want to check your ~/.profile file (or) ~/.bashrc, ~/.bash_profile to see if there is PATH variable.
To debug, just include which java in your shell script and check the output when the autosys runs it. This will give you the path of the java binary.
(or) Simply login as your autosys user and execute the same script.
May be, you might also be missing some bootstrap commands before executing your script (like export PATH etc).

Adding JVM system properties to running JVM?

I have a running Java program which I would like to control at the command line by changing a custom system property which I'm listening for changes to inside the application, thereby not requiring a restart of the application for the changes to take affect.
I understand that it's possible to change some of the default JVM properties using jinfo but it doesn't seem to work for custom properties which I set at compile time using the -D flag.
For example, if I start a JAR using the following command line arguments:
java -Dfoo=1 -jar my_jar.jar
attempting to call the following fails:
jinfo -flag foo=2 'pid'
Is there something I'm missing or is this simply not possible?
This utility is unsupported and might not be available in future releases of the JDK. Official Doc
Not all of the available parameters can be changed. Reference
IMO, Recommended way is to have some event-listener or service call to change the flags of running application.
I had used some system properties using jinfo so not sure if custom flags can be changed during jar execution. Try some custom boolean flag using command jinfo -flag +<customBooleanFlag> <PID> to check if its working.

Access environment variable in .conf file for spring boot app

I have set environment variable by executing the command
export test=abcd
I can see test=abcd when I run printenvcommand
I have deployed a springboot.jar application and I am passing the JAVA_OPTS from the springboot.conf file.
JAVA_OPTS='-Dspring.profiles.active=aaa -Denv=$test'
I started the app by service springboot start . When I check the process, env variable doesn't have the value of $test environment variable.
/usr/bin/java -Dsun.misc.URLClassPath.disableJarChecking=true -Dspring.profiles.active=aaa -Denv=.
How can I access the environment variable in the conf file? I read somewhere the environment variables will be stripped off when run as service. Basically I want to run as service springboot start which internally executes the below command
java -Dspring.profiles.active=aws -Denv=${whatever is set for env environment variable} -jar springboot.jar
I have tried the below configurations but nothing worked
JAVA_OPTS='-Dspring.profiles.active=aaa -Denv='$test
JAVA_OPTS='-Dspring.profiles.active=aaa -Denv='${test}
JAVA_OPTS='-Dspring.profiles.active=aaa -Denv=${test}'
JAVA_OPTS="-Dspring.profiles.active=aaa -Denv=$test"
Be careful about your quotes. Assuming that you use a "normal" shell, variables won't be substituted in single quotes.
java -Dspring.profiles.active=aws -Denv="$myvariable" -jar springboot.jar should lead to env being available in the JVM, no matter if you run it as a service or not.
If you can't get it to work, try to specify a hard coded value like this java -Dspring.profiles.active=aws -Denv=foo -jar springboot.jar. If env is now available in the JVM, your problem is with your shell or run mechanism. Verify that the user who runs the command (i.e. do you use sudo?) has the variable set.
I had the same problem where my .conf was referencing an environment variable which was in the .bashrc.
What I found out is:
The problem is service strips all environment variables but TERM, PATH and LANG which is a good thing. If you are executing the script directly nothing removes the environment variables so everything works.
https://unix.stackexchange.com/questions/44370/how-to-make-unix-service-see-environment-variables
One solution would be to install your app as a systemd service:
https://docs.spring.io/spring-boot/docs/1.3.x-SNAPSHOT/reference/html/deployment-install.html
Or another way is to use docker and you can specify extra configuration in the docker file, like loading a file which contains your environment variables.
As those solutions where not available in my case I ended up with having the value in the .conf file, like: -Denv=prod

Java System.getEnv()

In mac OSX and in Linux CentOS, I insert a new system environment variable (i.e. "MYAPP") using .bashrc & .bash_profile. I even restarted my laptop (mac) and my server (linux).
When I use the command line "env", that environment variable showed with the correct value. But somehow every time I try to get it in a Java app (desktop app or web app or EJB or servlet any other java app) in either mac or linux, that environment variable ("MYAPP") is not retrieved.
I tried to iterate through the entire environment variables that Java can retrieve and it turns out that it retrieves every environment variables other than "MYAPP". This is very odd.
Anyone know how to solve this?
Did you export MYAPP=...? Exporting the variable makes it available to child processes, like java being run by your shell.
In Linux, if you only set the variable (or export it) in a bash session, it will be available to a kind of "sub" session, which is only available to the command you just executed, and nothing else.
You could probably use the dot operator in bash (also called "source" command). From the page:
When a script is run using `source' it runs within the existing shell, any variables created or modified by the script will remain available after the script completes.
So you could try doing . export VARIABLE=value, and then running your java program. This is similar to setting a variable in a Windows terminal, and then opening a new terminal and expecting the env var to be there. It won't.
This way, you are telling bash "this command should be available in this specific session (the session's process)". OTherwise you are telling it "set this env var for the bash session that will end after I run this export command" thus, it won't exist when you run your Java program.
After having defined and exported the environment variable. Launch your IDE from the same Terminal.
Try to write
"$System.env.STOREPWD"

getenv() not working

I've created a stand alone a java application in Ubuntu 10.04 using Netbeans 6.9. I'm not able to use use the getenv() command in Netbeans, though if i create a separate java file in gedit and compile it in the terminal then it gives the desired output.
System.out.println(System.getenv("TRGRAPH"));
The above code when executed through the terminal gives the desired output but the same code if i try to run in Netbeans then it returns a null string.
Can anyone tell me how to get the output using netbeans??
You need to launch Netbeans from the same terminal after you have set and exported TRGRAPH.
Example, in a terminal:
$ export TRGRAPH=foo
$ netbeans&
I use Eclipse, not NetBeans, but I bet they are similar. Look for a dialog that controls how your program gets launched. This dialog probably has a place where you can specify environment variables that should be set when your app is launched.
The other alternative is to set the environment variable before you launch Netbeans.
It means that TRGRAPH is not defined in the process. The environment gets inherited from the environment of Netbeans. Make sure, that Netbeans gets the variable, e.g., by starting it from a command line or by invoking it using a shell script sourcing your .bashrc (or wherever you define TRGRAPH).
Alternatively, you can start an external Java process using the ProcessBuilder and pass it any environment you like. Quite complicated, but very flexible.

Categories

Resources