The JavaExec task has a task action which executes the Java program. By using <<, you’re adding the configuration of the main, classpath, and args as a task action as well. When the task action provided by the JavaExec runs, the second task action to configure these values has not run yet. You likely want to configure these values in the configuration phase, not in a task action by removing the <<.
I don’t think you understood the responses to your question. You asked what you did wrong with your original code. Both @Rene and I both mentioned what you did wrong (included <<) at about the same time. In my answer, I also described what your code was actually doing from a Gradle lifecycle and implementation perspective so that you could understand why removing << was the answer.
You quoted “task action which executes the Java program.” Your encrypt task is defined as type JavaExec. Tasks have a sequence of task actions that are executed. JavaExec has one that executes the Java program with the main you specified. That’s just a fact of how it works, not a suggestion. The main point was just that the ordering needed to be correct.