I need to execute java code in a docker container. This code uses swing to open a file chooser. Running the container produces the following error:
Exception in thread "main" java.awt.HeadlessException:
No X11 DISPLAY variable was set,
but this program performed an operation which requires it.
at java.desktop/java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:166)
at java.desktop/java.awt.Window.<init>(Window.java:553)
at java.desktop/java.awt.Frame.<init>(Frame.java:428)
at java.desktop/javax.swing.JFrame.<init>(JFrame.java:224)
at test.main(test.java:50)
This is the code that produces the error:
JFrame frame = new JFrame("select input");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JFileChooser chooser = new JFileChooser();
frame.add(chooser);
frame.setLocationRelativeTo(null);
int option = chooser.showOpenDialog(frame);
if (option == JFileChooser.APPROVE_OPTION) {
File selectedFile = chooser.getSelectedFile();
System.out.println(selectedFile.getAbsolutePath());
}
frame.pack();
frame.setVisible(true);
frame.dispose();
It seems i need to set a variable. How can I set this variable in a docker container?