Recupérer des variables d'environnement

lara0o Messages postés 15 Date d'inscription mercredi 31 août 2005 Statut Membre Dernière intervention 15 décembre 2005 - 14 nov. 2005 à 16:43
lara0o Messages postés 15 Date d'inscription mercredi 31 août 2005 Statut Membre Dernière intervention 15 décembre 2005 - 15 nov. 2005 à 11:29
Salut à toute la bande,

Comment peut on recuperer des variables d'environnement en java?
J'ai trouve la fonction System.getProperty mais elle ne concerne que les vraibale d'environnement de la JVM. Moi je veux récupérer les variables d'environnement de mon OS. Comment je peux faire?

0o.
A voir également:

2 réponses

pla78 Messages postés 1 Date d'inscription vendredi 30 mai 2003 Statut Membre Dernière intervention 15 novembre 2005
15 nov. 2005 à 11:11
Salut,

J'ai eu le même problème récemment, il n'y a rien en standard!!!

J'ai trouvé du code sur Internet et je l'ai intègrè en modifiant quelques trucs, et ç a marche sur Windows, UNIX, Linux et OS/400...

Attention je l'ai mise dans mon package util, à chager éventuellement...

Pascal,

/**
* Copyright 2002 ROLANDS & ASSOCIATES Corporation.
* @author David J. Ward
*/
package util;



import java.io.InputStreamReader;


/**
* Classe permettant la récupération des variables d'environement placées au
* niveau du sytème d'exploitation.
* @author Pascal
* @version 7 oct. 2005
*/
public class OSEnvironment
{


private static java.util.Properties env = null;


/**
* Private Constructor. OSEnvironment is not instantiable.
*/
private OSEnvironment()
{}


/**
* @return a Properties object containing the environment variables and their
* associated values.
* @throws Throwable if an execption occurs.
*/
private static java.util.Properties getEnvironment()
{


Process p = null;
java.util.Properties envVars = new java.util.Properties();
Runtime r = Runtime.getRuntime();
String OS = System.getProperty("os.name").toLowerCase();
boolean isEbcdic = false;
try
{
// Get the Windows 95 environment variables
if (OS.indexOf("windows 9") > -1)
{
p = r.exec("command.com /c set");
}
// Get the Windows NT environment variables
else if (OS.indexOf("nt") > -1)
{
p = r.exec("cmd.exe /c set");
}
// Get the Windows 2000 environment variables
else if (OS.indexOf("2000") > -1)
{
p = r.exec("cmd.exe /c set");
}
// Get the Windows XP environment variables
else if (OS.indexOf("xp") > -1)
{
p = r.exec("cmd.exe /c set");
}
// Get the unix environment variables
else if (OS.indexOf("linux") > -1)
{
p = r.exec("env");
}
// Pour l'AS/400 iSeries
else if (OS.indexOf("os/400") > -1)
{
isEbcdic = true;
p = r.exec("/usr/bin/env");
}
// Get the unix environment variables
else if (OS.indexOf("unix") > -1)
{
p = r.exec("/bin/env");
}
// Get the unix environment variables
else if (OS.indexOf("sunos") > -1)
{
p = r.exec("/bin/env");
}
else
{
System.out.println("OS not known: " + OS);
}
java.io.BufferedReader br;
if (isEbcdic)
{
br = new java.io.BufferedReader(new InputStreamReader(p
.getInputStream(), "Cp037"));
}
else
{
br = new java.io.BufferedReader(new InputStreamReader(p
.getInputStream()));
}
String line;
int idx;
String key, value;
while ((line = br.readLine()) != null)
{
//System.out.println("Line :" + line);
idx = line.indexOf('=');
// if there is no equals sign on the line skip to the net line
// this occurs when there are newline characters in the environment
// variable
if (idx < 0)
continue;
key = line.substring(0, idx);
value = line.substring(idx + 1);
envVars.setProperty(key, value);
}
}
catch (Exception e)
{
e.printStackTrace();
}


return envVars;
}


/**
* Return a Properties object containing the environment variables.
* @return a Properties object containing the environment variables and their
* associated values.
*/
public static java.util.Properties get()
{
if (env == null)
env = getEnvironment();
return (java.util.Properties)env.clone();
}


/**
* Searches for the property with the specified key in the environment. The
* method returns
null
if the property is not found.
* @param key the property key.
* @return the value in environment with the specified key value.
*/
public static String getProperty(String key)
{
if (env == null)
env = getEnvironment();
return env.getProperty(key);
}


/**
* Searches for the property with the specified key in environment. The method
* returns the default value argument if the property is not found.
* @param key the hashtable key.
* @param defaultValue a default value.
* @return the value in this property list with the specified key value.
*/
public static String getProperty(String key, String defaultValue)
{
if (env == null)
env = getEnvironment();
String val = env.getProperty(key);
return (val == null) ? defaultValue : val;
}


/**
* Main method to test the OSEnvironment class.
* @param args command line arguments
*/
public static void main(String args[])
{
try
{
java.util.Properties p = OSEnvironment.get();
//System.out.println("Taille " + p.size());
p.list(System.out);
System.out.println("the current value of TEMP is : "
+ p.getProperty("TEMP"));
}
catch (Throwable e)
{
e.printStackTrace();
}
}


}
0
lara0o Messages postés 15 Date d'inscription mercredi 31 août 2005 Statut Membre Dernière intervention 15 décembre 2005
15 nov. 2005 à 11:29
Merci beaucoup pour ta reponse complete! Je teste ca cet aprem'

0o.
0
Rejoignez-nous