0

In my Groovy script I have this code:

def username = System.getenv()['USER']
def password = System.getenv()['PASS']

It is because I can't use this information as parameter to Groovy script, because other users shouldn't know it. When I run this script I set this parameter in my system. Now I have a problem. I have a Java application which runs Groovy script remotely. Is there some way that I can set enviromental variable in Java code? (Here I found that it isn't possible). Or is there some safe ways to send this properties from Java to Groovy?

3
  • What do you mean by "...which run groovy script remotely"? How is the Java running the groovy script? Also, setting the username/password in the system environment means you can only run a single user at a time? Commented Jul 31, 2012 at 10:21
  • look here: stackoverflow.com/questions/11155799/… I have web application on application server with this code Commented Jul 31, 2012 at 10:24
  • So... You're not running it remotely then, you're just running it... Commented Jul 31, 2012 at 10:31

1 Answer 1

1

If you're launching the groovy script by the lowest common denominator of Runtime.exec (or similar), then you can specify the environment in one of the overloaded methods:

Executes the specified string command in a separate process with the specified environment.

...

envp - array of strings, each element of which has environment variable settings in the format name=value, or null if the subprocess should inherit the environment of the current process.

If on the other hand you're invoking the Groovy script within the same Java process, then it will have the same properties as the running process. So simply calling System.setProperty("USER", xxx) before invoking the Groovy script will mean this property is visible to your Groovy logic.


You should note that the environment is an operating-system level thing; a measure of the properties of the OS on which the process is running.

If you're looking for application-level settings, you really ought to be checking System.properties instead.

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

4 Comments

I try to use: System.setProperty and then call this variable: System.getenv()['USER'] but with no success
@hudi - just noticed that you're checking the environment for an application setting. This is fundamentally not the right thing to do, you should check the system properties instead. (The environment is basically "how is Windows set up?", which understandably can't be modified by the application.)
I cant use systemProperty because in article what I mention in my question is written then these can be set with commandline to: -Dpropertyname=value what I dont want
If you want to set properties for the Groovy script, then you should have the Groovy script check system properties. If it's going to check environment variables, then you'll need to change the OS' environment variables (which you can't do programatically) - it's that simple.

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.