How to pass property to JVM without using -D syntax? - java

I need to pass properties to a java execution without using the -D syntax, is it possible?
Normally I run:
java -DrandomProperty=randomVal MyPackage.MyClass
But now, I cannot pass properties to the java command (I have no access to the actual call because it's nested) and I can neither pass them as environment variables (I do not have access to MyPackage.MyClass sources and thus I can't replace System.getProperty("randomProperty") calls with System.getenv("randomProperty").

One way is to set the environmental variable _JAVA_OPTIONS. The JVM reads this environmental variable to get the default JVM parameters to start with. Setting the _JAVA_OPTIONS to -DrandomProperty=randomVal will cause all JVMs to start as though the flag was passed into the command line.
http://progexc.blogspot.co.uk/2013/12/what-i-discovered-while-trying-to-pass.html

Related

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

passing argument to Runtime.getRuntime().exec() with an option at the beginning

I'm trying to pass a command like the following to Runtime.getRuntime().exec() where the option should be at the beginning.
option="something" ./test.sh a b
Whatever I'm trying, I get this exception:
Cannot run program "option="something"": error=2, No such file or directory
Any ideas?
Doing option="something" only works in shells to set an environment variable for this specific process. To do this in java you must use one of the overloads of exec that take environment variables as a parameter.
Such as:
https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec(java.lang.String[],%20java.lang.String[])
or use ProcessBuilder:
https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html
Edit:
You should note that explicitly setting environment variables for the new process, causes all environment variables in the current program to be ignored. If you want to pass the current environment variables you must include them yourself.

Run java with properties from environment variable

In my app i need send http requests via proxy. In terminal i start it by this:
java -Dhttp.proxyPort=**** -Dhttp.proxyHost=***.***.***.*** -jar app.jar
What environment variable i should use for starting on my apps without -D options, like
java -jar app.jar
OS Linux. Java 7.
Thx!
PS already tried JAVA_OPTS, JAVA_OPTIONS, _JAVA_OPTIONS, JAVA_TOOL_OPTIONS...
Java has two separate ways to pass parameters to programs:
Properties, which are typically specified in the command line arguments (as in your first example), loaded from files or manually added by code.
Environment Variables, which are determined by settings in your operating system.
These two concepts are separate; the former doesn't affect the latter and vice versa. As such, you cannot set a property by means of an environment variable.
Other options include loading a .properties file during runtime (assuming your proxy hasn't already been initialized at that point) or putting the full command (-D arguments and all) in a shell script for easier launching.
Nothing is going to work with the standard Java executable. The Java executable does not recognize any environment variables for setting general JVM options.
All those things that you have tried are conventions used by 3rd-party tools, scripts and launchers.
As far as I am aware, the only Java-specific environment variable that the Java executable pays attention to is CLASSPATH, and that is ignored when you run java with the -jar option.
Having said that, there is nothing1 stopping you from:
creating a wrapper script called java and putting it on your search path ahead of the standard java executable, or
creating a shell alias called java.
Such a wrapper or alias could get JVM options from an environment variable, and you could call it anything you wanted to.
1 - ... apart from good sense :-)

How to use all the Unix environment variables?

Using Java (JSch API) I am trying to execute an Unix command on a remote machine. Now, to successfully execute this command I need to use all the environment variables already set on the remote box.
I can use export <variable> command to set the variables on runtime. But as the number of such variables is quite large I am wondering if there is any better way to use the variables in runtime.
Can anyone help please or should I explain a bit more?
You can save variables into a file:
set > /tmp/vars
echo "A=120" >> /tmp/vars
and then "import" the variables with dot in a script like this:
set -a
. /tmp/vars
mycommand
The work around I found was to use
set | mycommand
Set will manually call the initialization process and it will add environment variables into the scope.
JSch calls bash with -c. This causes .bashrc not to be initialized inside of the bash scope. Set will read the .bashrc file and other config files.

Giving environment variables as arguments

I have a jar file that gets arguments from commandline and I want to give parameter that contains environment variable. Something like below:
java -jar MyDev.jar -f %PROJECT_HOME%/src/test
But in above case program creates a directory named %PROJECT_HOME% however I want that PROJECT_HOME value in system is /home/jack path. And program should follow /home/jack/src/test not %PROJECT_HOME%/src/test path.
How can I do that ?
Are you running this in a Unix shell? If so, I suspect you just want:
java -jar MyDev.jar -f ${PROJECT_HOME}/src/test
Using % is the Windows way of specifying environment variables - which doesn't appear to fit with a home directory of /home/jack...
The component responsible for environment variables substitution is the shell/command line processor (cmd.exe on Windows).
I wrote the following main method:
public static void main(String[] args) {
System.out.println(args[0]);
}
When I pass "%PATH%" as an argument, running it from within Eclipse prints out %PATH%. Running it from the command line prints out the actual path environment variable.
Note that you can access environment variables from your Java code by using System.getenv().
For example, System.out.println(System.getenv("PATH")) prints out the actual path variable both from Eclipse and from the command line.
One very likely cause for this could be that the variable PROJECT_HOME is not defined or has a misspelled name. Hence, unless you have already done so, you should do echo %PROJECT_HOME% right before you start the java program in order to ensure that the variable is defined.

Categories

Resources