code_junkie

Cross-platform way to open a file using Java 1.5

Nov 12th, 2011
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. Runtime.getRuntime().exec(new String[]{ "rundll32",
  2. "url.dll,FileProtocolHandler", fileName });
  3.  
  4. Desktop desktop = Desktop.getDesktop();
  5.  
  6. desktop.open( aFile );
  7. desktop.imaginaryAction( aFile );
  8.  
  9. package your.pack.name;
  10.  
  11. import java.io.File;
  12.  
  13. public class Desktop{
  14.  
  15. // hide the constructor.
  16. Desktop(){}
  17.  
  18. // Created the appropriate instance
  19. public static Desktop getDesktop(){
  20.  
  21. String os = System.getProperty("os.name").toLowerCase();
  22.  
  23. Desktop desktop = new Desktop();
  24. // This uf/elseif/else code is used only once: here
  25. if ( os.indexOf("windows") != -1 || os.indexOf("nt") != -1){
  26.  
  27. desktop = new WindowsDesktop();
  28.  
  29. } else if ( os.equals("windows 95") || os.equals("windows 98") ){
  30.  
  31. desktop = new Windows9xDesktop();
  32.  
  33. } else if ( os.indexOf("mac") != -1 ) {
  34.  
  35. desktop = new OSXDesktop();
  36.  
  37. } else if ( os.indexOf("linux") != -1 && isGnome() ) {
  38.  
  39. desktop = new GnomeDesktop();
  40.  
  41. } else if ( os.indexOf("linux") != -1 && isKde() ) {
  42.  
  43. desktop = new KdeDesktop();
  44.  
  45. } else {
  46. throw new UnsupportedOperationException(String.format("The platform %s is not supported ",os) );
  47. }
  48. return desktop;
  49. }
  50.  
  51. // default implementation :(
  52. public void open( File file ){
  53. throw new UnsupportedOperationException();
  54. }
  55.  
  56. // default implementation :(
  57. public void imaginaryAction( File file ){
  58. throw new UnsupportedOperationException();
  59. }
  60. }
  61.  
  62. // One subclass per platform below:
  63. // Each one knows how to handle its own platform
  64.  
  65.  
  66. class GnomeDesktop extends Desktop{
  67.  
  68. public void open( File file ){
  69. // Runtime.getRuntime().exec: execute gnome-open <file>
  70. }
  71.  
  72. public void imaginaryAction( File file ){
  73. // Runtime.getRuntime().exec:gnome-something-else <file>
  74. }
  75.  
  76. }
  77. class KdeDesktop extends Desktop{
  78.  
  79. public void open( File file ){
  80. // Runtime.getRuntime().exec: kfmclient exec <file>
  81. }
  82.  
  83. public void imaginaryAction( File file ){
  84. // Runtime.getRuntime().exec: kfm-imaginary.sh <file>
  85. }
  86. }
  87. class OSXDesktop extends Desktop{
  88.  
  89. public void open( File file ){
  90. // Runtime.getRuntime().exec: open <file>
  91. }
  92.  
  93. public void imaginaryAction( File file ){
  94. // Runtime.getRuntime().exec: wow!! <file>
  95. }
  96. }
  97. class WindowsDesktop extends Desktop{
  98.  
  99. public void open( File file ){
  100. // Runtime.getRuntime().exec: cmd /c start <file>
  101. }
  102.  
  103. public void imaginaryAction( File file ){
  104. // Runtime.getRuntime().exec: ipconfig /relese /c/d/e
  105. }
  106. }
  107. class Windows9xDesktop extends Desktop{
  108.  
  109. public void open( File file ){
  110. //Runtime.getRuntime().exec: command.com /C start <file>
  111. }
  112.  
  113. public void imaginaryAction( File file){
  114. //Runtime.getRuntime().exec: command.com /C otherCommandHere <file>
  115. }
  116. }
  117.  
  118. public static boolean isWindows() {
  119. String os = System.getProperty("os.name").toLowerCase();
  120. return os.indexOf("windows") != -1 || os.indexOf("nt") != -1;
  121. }
  122. public static boolean isMac() {
  123. String os = System.getProperty("os.name").toLowerCase();
  124. return os.indexOf("mac") != -1;
  125. }
  126. public static boolean isLinux() {
  127. String os = System.getProperty("os.name").toLowerCase();
  128. return os.indexOf("linux") != -1;
  129. }
  130. public static boolean isWindows9X() {
  131. String os = System.getProperty("os.name").toLowerCase();
  132. return os.equals("windows 95") || os.equals("windows 98");
  133. }
  134.  
  135. if (isLinux())
  136. {
  137. cmds.add(String.format("gnome-open %s", fileName));
  138. String subCmd = (exec) ? "exec" : "openURL";
  139. cmds.add(String.format("kfmclient "+subCmd+" %s", fileName));
  140. }
  141. else if (isMac())
  142. {
  143. cmds.add(String.format("open %s", fileName));
  144. }
  145. else if (isWindows() && isWindows9X())
  146. {
  147. cmds.add(String.format("command.com /C start %s", fileName));
  148. }
  149. else if (isWindows())
  150. {
  151. cmds.add(String.format("cmd /c start %s", fileName));
  152. }
  153.  
  154. final Program p = Program.findProgram(fileExtension);
  155. p.execute(file.getAbsolutePath());
  156.  
  157. Program.launch("http://google.com/");
  158.  
  159. Display.syncExec(new Runnable() {
  160. public void run() {
  161. Program.launch("http://google.com/");
  162. }
  163. });
Add Comment
Please, Sign In to add comment