2012-08-13 97 views
3

我正在嘗試使用終端命令在osx上取消隱藏〜\ Library \文件夾的簡單java程序。據我研究了從java運行系統命令的代碼是 Runtime.getRuntime()。exec(); ,並且在我查看的每個地方都列出。從eclipse使用java運行Mac OSX命令

但是,我的程序不起作用。主要方法如下。

public static void main(String[] args) throws IOException { 

    String[] noHide = {"chflags"," " ,"nohidden"," ", "~/Library/"}; 
    try { 
     Runtime.getRuntime().exec(noHide); 
     System.out.println("library unhidden"); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

該程序不會引發異常,編譯並執行正常,但Library文件夾根本不會取消隱藏。無論我重新格式化cmd字符串。的格式沒有下文的工作

String noHide = "chflags nohidden ~/Library"; 
String[] noHide = {"chflags", "nohidden","~/Library"}; 
String[] noHide = {"chflags"," " ,"nohidden"," ", "~/Library/"}; 

如果刪除它們拋出異常(當然,不是字符串數組對象)的空間。我可以在osx終端上運行命令(chflags noHidden〜/ Library)。任何人有一個想法爲什麼?

回答

2

您需要使用您擁有的trycatch。但是,你main應該是這樣的:

public static void main(String[] args) { 
    String[] noHide = {"chflags", "nohidden","~/Library"}; 
    try { 
     Runtime.getRuntime().exec(noHide); 
    } 
    catch (Exception e) { 
     } 
    } 

基本上,你不需要throws IOException。這對我很有用,所以如果它仍然不能在你的程序中運行,那麼你設置的方式可能會有更大的問題。

+0

不起作用。同樣的結果。 – pipsqueaker117 2012-08-13 17:55:01

+0

@ pipsqueaker117對不起,更正了我的答案,參見上文。 – Nathan 2012-08-13 17:55:38