问题
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.
java.persistence@2.2 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
回答1:
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'. ;)
回答2:
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.
来源:https://stackoverflow.com/questions/46471927/which-module-should-i-require-in-java-9-to-use-jpa