Im working on a javafx app and a requirement is that on clicking a hyperlink, it opens the given url in the browser. I have this working with the following
url = "files/file.html";
getHostServices().showDocument(url);
However, when the url includes an id to auto focus a specific portion of the page, the browser never launches. There's no errors, and nothing seems to be logged indicating anything went wrong, the browser just never opens. Having trouble finding anyone else who's run into this problem or any potential solutions.
tldr; How do I get the below to actually work and open in a browser?
String url = "files/file.html#section";
getHostServices().showDocument(url);
Also, this is only an issue with local files it seems. Opening https sites this way works fine
UPDATE: So thanks to Sai's great post, it sounds like the official conclusion is that it's a deeper issue than Java, and simply blocked for security reasons relating to data injection. Won't pretend to understand, but I found a solution based on this link (again shared by Sai below) and more specifically this link shared in that answer, and wanted to include it for any future confused people. I also updated the title to be more specific to the problem.
So basically, the only true way around it that gets all the functionality is to create a temporary file that then gets opened, automatically redirects to the proper file with the fragment, and then delete it. Basic code is as follows:
public static void openWebPage(String url) {
String tmpPath = "path/to/.tmp/temp.html";
File tempFile = File(tmpPath);
if (!tempFile.exists()) {
tempFile.mkdirs();
} else {
tempFile.delete();
}
tempFile.createNewFile();
FileWriter tempFileWriter = FileWriter(tmpPath);
tempFileWriter.write("<html><meta http-equiv=Refresh content=\"0; url=$url\"><body></body></html>");
tempFileWriter.close();
Main.getStaticHostServices().showDocument(tmpPath);
}