1 package com.ca.commons.cbutil; 2 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 6 12 13 public class CBLauncher 14 { 15 16 17 24 25 public static void launchProgram(String extension, String fileName) 26 { 27 String command = null; 28 StringBuffer fileType = new StringBuffer (20); 29 StringBuffer program = new StringBuffer (50); 30 Process p; 31 InputStream stdOut = null; 32 boolean collecting; 33 int b; 34 35 if (command == null) 36 { 37 47 48 try 49 { 50 p = Runtime.getRuntime().exec("cmd /c assoc " + extension); 51 stdOut = p.getInputStream(); 52 collecting = false; 53 54 while ((b = stdOut.read()) != -1) 55 { 56 char c = (char) b; 57 if (c == '\r' || c == '\n') 58 { 59 } 61 else if (collecting) 62 { 63 fileType.append(c); 64 } 65 else if (c == '=') 66 { 67 collecting = true; 68 } 69 } 70 } 71 catch (IOException e) 72 { 73 CBUtility.error(CBIntText.get("Error trying to associate file extension: {0} with program", new String []{extension}) + "\n" + e); 74 } 75 76 86 87 try 88 { 89 p = Runtime.getRuntime().exec("cmd /c ftype " + fileType.toString()); 90 stdOut = p.getInputStream(); 91 collecting = false; 92 93 while ((b = stdOut.read()) != -1) 94 { 95 char c = (char) b; 96 97 if (c == '\r' || c == '\n') 98 { 99 } 101 else if (collecting) 102 { 103 program.append(c); 104 } 105 else if (c == '=') 106 { 107 collecting = true; 108 } 109 } 110 } 111 catch (IOException e) 112 { 113 CBUtility.error(CBIntText.get("Error trying to associate file extension: {0} with program", new String []{extension}) + "\n" + e); 114 } 115 command = program.toString(); 116 } 117 118 String fullProgramName = program.toString(); 119 String runProgramName; 120 121 if (program.toString().endsWith("%1")) runProgramName = fullProgramName.substring(0, fullProgramName.lastIndexOf("%1") - 1); 123 else if (program.toString().endsWith("\"%L\"")) runProgramName = fullProgramName.substring(0, fullProgramName.lastIndexOf("\"%L\"") - 1); 125 else 126 runProgramName = program.toString(); 127 128 Runtime r = Runtime.getRuntime(); 129 130 try 131 { 132 r.exec(runProgramName + " \"" + fileName + "\""); 133 } 134 catch (IOException e) 135 { 136 CBUtility.error(CBIntText.get("Error occured when trying to launch program: {0} with the file {1}. Either the program found does not support the file type, or the file name was incorrect.", 137 new String []{runProgramName, fileName}) + "\n\n\n" + e); 138 } 139 } 140 } | Popular Tags |