Obtener sistema operativo con Java

Una de las grandes ventajas de usar java es sin duda el soporte para multiplataforma, el poder hacer un sólo programa que pueda ser ejecutado tanto en Windows, Mac, Unix… cualquier SS.OO que disponga de una máquina virtual Java.

Navegando por la red me he encontrado este código tan interesante, se trata de una manera eficaz de conocer sobre que sistema operativo se está ejecutando nuestro programa y obtener las rutas absulutas correctas para el buen funcionamiento de nuestro programa.

Miguel Álvarez Úbeda ha enviado este código a la lista de desarrolladores de gvSIG:

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

public class ApplicationLauncher {

public static void main(String args[]){
if( args.length!=1) {
System.out.println("Numero de parametros erroneo forma de uso: java -cp . ApplicationLauncher fichero|URL");
return;
}
if(args[0].indexOf("http")!=-1) launchURL(args[0]);
else launchDefaultViewer(args[0]);
}

private static String linuxDesktop = null;

private static String getEnv(String envvar){
try{
Process p = Runtime.getRuntime().exec("/bin/sh echo $"+envvar);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String value = br.readLine();
if(value==null) return “";
else return value.trim ();
}
catch(Exception error){
return “";
}
}

private static String getLinuxDesktop(){
//solo se averigua el entorno de escritorio una vez, despues se almacena en la variable estatica
if(linuxDesktop!=null) return linuxDesktop;
if(!getEnv("KDE_FULL_SESSION").equals("") || !getEnv("KDE_MULTIHEAD").equals("")){
linuxDesktop="kde";
}
else if(!getEnv("GNOME_DESKTOP_SESSION_ID").equals("") || !getEnv("GNOME_KEYRING_SOCKET").equals("")){
linuxDesktop="gnome";
}
else linuxDesktop="";

return linuxDesktop;
}

public static Process launchURL(String url){
try{
if (System.getProperty("os.name “).toUpperCase().indexOf("95?) != -1)
return Runtime.getRuntime().exec( new String[]{"command.com“, “/C", “start", url} );
if (System.getProperty("os.name“).toUpperCase().indexOf("WINDOWS") != -1)
return Runtime.getRuntime().exec( new String[]{"cmd.exe", “/C", “start", url} );
if (System.getProperty("os.name“).toUpperCase().indexOf("MAC") != -1)
return Runtime.getRuntime().exec( new String[]{"open", url} );
if (System.getProperty("os.name“).toUpperCase().indexOf("LINUX") != -1 ) {
if(getLinuxDesktop().equals("kde"))
return  Runtime.getRuntime().exec( new String[]{"kfmclient", “exec", url} );
else
return Runtime.getRuntime().exec( new String[]{"gnome-open", url} );
}
}
catch(IOException ioex){System.out.println(ioex);}

return null;
}

public static Process launchDefaultViewer(String filepath){
return launchURL( new File(filepath).getAbsolutePath());
}
}

Espero que sirva de ayuda ;)


Comparte este artículo

rss delicious meneame technorati webeame bitacoras enchilame fresqui google reddit

Otros temas interesantes


 

Trackbacks / Pingbacks


4 Comentarios

Deja un comentario


Autor    NOnameGIRL dice:

Entiendo más el Chino que ésto… :S

Escrito el 27/02/08 a las 00:15:16 (#1) ( responder)

Autor    Emilio dice:

jajajaja lógico.. :) cada uno buenamente con lo suyo ¿verdad?

Escrito el 27/02/08 a las 08:30:47 (#2) ( responder)

Autor    manix dice:

hola, el codigo esta mal escrito, incluso te pinta mal el codigo por lo mismo

saludos

Escrito el 17/10/08 a las 20:44:39 (#3) ( responder)

Autor    Emilio dice:

¿Qué es lo que falla manix? he cambiado el css y los plugins por lo que ya no lo colorea.

Escrito el 17/10/08 a las 21:36:46 (#4) ( responder)

Deja un comentario

Normas de uso
  • No insultes
  • Escribe sin faltas de ortografía
  • Si vas a contestar a alquien, utiliza el botón responder, se le avisará