0

What is the dependency to add to pom.xml in order to get rid of this:

java.lang.NoClassDefFoundError: java/sql/Date
    at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:445)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:587)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)

I'm with Java 17, but my production platform is with Java 8. So, I need a dependency, not a Java 9 module. Please don't suggest changing my dev or prod environments. I do understand that it would be the best option.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
yegor256
  • 102,010
  • 123
  • 446
  • 597
  • You shouldn't need a Maven import, `java.sql.Date` is still part of the JDK. Your error message mentions `java.base` which means that you're currently using the module system. You'll need to require module `java.sql`. That may only be necessary for Java 17, so maybe you can do that with some command-line flags. – Rob Spoor Jun 27 '22 at 16:57
  • @RobSpoor I want my `pom.xml` to compile with both Java 8 and Java 17. If I add command line args, it will stop working with Java 8. I'm looking for a solution similar to this one https://stackoverflow.com/a/43574427/187141 – yegor256 Jun 27 '22 at 17:00
  • Java 8 has passed the end of its support life. I would recommend LTS versions 11 and 17, nothing else. – duffymo Jun 27 '22 at 17:00
  • @RobSpoor if `java.sql.Date` would be part of JDK, I think it would exist in the classpath and `URLClassLoader` would find it. Or I miss something? – yegor256 Jun 27 '22 at 17:02
  • 1
    Your development environment isn't sane, when it's not compatible with production. Common sense may suggest to simply use Java 8 or 11 for development. Either downgrade the development environment or upgrade the production environment. Everything else is highly questionable. – Martin Zeitler Jun 27 '22 at 17:04
  • Do you have a module-info.java in your project? Then either remove it if you don't want to use modular java, or add `requires java.sql;` to it if you want to use JDBC. As others have mentioned, `java.sql.Date` is part of the Java standard library. It is also possible your code is doing some odd classloading, but in that case you will need to post a [mre]. – Mark Rotteveel Jun 27 '22 at 17:11
  • 1
    I strongly recommend you don’t use `java.sql.Date`. That class is poorly designed and long outdated. Instead use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). You may have known this already, but the comment should stand here for other users who may not be aware. – Ole V.V. Jun 27 '22 at 17:20
  • 1
    The only place where you should be using java.sql.Date is for relational database code. The java.time package should be preferred. – duffymo Jun 27 '22 at 17:28

0 Answers0