I have files which are created via clojure
frontendapp.jar
backendapp.jar
and take an environment variable, APP_PORT
What I want to understand is how do I pass variables to the jar files so that they run with the variable APP_PORT?
My assumption is something like this
java -jar frontenapp.jar APP_PORT=8080
Am I correct in my assumption?
Apologies if the question is bad, it's my first time deploying a clojure application
Assuming a unixy shell like bash, you can do
$ APP_PORT=8080 java -jar frontendapp.jar
or perhaps more commonly:
$ export APP_PORT=8080
$ java -jar frontendapp.jar
In the first case, APP_PORT will only be set in the environment given to the java process. In the second case, with export, the variable will be set in the environments of all processes subsequently started from that shell.
Related
I invoke my code like so:
java -Dconfig=val -jar file-1.0-SNAPSHOT.jar
Inside the Java code, I can only obtain the "config" parameter if I use:
System.getProperty("config");
Is there a way to send environment variables to a JAR so that they will be available using
System.getenv("config") ?
Maybe send the environment variables in some other way.
Thanks in advance.
If you need to access config as an environment variable via System.getenv, you need to make sure that such environment variable is set before you run your Java app:
Windows
> SET config=val
> java -jar file-1.0-SNAPSHOT.jar
Linux
$ export config=val
$ java -jar file-1.0-SNAPSHOT.jar
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
This question already has answers here:
How to install Java application to my linux system
(2 answers)
Closed 6 years ago.
I'm new to Linux and Ubuntu environment.
I'm calling a jar file like this :
java -jar app.jar /somearg /anotherarg
But I find this ugly and I want to call my jar file like :
MyApp /somearg /anotherarg
So I think I have to set a environment variable like MyApp = java -jar app.jar.
But I don't know how can I do it.If anyone can help me I'll be very pleased.Thanks.
Using
java -jar app.jar /somearg /anotherarg
instead of
myapp /somearg /anotherarg
saves you only 2 words, so not that much. Anyway if you run your application frequently it is a good idea to provide an alias. If you are using bash shell (echo $SHELL shows something like /bin/bash) then here's the command you can paste in your terminal:
alias myapp="java -jar /path/to/your/app/app.jar"
After that you can use
myapp /somearg /anotherarg
It is important to provide the whole path to your app.jar file if you run it from different locations. Also if you want that your alias is permament (i.e. it is always after you log in) just add the same line at the end of ~/.bashrc file, using for example:
pico ~/.bashrc
PS. You don't need slash symbol to provide parameters. In unix systems it is more common to use "-" to set argument options.
PSS. Unix systems are case sensitive and it is typical that command line programs are all written in lowercase.
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 :-)
I am writing a Java program and I need to set a temporary classpath that includes my package. The package is on my Ubuntu desktop, which I am importing as /home/gaurav/Desktop. Do you have
any idea how to set the Java CLASSPATH temporarily?
You set the Java classpath on Ubuntu the same way as you do on any Linux / UNIX platform (or indeed on Windows ... modulo some minor syntactic differences). There are two ways:
$ java -cp <classpath> some.ClassName arg1 arg2 ...
or
$ export CLASSPATH=<classpath>
$ java some.ClassName arg1 arg2 ...
where <classpath> is a sequence of pathnames with ':' separators.
For more details refer to the manual entry for the 'java' command; e.g. here and here.
If you don't understand export CLASSPATH=... read the Ubuntu manual entry for bash, paying attention to what it says about setting variables, environment variables, and the export built-in shell command. (Hint: $ man bash.)
This is temporary. To make it permanent, add the line to the relevant shell init script; see man bash for details.
how do i get details for my path which path i have set
The classpath is a list of pathnames of directories and JAR files that you want the JVM to search in order to find the classes it needs to run your application. You'll need to figure that out yourself ... or (re-)read the documentation of whatever it is you are trying to run.
If you want to run a java program from the desktop, you have three choices.
The easy choice is to write a small shell script and put that on the desktop. The smallest example might be:
#!/bin/sh
java -cp YOUR_CLASSPATH YOUR_CLASS_NAME "$*"
Next comes the use of 'jarjar' or 'shade' to make one big jar containing all your dependencies, and then run that with java -jar. (As a sub-option, if it's really just for you, you can make a jar with a META-INF/MANIFEST.MF containing a class path with absolute pathnames.)
The more complex choice is to learn to use JNLP to build a launchable item.
IIRC you can control the classpath with either an environment variable or a command-line option to java.