Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   JAVA: Plattform unabhängig programmieren (https://www.delphipraxis.net/86447-java-plattform-unabhaengig-programmieren.html)

geisi 14. Feb 2007 11:32


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)?

TonyR 14. Feb 2007 12:08

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.

merlin17 14. Feb 2007 12:25

Re: JAVA: Plattform unabhängig programmieren
 
mti dieser Java-klasse sollte es gehen!

-take care

:-) thomas


Code:
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();
     }
   }
}
[edit=Luckie]Code-Tags nach C geändert. Mfg, Luckie[/edit]

Sergej 14. Feb 2007 13:21

Re: JAVA: Plattform unabhängig programmieren
 
-> Java ist auch eine Insel


Alle Zeitangaben in WEZ +1. Es ist jetzt 06:54 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz