Multi line string in Java 17

1k views Asked by At

I am using multi line strings in java 17.0.2 but have been recieving the following error

        String test = """
        {
            "deploymentResourceId": "deployment"
        }
        """;

Error:

/sparta/input/PackageResource.java:262:25: expecting SEMI, found '"
        {
            "'

2023-10-17 23:31:16.027 UTC [ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:2.17:check (verify-style-1) on project : Execution verify-style-1 of goal org.apache.maven.plugins:maven-checkstyle-plugin:2.17:check failed: begin 1253, end 18, length 18 -> [Help 1]

Using string builder works fine but i want to use multi line strings

2

There are 2 answers

3
Thomas Kläger On BEST ANSWER

The error message is produced by an outdated version of CheckStyle.

According to the CheckStyle ticket Support for Java 15 Strings you need at least CheckStyle version 8.36 if you want to use text blocks / multiline strings together with CheckStyle checks.

The error message (org.apache.maven.plugins:maven-checkstyle-plugin:2.17:check) indicates that your project uses the Maven-CheckStyle-Plugin version 2.17 which according to Maven Checkstyle Plugin Releases History by default uses CheckStyle version 6.11.2

You can try to configure your project to use the Maven CheckStyle Plugin 2.17 together with the CheckStyle version 8.36 using the following configuration (source: Upgrading Checkstyle at Runtime):

<project>
  ...
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-checkstyle-plugin</artifactId>
          <version>2.17</version>
          <dependencies>
            <dependency>
              <groupId>com.puppycrawl.tools</groupId>
              <artifactId>checkstyle</artifactId>
              <version>8.36</version>
            </dependency>
          </dependencies>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
  ...
</project>
0
pirateking On

Upgrading the maven-checkstyle-plugin and checkstyle resolved the issue:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-checkstyle-plugin</artifactId>
  <version>3.3.0</version>
  <dependencies>
    <dependency>
      <groupId>com.puppycrawl.tools</groupId>
      <artifactId>checkstyle</artifactId>
      <version>10.12.1</version>
    </dependency>
  </dependencies>
</plugin>