0

I have a multi module maven project, and in the dao module, I added the JSON-IO dependency. When I try to deserialize my object, it gives me:

Exception in thread "main" com.cedarsoftware.util.io.JsonIoException: Class listed in @type [hu.kleatech.projekt.model.Employee] is not found

The class name is correct, the Employee is public, and the dao has the module as dependency. What could have gone wrong?

Edit: Since this is an old question and have been answered long ago, I'm deleting the github repository that I made specifically for this question. The solution to the problem is in the accepted answer, the exact code is not relevant.

Ádám Bozzay
  • 529
  • 7
  • 19

3 Answers3

1

Please try adding an empty constructor to Employee class.

Edit: Actually, while adding an empty constructor solves the problem, it is not necessarily required. Json-IO "will make a valiant effort to instantiate passed in Class, including calling all of its constructors until successful. The order they tried are public with the fewest arguments first to private with the most arguments."

(copied from MetaUtils.java javadoc)

Also, when calling a multi-argument constructor, the library fills the arguments with nulls and defaults for primitives. Then any exceptions thrown during the constructor call is ignored. In your case, a NullPointerException was thrown, because the constructor is not null-safe. So either modify the constructor so that it can handle nulls, or add an empty constructor.

Pecc
  • 73
  • 4
0

As per your project and modules Pom your main Pom should have modules in following order ....

    <modules>
        <module>core</module>
        <module>controller</module>
        <module>service</module>
        <module>dao</module>
    </modules>
  • service depends on core so core should be build before service
  • dao depends on service and core both so dao should be after core and service.

Employee class is available in core and it should be available in core jar.

You should add depencyManagent in main Pom and then add all the module as dependencies in dependencyManagement so whoever adds your main Pom as dependency will be able to access all your jars.

Once you change order build your project again and then update your maven project.

If this code is being used in another project then make sure that you have uploaded jars to repository (mvn deploy) so whoever uses it can download it when they are building their project.

One way to verify whether this jar is downloaded in the main project where it is used or not is check in project explorer there would be a Maven Dependencies section where you can see all dependency jars and check if core is present or not.

I am not sure what controller module is doing in main Pom as I couldn’t find a module by that name in your project so you should either remove it or add a module (folder) for it.

JRG
  • 4,037
  • 3
  • 23
  • 34
  • There is an empty Controller modul, I just didn't include that in the commit. Changing the order of the modules does not make any difference, still the same problem – Ádám Bozzay Oct 30 '17 at 16:20
  • Did you rebuild your project and update maven project? – JRG Oct 30 '17 at 16:22
  • I did a Clean and Build, and a Build with Dependencies with Netbeans. What does refresh means? – Ádám Bozzay Oct 30 '17 at 16:25
  • You should add depencyManagent in main Pom and then add all the module as dependencies in dependencyManagement so whoever adds your main Pom as dependency will get all your jars. – JRG Oct 30 '17 at 16:33
  • In eclipse usually there is a way to refresh and update maven project by right click – JRG Oct 30 '17 at 16:35
0

Maven dependency configuration is hierarchical from <parent> element not from <modules> element.

It means that in the project's pom.xml file where you have dependency on "JSON-IO dependency" you do not have dependency on your dao project or where that class is.

<modules> stands only to define what projects to build. Order of modules definition does not matter, since Maven detects order by required dependencies

So, you can define dependency in <parent> pom.xml either in

  • <dependencies> element. then all children will have it.

  • or in <dependencyManagement> - then children who need it can include it in their <dependencies> without common configurations like version, scope etc...

look at quite similar answer here: How to minimize maven pom.xml

Vadim
  • 4,027
  • 2
  • 10
  • 26
  • I don't understand. "in the project's pom.xml file where you have dependency on "JSON-IO dependency"": I don't have JSON-IO dependency in the project's pom.xml, I have it in the DAO module's pom.xml. And what should I write in what parent? – Ádám Bozzay Oct 30 '17 at 19:45
  • When you run your application and see exception about Class Not Found - it means that class is not in Class Path. Maven determines Class Path entries from defined and transitive dependencies in `` elements. Actual dependencies are collecting in Maven from `parent/child` hierarchy as defined above. It does not matter are there `` hierarchy or not. It has no deals with dependencies. So, if you are in Eclipse Maven editor - you need to check tab "Effective POM" (or "Dependency Hierarchy") of your project and see is "dao" project dependency there. I guess it is not – Vadim Oct 30 '17 at 20:11
  • I still don't understand anything about how this parent-child relationship works, and how it influences the dependencies. There is no Maven editor in Netbeans, I can't check it. – Ádám Bozzay Nov 01 '17 at 10:51
  • It is simple. Models hierarchy does not propogate dependencies through modules.it define onle what to build. Dependencies propogate through pardnt- child hierarchy. Did you read my answer in the link I gave you? – Vadim Nov 01 '17 at 13:42