cloudnaut
cloudnaut

Reputation: 982

Reading Windows Environment Variable (Encoding/Charset Issue)

what I wanna do is read the Windows environment variable %APPDATA%. I have some severe problems with Umlauts though e.g. if APDATA is set to "ÄÄÄ" I ll get sth like "'''".

Here is my code:

final Process p = Runtime.getRuntime().exec("cmd.exe /C echo %APPDATA%");
final BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String buffer;
if((buffer = br.readLine()) != null) {
    return buffer;
}

If I print out

String encoding_system = System.getProperty("file.encoding");

I'm getting Cp1252. I tried to tell the InputStreamReader the system encoding, but nothing changes.

Upvotes: 0

Views: 1854

Answers (1)

G_H
G_H

Reputation: 12009

That's a brittle way of trying to get an environment variable's value. You're better off using System.getEnv(). This will return a Map<String, String> instance linking environment variable names to their values.

Upvotes: 1

Related Questions