ADDITIONAL SYSTEM INFORMATION :
Tested on: Linux
JRE 1.8.0_202
A DESCRIPTION OF THE PROBLEM :
Opening a mail attachment within a JavaFx thread freezes the GUI in linux platforms.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
We want to open a mail attachment within a JavaFx application. We have a simple eml file that has a pdf attachment. I am unable to upload the exact eml file here, but any sample eml should work. We use aspose library to create the eml file and to read it.
Pre-requisites:
1. sample eml file containing a pdf attachment
2. test aspose email library
3. java fx libraries
4. pdf viewer
Test class:
The test class contains 2 test cases: TestingJavaFxThread, TestingBackgroundThread
Steps:
1. Include the sample eml file and run the test class. Please use test license version of aspose.
2. Click on the "Open the mail attachment" button. Mostly in the first attampt, the attachment will be extracted from the eml and opened with a PDF viewer.
3. When the button is clicked again, the GUI freezes and no further action is possible except restarting the machine or killing the process.
4. Please uncomment the test case TestingBackgroundThread and rerun the application. You can see that there is no GUI freeze.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The GUI should not freeze when the attachment is opened in a JavaFx thread.
ACTUAL -
The GUI freezes and crashes without any error message. GUI freeze occurs when the attachment is opened more than once.
---------- BEGIN SOURCE ----------
import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import com.aspose.email.Attachment;
import com.aspose.email.EmlLoadOptions;
import com.aspose.email.MailMessage;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class SimpleJavaFxApp extends Application {
static {
com.aspose.email.License email_license= new com.aspose.email.License();
try {
email_license.setLicense("XXX");
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void start(Stage stage) throws Exception {
Button btn = new Button();
btn.setText("Open the mail attachment");
File file = new File("mail.eml");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Attachment opened");
try {
TestingJavaFxThread(file); //Freezes from second time
//TestingBackgroundThread(file); //Does not Freeze
} catch (Exception e) {
e.printStackTrace();
}
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
stage.setTitle("JavaFx Application");
stage.setScene(scene);
stage.show();
}
public void TestingJavaFxThread(File file){
Platform.setImplicitExit(true);
// Run the task in a JavaFx thread
Platform.runLater(new Runnable() {
public void run() {
try {
OpenAttachment(file);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public static void main(String[] args) throws Exception {
launch(args);
}
public void TestingBackgroundThread(File file) throws Exception {
// Run the task in a background thread
Thread backgroundThread = new Thread(() -> { OpenAttachment(file); });
backgroundThread.setDaemon(true);
backgroundThread.start();
}
public static MailMessage ReadMessageFromEml(File eml_file) throws Exception {
return ReadMessageFromEml(eml_file.getAbsolutePath());
}
public static MailMessage ReadMessageFromEml(String eml_file) throws Exception {
EmlLoadOptions loadOptions = new EmlLoadOptions();
loadOptions.setPrefferedTextEncoding(StandardCharsets.UTF_8);
MailMessage message = MailMessage.load(eml_file, loadOptions);
if (message == null) {
throw new IOException();
}
return message;
}
public static void OpenAttachment(File file) {
MailMessage message;
try {
message = ReadMessageFromEml(file);
Attachment attachment = message.getAttachments().get_Item(0);
String filename = attachment.getName();
FileOutputStream stream = new FileOutputStream(filename);
attachment.save(stream);
Desktop.getDesktop().open(new File(filename));
} catch (Exception e) {
e.printStackTrace();
}
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Please uncomment the line: //TestingBackgroundThread(file);
By running the task in background thread, there is no GUI freeze. The same behaviour should be reflected in JavaFx thread as well.
FREQUENCY : always
Tested on: Linux
JRE 1.8.0_202
A DESCRIPTION OF THE PROBLEM :
Opening a mail attachment within a JavaFx thread freezes the GUI in linux platforms.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
We want to open a mail attachment within a JavaFx application. We have a simple eml file that has a pdf attachment. I am unable to upload the exact eml file here, but any sample eml should work. We use aspose library to create the eml file and to read it.
Pre-requisites:
1. sample eml file containing a pdf attachment
2. test aspose email library
3. java fx libraries
4. pdf viewer
Test class:
The test class contains 2 test cases: TestingJavaFxThread, TestingBackgroundThread
Steps:
1. Include the sample eml file and run the test class. Please use test license version of aspose.
2. Click on the "Open the mail attachment" button. Mostly in the first attampt, the attachment will be extracted from the eml and opened with a PDF viewer.
3. When the button is clicked again, the GUI freezes and no further action is possible except restarting the machine or killing the process.
4. Please uncomment the test case TestingBackgroundThread and rerun the application. You can see that there is no GUI freeze.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The GUI should not freeze when the attachment is opened in a JavaFx thread.
ACTUAL -
The GUI freezes and crashes without any error message. GUI freeze occurs when the attachment is opened more than once.
---------- BEGIN SOURCE ----------
import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import com.aspose.email.Attachment;
import com.aspose.email.EmlLoadOptions;
import com.aspose.email.MailMessage;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class SimpleJavaFxApp extends Application {
static {
com.aspose.email.License email_license= new com.aspose.email.License();
try {
email_license.setLicense("XXX");
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void start(Stage stage) throws Exception {
Button btn = new Button();
btn.setText("Open the mail attachment");
File file = new File("mail.eml");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Attachment opened");
try {
TestingJavaFxThread(file); //Freezes from second time
//TestingBackgroundThread(file); //Does not Freeze
} catch (Exception e) {
e.printStackTrace();
}
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
stage.setTitle("JavaFx Application");
stage.setScene(scene);
stage.show();
}
public void TestingJavaFxThread(File file){
Platform.setImplicitExit(true);
// Run the task in a JavaFx thread
Platform.runLater(new Runnable() {
public void run() {
try {
OpenAttachment(file);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public static void main(String[] args) throws Exception {
launch(args);
}
public void TestingBackgroundThread(File file) throws Exception {
// Run the task in a background thread
Thread backgroundThread = new Thread(() -> { OpenAttachment(file); });
backgroundThread.setDaemon(true);
backgroundThread.start();
}
public static MailMessage ReadMessageFromEml(File eml_file) throws Exception {
return ReadMessageFromEml(eml_file.getAbsolutePath());
}
public static MailMessage ReadMessageFromEml(String eml_file) throws Exception {
EmlLoadOptions loadOptions = new EmlLoadOptions();
loadOptions.setPrefferedTextEncoding(StandardCharsets.UTF_8);
MailMessage message = MailMessage.load(eml_file, loadOptions);
if (message == null) {
throw new IOException();
}
return message;
}
public static void OpenAttachment(File file) {
MailMessage message;
try {
message = ReadMessageFromEml(file);
Attachment attachment = message.getAttachments().get_Item(0);
String filename = attachment.getName();
FileOutputStream stream = new FileOutputStream(filename);
attachment.save(stream);
Desktop.getDesktop().open(new File(filename));
} catch (Exception e) {
e.printStackTrace();
}
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Please uncomment the line: //TestingBackgroundThread(file);
By running the task in background thread, there is no GUI freeze. The same behaviour should be reflected in JavaFx thread as well.
FREQUENCY : always