How to open and view a file (similar to double-clicking a file) using Java

I would like to know how to find out Java code that will help to perform the same operation as double-clicking on any OS in a file, opening it and thereby allowing us to view its contents when the user supplies the file location on his PC. Any suggestion would be very helpful since I needed to finish my application.

+4
source share
3 answers
+5
source

I used the following code. In windows, you will open the Windows file open dialog box if no program is associated with the file type. If it does not start in windows, it returns to Desktop.open() , which works if the file type is known to the system.

 public static boolean openFile(File file){ try { if (System.getProperty("os.name").contains("Windows")) { Runtime.getRuntime().exec( "rundll32 SHELL32.DLL,ShellExec_RunDLL " + file.getAbsolutePath()); } else if (Desktop.isDesktopSupported()) { Desktop.getDesktop().open(file); } else { return false; } return true; } catch (final Exception e1) { System.err.println("Error opening file " + file, e1); return false; } } 
+2
source

in windows xp:

 rundll32 shell32.dll,ShellExec_RunDLL "file:///d:\download\theapp.lnk" 

you can add a registry so that you can run the lnk file in the RUN dialog and the folder cruiser:

 Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\lnkfile\shell] [HKEY_CLASSES_ROOT\lnkfile\shell\edit] [HKEY_CLASSES_ROOT\lnkfile\shell\edit\command] @="rundll32 shell32.dll,ShellExec_RunDLL \"file:///%1\"" 
0
source

All Articles