**UPDATE---The issue was not the file writer not closing but incorrect termination of the Java application. I have updated the question.
I have the following classes launching a JAVAFX web view and exposing some java objects to the web view's html.
public class FileSystemBridge {
private void writeToFile(String[] fileContents){
if (content!=null){
String fileName ="pathToFile";
BufferedWriter fileWriter;
for (int i =0; i<fileContents.length(); i++ ){
String fileContent fileContents[i]);
try {
fileName = fileName+Integer.toString(i)+".txt";
fileName = fileName.replaceAll("\\s","");
System.out.println(fileName);
File f= new File(filesDir+"/"+fileName);
f.createNewFile();
fileWriter = new BufferedWriter(new FileWriter(f));
fileWriter.write("");
fileWriter.write(fileContent);
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
System.out.println("in the exception!");
e.printStackTrace();
}
}
}
else {
System.out.println("no content");
}
System.out.println("done writing, exit app now");
}
public void exit(){
System.out.println("EXITING!");
Platform.exit();
System.exit(0);
}
}
The above class also has additional member classes who serve as POJOS to expose the structure of files being read/written to the "front-end" html.
I pass an instance of FileSystemBridge to the web view by overriding the default Browser class constructor and adding the following code.
webEngine.getLoadWorker().stateProperty().addListener(
(ObservableValue<? extends State> ov, State oldState,
State newState) -> {
if (newState == State.SUCCEEDED) {
JSObject context= (JSObject) webEngine.executeScript("window");
context.setMember("fsBridge", new FileSystemBridge());
webEngine.executeScript("init('desktop')");//the hook into our app essentially
}
});
The webEngine.executeScrit("init) essentially performs some initialization on our front end. Then on the javascript executing on the webview on user interaction we invoke our FileSystemBridge write method with a callback to invoke the FileSystemBridge's exit method, which is essentially a call to Platform.exit().
On user click
App.handleWrite(contentToBeWritten, function(success){
if (success){
console.log("inside success!");
App.handleExit();
}
});
then our handleWrite javascript function
handleWrite: function(content, callback){
fsBridge.callWrite(content);
retVal = true;//more logic to this but simplified for demo
callBack(retVal);
}
Now in the FileSystemBridge.exit() method I have added System.exit(0), which succesfully terminates my java instance which was the original problem.However I would like to know if this is the correct approach to handling the exiting of a java app which uses a JAVAFX webview. Are there unforeseen consequences to using System.exit(0) in this manner?