Angsuman's Java Blog

News and views from my perspective as a Chief Software Architect and CEO. Focused on Java and Web Technologies.


Monday, July 28, 2003

Code snippet to download and execute NAV Update. Run this as as daily task from MS Task and you will never miss an update.

navaut.exe executes the downloaded executable without user intervention. Email me if you need the executable, or you can hack a some cscript if you want. Running the executable directly from Java doesn't work for two reasons, one being user interaction is required to complete the task, making it unsuitable as an unattended task.


import com.meterware.httpunit.*;
import java.io.*;

public class NAVUpdate {
public static final String URL = "http://securityresponse.symantec.com/avcenter/download/pages/US-N95.html";
public static final String FILE = "navupdate.exe";
public static final String INSTALL = "navaut.exe";
public static void main(String args[]) throws Exception {
update();
}

public static void update() throws Exception {
InputStream is = (new WebConversation()).getResponse(new GetMethodWebRequest(URL)).getTableStartingWith("Filename").getTableCell(1,0).getLinks()[0].click().getInputStream();
File file = new File(FILE);
if(file.exists()) file.delete(); // Delete old copy, if present
BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(file));
drain(is, os);
execSync(INSTALL);
file.delete();
}

public static void drain(InputStream is, OutputStream os) throws IOException {
for(int i;(i = is.read()) != -1;) os.write((char) i);
os.flush();
os.close();
}

public static int execSync(String file) throws Exception {
Process process = Runtime.getRuntime().exec(file);
process.waitFor();
return process.exitValue();
}
}