2014-01-06 26 views

回答

4

有尋找操作系統... Java中的內置API。

System.getProperty("os.name");

然後,您可以編寫一個caseif else。像...

private String os = System.getProperty("os.name").toLowerCase(); 

if (os.contains("windows"){ 
    p = Runtime.getRuntime().exec("your batch file"); 
    p.waitFor(); 
} else if (os.contains("mac"){ 
    p = Runtime.getRuntime().exec("your sh file or dmg"); 
    p.waitFor(); 
} else { 
    p = Runtime.getRuntime().exec("your .sh file"); 
    p.waitFor(); 
} 
0
/** 
* @Reff http://www.tutorialspoint.com/java/lang/system_getproperties.htm 
*/ 

import java.lang.String; 

public class Main { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     String os = System.getProperty("os.name").toString(); 
     System.out.println("OS: " + os); 

     if("Windows 8".equals(os)) { 
     System.out.println("Windows 8"); 
     } else { 
     System.out.println("Others"); 
     } 
    } 
}