Multiline Java Strings in Eclipse

In .NET I could create multiline constants pretty easily. They would look something like this.

There isn’t an equivalent that comes baked-in with Java, but this simple solution will give you full Multiline String functionality in Java.

We start with Adrian Walker’s Java @Multiline annotation processor. Adrian explains the source and packages the annotation processor for Maven. Complete instructions for use are on his post.

If you’re not a 24/7 Maven Guy (like me), you can install the annotation updates in Eclipse very simply by following the instructions with this implementation of Multiline for Eclipse from Sanghyuk Jung. Screenshots will walk you through the process of:

1) Making sure the Project Specific Settings are enabled.

Project -> Properties -> Java Compiler

2) Add the Multiline JAR to the project’s build path

Project -> Properties -> Java Build Path -> Libraries

3) Under the project’s Java Compiler Annotation Processing Settings, enable annotation processing and processing in editor.

Project -> Properties -> Java compiler -> Annotation Processing

4) One final step is to add the Multiline JAR to the Factory Path.

Project -> Properties -> Annotation Processing -> Factory Path

There are good examples of using the Multiline Annotation Processor on the pages referenced above. Here’s how I used it earlier today. I’m using Jsoup to extract elements from HTML files and having Multiline support will really speed up coding the logic.

One tip I almost forgot to mention. Notice the // @formatter:off and // @formatter:on wrappings? That’s one of Eclipse’s best “hidden features.” Those are used to turn editor formatting on and off. ¬†Using them prevents the multiline area from being prefixed with “*” asterisks and mess up your multiline string were you to format your source document.

Windows -> Preferences -> Java -> Code Style -> Formatter -> Profile -> On/Off Tags

I should add that I’m using the Multiline Processor in Eclipse Kepler and Java 8, and it works very sweetly.