linux - How to open file with unsupported file extension using Java Code via the Desktop? -


i have program reads in file path or uri , launches ever file or uri via desktop. problem i'm running in unsupported file extensions. odd thing error when fails open file isnt expect. error reads

java.io.ioexception: failed open file:/c:/users/angel/desktop/test.test. error message: access denied.  @ sun.awt.windows.wdesktoppeer.shellexecute(wdesktoppeer.java:77) @ sun.awt.windows.wdesktoppeer.open(wdesktoppeer.java:54) @ java.awt.desktop.open(desktop.java:272) @ desktopopenfile.desktopopenfile.openfile(desktopopenfile.java:24) @ desktopopenfile.desktopopenfile.main(desktopopenfile.java:15) 

i think error more along lines of file extension unsupported. can verify file extension in other program feeds database supports wondering if there way allow user pick application open unsupported file with.

my ideal solution work similar when attempt open unsupported file in windows ask program you'd use open or search web. have been if had gotten option using code below. idea want able open file type via uri or file path. code works .pdf, .xml, .pptx, , word files. suggestions awesome.

 /**  * checks if file or uri included in message , if opens via  * desktop  */ private void checkmessage() {     // check make sure data model , time model set     if (datamodel == null || timemodel == null) {         return;     }      int selectedindex = jtable.convertrowindextomodel(jtable.getselectedrow());     if (selectedindex != -1) {         intdata intdata = datamodel.getdata(selectedindex);         icedata icedata = ((icedata) intdata);         if (icedata != null) {             if (icedata.getmessage() != null) {                 string message = icedata.getmessage();                 if (message != null) {                     if (message.contains("file://")) {                         string path = message.substring(message.lastindexof("/") + 1, message.indexof(" ", message.lastindexof("/")));                         openfile(path);                     }                     else if (message.contains("uri://")) {                         try {                             uri uri = new uri(message.substring(message.indexof("/") + 2, message.indexof(" ", message.lastindexof("/"))));                             openuri(uri);                         }                         catch (ioexception | urisyntaxexception ex) {                             logger.getlogger(icedatalistpanel.class.getname()).log(level.severe, "failed open uri: {0}", ex.getmessage());                         }                     }                 }             }         }     } }  /**  * opens file given path  *  * @param path file  */ private void openfile(string path) {     try {         file file = new file(path);          if (file.exists()) {             if (desktop.isdesktopsupported()) {                 desktop.getdesktop().open(file);             }             else {                 system.out.println("awt desktop not supported");             }         }         else {             system.out.println("file not exist");         }     }     catch (ioexception ex) {         logger.getlogger(icedatalistpanel.class.getname()).log(level.severe, "failed open file: {0}", ex.getmessage());     } }  /**  * opens uri given uri  *  * @param path file  */ private void openuri(uri uri) throws ioexception {     if (desktop.isdesktopsupported()) {         desktop.getdesktop().browse(uri);     }     else {         system.out.println("awt desktop not supported");     } } 

i standard os dialog asking application (with java 7 , windows 8). if access denied, because user running java program has no right open file.

enter image description here

test app:

public class desktopopenfile {     public static void main(string[] args) {         openfile("huhu.orttr");     }      private static void openfile(string path) {         try {             file file = new file(path);              if (file.exists()) {                 if (desktop.isdesktopsupported()) {                     desktop.getdesktop().open(file);                 }                 else {                     system.out.println("awt desktop not supported");                 }             }             else {                 system.out.println("desktopopenfile::openfile: file = " + file.getabsolutepath() + (file.exists() ? " - exists" : " - not exist!"));             }         }         catch (ioexception ex) {             ex.printstacktrace();         }     } } 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -