Java Reference
In-Depth Information
L ISTING 9.5
Continued
private static void extractNativeResource(String resourceName) {
InputStream is = loader.getResourceAsStream(resourceName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(tempFileName);
} catch (IOException ioe) {
System.out.println(“creating temp file: “+ioe);
}
int c;
try {
while ((c = is.read()) != -1)
fos.write(c);
is.close();
fos.close();
} catch (IOException ioe) {
System.out.println(“extracting installer: “+ioe);
}
}
private static void executeNativeResource(String fileName){
try {
Runtime.getRuntime().exec(fileName);
} catch (IOException ioe) {
System.out.println(“executing installer: “+ioe);
}
}
private static void deleteTempFile(String fileName){
try {
File f = new File(fileName);
f.delete();
System.out.println(“temp file deleted.”);
} catch (Exception ioe) {
System.out.println(“deleting temp file: “+ioe);
}
}
}
After reading the input parameters, the main() method (lines 15-30 of Listing 9.5) extracts
the native file to a temporary location, executes it, and then cleans up, leaving the temp.bat
file or not, as specified from the input parameter.
Search WWH ::




Custom Search