![]() |
JAVA: Plattform unabhängig programmieren
ich möchte mit java ein plattform unabhängiges programm schreiben.
Aber wie bekomme ich die Pfade für: - Systemroot (z.b.: C:\ oder /) - Temp-Verzeichnis (z.B.: C:\windows\temp) gibts keine klasse, aus der ich mir diese pfade holen kann (betriebssystem unabhängig)? |
Re: JAVA: Plattform unabhängig programmieren
Dafür gibts ja eigentlich die Umgebungsvariablen!!!
Ich weis nicht wie man diese in Java benutzt und ich weis auch nicht, ob es z.B. unter Linux überhaupt so etwas wie Umgebungsvariablen gibt. |
Re: JAVA: Plattform unabhängig programmieren
mti dieser Java-klasse sollte es gehen!
-take care :-) thomas
Code:
[edit=Luckie]Code-Tags nach C geändert. Mfg, Luckie[/edit]
public class ReadEnv {
public static Properties getEnvVars() throws Throwable { Process p = null; Properties envVars = new Properties(); Runtime r = Runtime.getRuntime(); String OS = System.getProperty("os.name").toLowerCase(); // System.out.println(OS); 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) ) { // thanks to JuanFran for the xp fix! p = r.exec( "cmd.exe /c set" ); } else { // our last hope, we assume Unix (thanks to H. Ware for the fix) 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 ); // System.out.println( key + " = " + value ); } return envVars; } public static void main(String args[]) { try { Properties p = ReadEnv.getEnvVars(); System.out.println("the current value of TEMP is : " + p.getProperty("TEMP")); } catch (Throwable e) { e.printStackTrace(); } } } |
Re: JAVA: Plattform unabhängig programmieren
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 10:20 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz