Just trying to get a file picker dialog running in SpringBoot 2, but I am getting the java.awt.headless exception with the following code:
String returnFileName = "No file selected.";
JFileChooser getFile = new JFileChooser();
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
int result = fileChooser.showOpenDialog(getFile);
if (result == JFileChooser.APPROVE_OPTION)
{
File selectedFile = fileChooser.getSelectedFile();
returnFileName = selectedFile.getAbsolutePath();
}
return returnFileName;
}
My Spring Application tries to set the headless property this way (using the SO link at the bottom):
@SpringBootApplication
public class Application extends SpringBootServletInitializer
{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder applicationBuilder)
{
return applicationBuilder.sources(Application.class);
}
public static void main(String[] args)
{
// SpringApplication.run(Application.class, args);
SpringApplicationBuilder builder = new SpringApplicationBuilder(Application.class);
builder.headless(false);
ConfigurableApplicationContext context = builder.run(args);
}
}
This does not work. When the fileChooser.getSelectedFile() runs, I get an invocation error with the 'java.awt.headless' exception.
I also tried another suggestion I saw here on SO:
System.getProperty("java.awt.headless", "false"); in the method code itself, but that did not work either -- still get the headless exception.
Spring Boot : java.awt.HeadlessException
Would appreciate any insights what I'm doing wrong.