-2

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

6
  • 6
    The error message is not from the Java Compiler but from the checkstyle plugin / from checkstyle. It could be that you are using an old version of the checkstyle plugin (your version is 2.17, the current version is 3.3.0) and / or an old version of checkstyle itself that doesn't yet support the multiline strings that have been added to Java 17. The fix would be to upgrade the checkstyle plugin and the checkstyle version. Commented Oct 18, 2023 at 17:32
  • 4
    @OHGODSPIDERS Java has support for multiline strings since around Java 15 (stackoverflow.com/questions/878573/…) and the OPs code is valid Java code for Java 17. Commented Oct 18, 2023 at 17:34
  • 4
    According to github.com/checkstyle/checkstyle/issues/9111 you need at least CheckStyle version 8.36 (and a version of the CheckStyle plugin that supports CheckStyle 8.36) Commented Oct 18, 2023 at 17:40
  • 1
    The close as duplicate isn't correct - an unescaped quote is valid in multi-line declaration, its just that checkstyle isn't up to date. Commented Oct 18, 2023 at 18:11
  • 1
    @ThomasKläger your comments would form the basis of a good answer. Hint, hint. Commented Oct 18, 2023 at 18:18

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that resolved my issue . I am trying to update my text block as below to include some variables String test = """ { "deploymentResourceId": "%s" } """.formatted(deploymentResourceId); but the variable is not getting replaced in the json. do you know how to resolve this? i also tried
I also tried String test = """ { "deploymentResourceId": "$deploymentResourceId" } """;
@pirateking you should open a new question for your new problem.
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.