¿Cómo establecer propiedades en el script Groovy que es ejecutable desde el código Java?

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? (Aquí I found that it isn't possible). Or is there some safe ways to send this properties from Java to Groovy?

preguntado el 31 de julio de 12 a las 11:07

Que quieres decir con "...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? -

mira aquí: stackoverflow.com/questions/11155799/… I have web application on application server with this code -

So... You're not running it remotely then, you're just running it... -

1 Respuestas

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.


Debe tener en cuenta que el entorno 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 preferiblemente.

Respondido 01 ago 12, 10:08

I try to use: System.setProperty and then call this variable: System.getenv()['USER'] but with no success - hudi

@hudi - just noticed that you're checking the entorno for an application setting. This is fundamentally not the right thing to do, you should check the Propiedades del sistema instead. (The environment is basically "how is Windows set up?", which understandably can't be modified by the application.) - andrzej doyle

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

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

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.