0

I'm trying to play with the Jackson package in a very simple custom-layout java project:

├── main.java
└── pom.xml

And here are the contents of both files:

main.java

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

class Main {
  static String tags = "[]";
  public static void main(String[] args) {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode root;
    root = mapper.readTree(tags);
  }
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>my-group</groupId>
  <artifactId>my-app</artifactId>
  <version>1.0-SNAPSHOT</version>
 
  <name>my-app</name>
  <dependencies>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.13.0</version>
    </dependency>
  </dependencies>

  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  
  <build>
    <sourceDirectory>${project.basedir}</sourceDirectory>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.3.0</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <classpathPrefix>lib/</classpathPrefix>
              <mainClass>Main</mainClass>
            </manifest>
          </archive>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Now, when I run mvn package it gives me this error:

... main.java:[9,27] unreported exception com.fasterxml.jackson.core.JsonProcessingException; must be caught or declared to be thrown

When I first had this error, I had a bigger JSON string to test with, then I suspected the issue might be a parsing one, so I simplified things by replacing it with just a simple empty array [] as shown in the code, with no luck.

How can I fix this error?

Samha'
  • 369
  • 2
  • 7
  • Do you use any editor like eclipse or something? Because readTree method throws IOException and JsonProcessingException. It should be handle by try/catch or throws in your main method. With editor tool, it show emit an error/warning for you. – Huy Nguyen Oct 23 '21 at 04:28
  • @huy I use VSCode and the terminal, I've tried enclosing them in a `try ... catch`, but just suppressing the errors with no clear benefit doesn't address the main issue. – Samha' Oct 23 '21 at 04:32
  • I just guess from your including code, it will throw compile error, you should include the update and full stack trace error. Then others can know your problem more easily. – Huy Nguyen Oct 23 '21 at 04:34
  • @huy the above code is the latest minified version producing this error. Enclosing them in a `try ... catch` clause suppressed the errors -- as expected -- but didn't give the desired output. – Samha' Oct 23 '21 at 04:37
  • 1
    Old error is "must be caught or declared to be thrown"... It relative to my previous command, you don't include new stack trace yet ... – Huy Nguyen Oct 23 '21 at 04:40
  • 1
    In my case, I am working well. I enclosed try.. catch. I think you had better modify your code and Error's detail. – bittap Oct 23 '21 at 04:44
  • @huy @lionThunder It worked successfully when I enclosed it in `try ... catch`, must've been trying something wrong. Thank you! – Samha' Oct 23 '21 at 04:53
  • Note: this is a **compilation** error. Therefore it can be nothing to do with the actual JSON that you are trying to parse. And there **won't** be a stacktrace. The compilation error is telling you that your code must catch a checked exception that *could* be thrown when the program is run. – Stephen C Sep 13 '22 at 04:45

2 Answers2

0

Thanks to @huy's comment, the message is descriptive enough and it was a matter of enclosing the parsing function in a try ... catch clause.

The updated code would be:

main.java

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;

class Main {
  static String tags = "[]";
  public static void main(String[] args) {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode root;
    try {
      root = mapper.readTree(tags);
      System.out.println(root.toString());
    } catch (JsonProcessingException e) {}
  }
}
Samha'
  • 369
  • 2
  • 7
0
public JsonNode readTree(String content)
                  throws IOException,
                         JsonProcessingException

You should handle exception with try/catch or throws in main method.

Huy Nguyen
  • 1,931
  • 1
  • 11
  • 11