View Full Version : [JAVA] Runtime.getRuntime().exec(comandi) e percorsi remoti
franksisca
05-01-2012, 16:19
sto eseguendo dei file dal mio programma seguendo questa sintassi:
String[] comandi = {"cmd", "/C", file.getAbsolutePath()};
Runtime.getRuntime().exec(comandi);
ma se il file non è su macchina locale, non funziona. come posso ovviare a questo probema?
franksisca
05-01-2012, 16:36
stessa cosa se metto
Desktop.getDesktop().open(file);
i file in locale funzionano,quelli in remoto no!!!!
franksisca
05-01-2012, 17:32
credo di aver risolto, spulciando in rete ho trovato questi frammenti di codice, che al mmomento sembrano funzionare
// generate uri according to the filePath
private URI getFileURI(String filePath) {
URI uri = null;
filePath = filePath.trim();
if (filePath.indexOf("http") == 0 || filePath.indexOf("\\") == 0) {
if (filePath.indexOf("\\") == 0) {
filePath = "file:" + filePath;
filePath = filePath.replaceAll("#", "%23");
}
try {
filePath = filePath.replaceAll(" ", "%20");
URL url = new URL(filePath);
uri = url.toURI();
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (URISyntaxException ex) {
ex.printStackTrace();
}
} else {
File file = new File(filePath);
uri = file.toURI();
}
return uri;
}
public void launchFile(File file) {
if (!Desktop.isDesktopSupported()) {
return;
}
Desktop dt = Desktop.getDesktop();
try {
dt.open(file);
} catch (IOException ex) {
// this is sometimes necessary with files on other servers ie
// \xxxxxx.xls
launchFile(file.getPath());
}
}
// this can launch both local and remote files
public void launchFile(String filePath) {
if (filePath == null || filePath.trim().length() == 0) {
return;
}
if (!Desktop.isDesktopSupported()) {
return;
}
Desktop dt = Desktop.getDesktop();
try {
dt.browse(getFileURI(filePath));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.