2017-07-07 146 views
0

我試圖以這種方式來打開默認的程序.jpg文件:沒有應用程序與此操作的指定文件相關聯。錯誤

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.IOException; 

public class Test { 
public static void main(String[] args) throws IOException { 
    File image = new File(System.getProperty("user.home") +   
"/Desktop/project/XL-116/DATAFILE17.jpg"); 
    try { 
     java.awt.Desktop.getDesktop().open(image); 
    } catch (FileNotFoundException ex) { 
     // ..... 
    } 
} 
} 

而且我得到這個錯誤:

Exception in thread "main" java.io.IOException: Failed to open C:\Users\khuda 
Dad\Desktop\project\XL-116\DATAFILE17.jpg. Error message: No application is 
associated with the specified file for this operation. 

at sun.awt.windows.WDesktopPeer.ShellExecute(Unknown Source) 
at sun.awt.windows.WDesktopPeer.open(Unknown Source) 
at java.awt.Desktop.open(Unknown Source) 
at Test.main(Test.java:9) 

Screenshot of program association

而且我已經檢查了默認程序。沒有錯。那麼問題是什麼?

回答

3

顯然,文件有關聯應用程序運行的事實並不意味着可以應用「打開」操作。

No application is associated with the specified file for this operation.

(重點煤礦)

行動 「開放」 是不適用的。如果你右擊DATAFILE17.jpg會發生什麼?它對我來說看起來是編輯。這可能是發佈的實際行動。

當然,它可以從計算機到計算機,但我的猜測是動作是「編輯」。試試這個:

Desktop.getDesktop().edit(image); 

方法isSupported(Desktop.Action action)返回是否支持動作。

+0

我同意:「IOException - 如果指定的文件沒有關聯的應用程序或關聯的應用程序無法啓動」 – dbalakirev

相關問題