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?

Community
  • 1
  • 1
hudi
  • 13,245
  • 40
  • 125
  • 230
  • 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? – tim_yates Jul 31 '12 at 10:21
  • look here: http://stackoverflow.com/questions/11155799/how-to-get-output-of-groovy-script-in-java I have web application on application server with this code – hudi Jul 31 '12 at 10:24
  • So... You're not running it remotely then, you're just running it... – tim_yates Jul 31 '12 at 10:31

1 Answers1

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.

Andrzej Doyle
  • 97,637
  • 30
  • 185
  • 225
  • I try to use: System.setProperty and then call this variable: System.getenv()['USER'] but with no success – hudi Aug 01 '12 at 09:15
  • @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.) – Andrzej Doyle Aug 01 '12 at 09:40
  • 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 – hudi Aug 01 '12 at 09:57
  • 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. – Andrzej Doyle Aug 01 '12 at 10:05