One Star

[resolved] How to get an environment variable from the system into a context var?

I am looking to implement the following scenario:
Setup a system environment variable on the server that Talend is running on. The value of this variable will be a path to a folder containing XML files that I use for various purposes. At runtime in my job, I would like to use some Talend component to retreive the value of the environment variable and store it in a context variable. I can then use that context variable as the leading source string for my tXMLInput.
I see a tSetEnv, but I do not see a tGetEnv comnponent.
Any easy way to do this?
thanks,
Dave
1 ACCEPTED SOLUTION

Accepted Solutions
Employee

Re: [resolved] How to get an environment variable from the system into a context var?

Hi;
To retrieve your environment variable use the following into a tJava (begin of your Job) like this : context.MyPath = System.getProperty("SYSTEM_ENV");
Best regards;
10 REPLIES
Employee

Re: [resolved] How to get an environment variable from the system into a context var?

Hi;
To retrieve your environment variable use the following into a tJava (begin of your Job) like this : context.MyPath = System.getProperty("SYSTEM_ENV");
Best regards;
One Star

Re: [resolved] How to get an environment variable from the system into a context var?

Hi;
To retrieve your environment variable use the following into a tJava (begin of your Job) like this : context.MyPath = System.getProperty("SYSTEM_ENV");
Best regards;

Thanks! Sounds like tSetEnv needs a brother (tGetEnv) Smiley Happy
Dave
One Star

Re: [resolved] How to get an environment variable from the system into a context var?

Hi;
To retrieve your environment variable use the following into a tJava (begin of your Job) like this : context.MyPath = System.getProperty("SYSTEM_ENV");
Best regards;

Thanks! Sounds like tSetEnv needs a brother (tGetEnv) Smiley Happy
Dave
In the following code, the commented out line works, but sun java documentation says that getenv is deprecated. The uncommented line (getProperty) only works when I query for java.xxx properties (ex: java.io.tmpdir), but returns null when I use system environment vars. System.getProperty() is a java function.

Also, I assume that this same command can be used against linux?

context.CCRHome = System.getProperty("CCR_HOME");
//context.CCRHome = System.getenv("CCR_HOME");

???
Dave
Community Manager

Re: [resolved] How to get an environment variable from the system into a context var?

Hello Dave
Please use the following routine, it both works on windows and Unix/Linux.
// template routine Java
package routines;
import java.io.*;
import java.util.*;
public class getEnv {
static String value = null;
public static String getEnvValue(String key) {
try {
Properties p = getEnv.getEnvVars();
value = p.getProperty(key);
} catch (Throwable e) {
e.printStackTrace();
}
return value;
}
public static Properties getEnvVars() throws Throwable {
Process p = null;
Properties envVars = new Properties();
Runtime r = Runtime.getRuntime();
String OS = System.getProperty("os.name").toLowerCase();
if (OS.indexOf("windows 9") > -1) {
p = r.exec("command.com /c set");
} else if ((OS.indexOf("nt") > -1) || (OS.indexOf("windows 2000") > -1)
|| (OS.indexOf("windows xp") > -1)) {
p = r.exec("cmd.exe /c set");
} else {
// it is Unix os.
p = r.exec("env");
}
BufferedReader br = new BufferedReader(new InputStreamReader(p
.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
int idx = line.indexOf('=');
String key = line.substring(0, idx);
String value = line.substring(idx + 1);
envVars.setProperty(key, value);
}
return envVars;
}
}

Best regards

shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: [resolved] How to get an environment variable from the system into a context var?

Hello Dave
Please use the following routine, it both works on windows and Unix/Linux.
// template routine Java
package routines;
import java.io.*;
import java.util.*;
public class getEnv {
static String value = null;
public static String getEnvValue(String key) {
try {
Properties p = getEnv.getEnvVars();
value = p.getProperty(key);
} catch (Throwable e) {
e.printStackTrace();
}
return value;
}
public static Properties getEnvVars() throws Throwable {
Process p = null;
Properties envVars = new Properties();
Runtime r = Runtime.getRuntime();
String OS = System.getProperty("os.name").toLowerCase();
if (OS.indexOf("windows 9") > -1) {
p = r.exec("command.com /c set");
} else if ((OS.indexOf("nt") > -1) || (OS.indexOf("windows 2000") > -1)
|| (OS.indexOf("windows xp") > -1)) {
p = r.exec("cmd.exe /c set");
} else {
// it is Unix os.
p = r.exec("env");
}
BufferedReader br = new BufferedReader(new InputStreamReader(p
.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
int idx = line.indexOf('=');
String key = line.substring(0, idx);
String value = line.substring(idx + 1);
envVars.setProperty(key, value);
}
return envVars;
}
}

Best regards

shong

Thanks shong.
One Star

Re: [resolved] How to get an environment variable from the system into a context var?

Hello Dave
Please use the following routine, it both works on windows and Unix/Linux.
// template routine Java
package routines;
import java.io.*;
import java.util.*;
public class getEnv {
static String value = null;
public static String getEnvValue(String key) {
try {
Properties p = getEnv.getEnvVars();
value = p.getProperty(key);
} catch (Throwable e) {
e.printStackTrace();
}
return value;
}
public static Properties getEnvVars() throws Throwable {
Process p = null;
Properties envVars = new Properties();
Runtime r = Runtime.getRuntime();
String OS = System.getProperty("os.name").toLowerCase();
if (OS.indexOf("windows 9") > -1) {
p = r.exec("command.com /c set");
} else if ((OS.indexOf("nt") > -1) || (OS.indexOf("windows 2000") > -1)
|| (OS.indexOf("windows xp") > -1)) {
p = r.exec("cmd.exe /c set");
} else {
// it is Unix os.
p = r.exec("env");
}
BufferedReader br = new BufferedReader(new InputStreamReader(p
.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
int idx = line.indexOf('=');
String key = line.substring(0, idx);
String value = line.substring(idx + 1);
envVars.setProperty(key, value);
}
return envVars;
}
}

Best regards

shong

Thanks - I ended up using getEnv("CCR_HOME") in java, and that worked fine.
Six Stars

Re: [resolved] How to get an environment variable from the system into a context var?

You can simply use System.getenv("myvar") in java, without an external code.
bye
One Star

Re: [resolved] How to get an environment variable from the system into a context var?

right - that worked great.
Thanks,
Dave
One Star

Re: [resolved] How to get an environment variable from the system into a context var?

Shong,
Nice solution. Thanks!
Six Stars

Re: [resolved] How to get an environment variable from the system into a context var?

There is a custom component for this in Talend Exchange - tGetEnvVarInput