Issue
I want to open a HTML document when clicking a jButton. this is the button's code
try {
Desktop dk = Desktop.getDesktop();
URI url = new URI("src/info/index.html");
dk.browse(url.resolve(url));
} catch (URISyntaxException | IOException ex) {
System.out.println("Error en btnAyudaActionPerformed:\n" + ex.getMessage());
JOptionPane.showMessageDialog(this, "No se puede abrir la ayuda");
}
But it throws
Failed to open src/info/src/info/index.html. Error message: The system cannot find the specified file.
When I put the absolute path it works correctly, but I cannot put the absolute path because I need to move the jar.
Solution
The URI need an absolute path, so I get the file's absolute path:
File htmlAyuda = new File("info/index.html");
String direccion = htmlAyuda.getAbsolutePath();
try {
Desktop dk = Desktop.getDesktop();
URI url = new URI(direccion.replace("\\", "/"));
dk.browse(url.resolve(url));
} catch (URISyntaxException | IOException ex) {
System.out.println("Error en btnAyudaActionPerformed:\n" + ex.getMessage());
JOptionPane.showMessageDialog(this, "No se puede abrir la ayuda");
}
The URI cannot have backslashes, so I have to replace them for normal slashes
Answered By - evening_g
Answer Checked By - Willingham (JavaFixing Volunteer)