How to prevent Org Babel from wrapping Java src code blocks in boilerplate code

136 views Asked by At

In Org Mode in Emacs, while writing Java src code blocks, all src code blocks get wrapped in some boilerplate code. The boilerplate code includes the default class name which is Main and a main function. The boilerplate code is not included if src code block contains a class declaration, but in all other cases it is added.

I am trying to write a public enum in src code block. When evaluating this src code block the public enum gets wrapped in the boilerplate code which then causes compilation error.

Is there a way to prevent Org Babel from automatically wrapping src code block in boilerplate code and instead use the code in src code block as it is.

BTW I am using Doom Emacs distribution of Emacs.

screenshot

I tried searching online but couldn't get a proper answer, hence I am writing this issue.

1

There are 1 answers

2
Y. E. On

Looking into org-babel internals, we can see, that org-babel-execute:java calls org-babel-java--expand-for-evaluation function, docstring of which states:

Expand source block for evaluation. In order to return a value we have to add a __toString method. In order to prevent classes without main methods from erroring we add a dummy main method if one is not provided.

So this should be an expected behavior in order to prevent errors evaluating a source block.

As for the compilation error, Java Error: illegal start of expression seems to address it.

[The question text & screenshot doesn't look fully aligned to me. Not having Java setup currently to verify, I might miss something.]