How to generate (Java) source code in academically acceptable way - using abstract syntax tree?
It highly depends on what is the domain you are working on, i.e. why do you need to generate Java Source code anyway?.

Can you explain better what knowledge do you have stored in your knowledge base?.

The most "formal" method I can think of is by using the Java grammar and generate and abstract syntax tree as you mentioned. But it may or may not be appropriate, it depends on what are the task you want to solve.

If you want to use the grammar approach you may want to check antlr, I've used it to compare AST in plagiarism detection. Here is a repository with the grammars of many languages, including Java: https://github.com/antlr/grammars-v4