I'm testing Java 9 with a project which requires JPA (javax.persistence.*
classes). When I add the module-info.java
and declare my module, all classes under javax.persistece
package become unavailable.
I searched a lot, but I couldn't find the module to require to use JPA in a Java 9 module project.
UPDATE As Alan suggested, I ran
$ jar --describe-module --file=javax.persistence-api-2.2.jar
No module descriptor found. Derived automatic module.
[email protected] automatic
requires java.base mandated
contains javax.persistence
contains javax.persistence.criteria
contains javax.persistence.metamodel
contains javax.persistence.spi
But still with this module-info.java
module my.module {
requires java.persistence;
}
I get "module-info.java:[3,18] module not found: java.persistence".
UPDATE 2 This is my project structure:
.
├── pom.xml
└── src
├── main
│ ├── java
│ │ ├── my
│ │ │ └── module
│ │ │ └── MyBean.java
│ │ └── module-info.java
│ └── resources
└── test
├── java
└── resources
The pom.xml
has the javax.persistence-api
dependency.
Testing repo: https://github.com/heruan/java9-jpa
Defining the Java 9 module. A module is a collection of code, data, and resources. It is a set of related packages and types (classes, abstract classes, interfaces, and more) with code, data files, and some static resources. For example, the module descriptor module-info.
The Java Persistence API was first released as a subset of the Enterprise JavaBeans 3.0 specification (JSR 220) in Java EE 5. It has since evolved as its own spec, starting with the release of JPA 2.0 in Java EE 6 (JSR 317). JPA was adopted as an independent project of Jakarta EE in 2019.
Java Module System is a major change in Java 9 version. Java added this feature to collect Java packages and code into a single unit called module. In earlier versions of Java, there was no concept of module to create modular Java applications, that why size of application increased and difficult to move around.
With maven, you can use a dependency like
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
with maven-compiler-plugin
as updated to work with jdk9 detailed here.
Similar with dependency with gradle
compile group: 'javax.persistence', name: 'javax.persistence-api', version: '2.2'
which is based out of the javaee/jpa-spec. This would facilitate the
requires java.persistence
as an automatic module as proposed to be the name intended for the module here.
Adding to the details, this is defined in the META-INF/MANIFEST.MF as :
Manifest-Version: 1.0
Bundle-Description: Java(TM) Persistence 2.2 API jar
Automatic-Module-Name: java.persistence
...
Note- The way to figure out the module name as suggested by Alan is precise, but no advertising and I still prefer using a class of some package and then let IntelliJ(2017.2.4) do that resolution for me when I say 'import class' and then 'add requires'. ;)
I don't think that JPA has been published as a module yet but you should be able to use it as an automatic module. Can you use jar --file=<jarfile> --describe-module
to see what module name is derived for the JPA JAR file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With