1

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Morkus
  • 517
  • 7
  • 21
  • A web app? Where are you expecting the file chooser to appear: on the server or on the client? – Andrew Thompson Oct 22 '19 at 17:08
  • Possible duplicate of [Exception in thread "main" java.awt.HeadlessException in spring boot java](https://stackoverflow.com/questions/58040229/exception-in-thread-main-java-awt-headlessexception-in-spring-boot-java) – George Z. Oct 22 '19 at 17:20
  • For clarity are you calling _get_ Property or _set_ Property? That's a _really_ important first letter in this case. – Ian McLaird Oct 22 '19 at 17:39
  • @Ian McLaird -- Sorry about confusion. Yes, was actually calling "setProperty()". :) When the getSelectedFile() runs, SpringBoot jumps to "Trowable targetException = var5.getTargetException()". The target says: "java.awt.HeadlessException". – Morkus Oct 22 '19 at 17:54
  • @AndrewThompson -- from SpringBoot, I'm just trying to show a file picker dialog (the chooser). This technique works fine with other REST frameworks so I'm just trying to see what I'm doing wrong here. Have tried to set the "Headless" setting the two ways I've seen them documented here on SO, but neither is working. Ideas??? Thx. – Morkus Oct 22 '19 at 17:55

0 Answers0