根据操作系统分别运行.bat文件和.sh文件

  • 本文关键字:文件 bat sh 运行 操作系统 java
  • 更新时间 :
  • 英文 :


我希望能够在mac操作系统上运行.sh,并使用相同的java应用程序从Windows系统运行.bat文件。有没有办法做到这一点?提前谢谢。

有一个内置的 API 用于查找 OS ... 在 Java 中。

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();
}
/**
 * @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");
      }
    }
}

最新更新