Home » Android » android – Failed to create component for 'dialog' reason: java.awt.HeadlessException

android – Failed to create component for 'dialog' reason: java.awt.HeadlessException

Posted by: admin June 15, 2020 Leave a comment

Questions:

After updating gradle to version 3.0.0-beta3 I have this error when I try to display a dialog box to ask for the password of the key.

I tried to add System.setProperty('java.awt.headless', 'false') but it did not work and I found nothing very new on google ….

Error:(20, 1) Failed to create component for ‘dialog’ reason:
java.awt.HeadlessException

java.awt.HeadlessException (no error message)

import groovy.swing.SwingBuilder

gradle.taskGraph.whenReady { taskGraph ->
        def storePassword = ''
        def keyPassword = ''
        if (System.console() == null) {
            new SwingBuilder().edt {
                dialog(modal: true, title: 'Enter password', alwaysOnTop: true, resizable: false, locationRelativeTo: null, pack: true, show: true) {
                    vbox {
                        label(text: "Please enter store passphrase:")
                        def input1 = passwordField()
                        label(text: "Please enter key passphrase:")
                        def input2 = passwordField()
                        button(defaultButton: true, text: 'OK', actionPerformed: {
                            storePassword = input1.password;
                            keyPassword = input2.password;
                            dispose();
                        })
                    }
                }
            }
        } else {
            storePassword = System.console().readPassword("\nPlease enter store passphrase: ")
            keyPassword = System.console().readPassword("\nPlease enter key passphrase: ")
        }

        if (storePassword.size() <= 0 || keyPassword.size() <= 0) {
            throw new InvalidUserDataException("You must enter the passwords to proceed.")
        }

        storePassword = new String(storePassword)
        keyPassword = new String(keyPassword)

        android.signingConfigs.Keys.storePassword = storePassword
        android.signingConfigs.Keys.keyPassword = keyPassword
}
How to&Answers:

Just add System.setProperty('java.awt.headless', 'false') before import groovy.swing.SwingBuilder and kill gradle deamon with ./gradlew --stop

it give you something liket that :

apply plugin: 'com.android.application'

System.setProperty('java.awt.headless', 'false')

import groovy.swing.SwingBuilder

Answer:

To solve this problem you have to add these 3 line exactly in this order (other orders don’t work)

apply plugin: 'com.android.application'

import groovy.swing.SwingBuilder

System.setProperty('java.awt.headless', 'false')

Answer:

def doPromptForPassword(msg) {
if (System.console() == null) {
    System.setProperty('java.awt.headless', 'false') // worked for me
    def ret = null
    new SwingBuilder().edt {
        dialog(modal: true, title: 'Enter password', alwaysOnTop: t

Answer:

For me it finally worked when I added System.setProperty('java.awt.headless', 'false') twice:

  • immediately after import
  • after System.console() == null check

But then it happens be ok with only after System.console() == null check.

Don’t forget to ./gradlew --stop as mentioned before.