Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use dependencies without module-info.class in a Java 9 module

I created two small projects de.app1 and de.app2, where App from de.app1 uses Test from de.app2.

├── de.app1
│   ├── de
│   │   └── app
│   │       └── App.java
│   └── module-info.java
└── de.app2
    └── de
        └── test
            └── Test.java

module-info.java in the first project just contains module de.app1 {}

I compiled the second project and created a jar file:

javac de/test/Test.java
jar cf app2.jar de/test/Test.class

and then tried to compile the first project like this:

javac -cp ../de.app2/app2.jar de/app/App.java module-info.java

which failed because Test could not be found. When I compile the project without module-info.java, everything is working as usual.

Is it somehow possible to use classes from a jar that is not a Java 9 module within a Java 9 module? Especially for projects that depend on 3rd-party projects (e.g. apache-commons,...), I think something like this would be required.

like image 672
Jan Gassen Avatar asked Jul 08 '16 14:07

Jan Gassen


People also ask

Do you need a module-info Java file?

java Contents. To declare a jar file as a named Java module, one needs to provide a module-info. class file, which is, naturally, compiled from a module-info.

Which of the following is correct about module system in Java 9?

Q 7 - Which of the following is correct about Module System in Java 9? A - javac, jlink, and java have additional options to specify module paths, which further locate definitions of modules.

Which module is available to your named module without needing a Requires directive in Java?

xml module, code in modules that read java. desktop becomes dependent on java. xml . Without the requires transitive directive in java.

What is the use of module-info Java?

To set up a module, we need to put a special file at the root of our packages named module-info. java. This file is known as the module descriptor and contains all of the data needed to build and use our new module. We start the module declaration with the module keyword, and we follow that with the name of the module.


1 Answers

Yes, it is possible. What you are looking for are automatic modules.

To create one you simply put a regular JAR into a folder that you mention on the module path (e.g. guava-19.0.jar in a folder libs - see my example project). This tells the JVM to create a module from it. This module will get a name based on the file name (in this case guava), will read all other modules, and export all of its packages.

You can then require it with the name it was given (e.g. require guava; - see here).

like image 76
Nicolai Parlog Avatar answered Oct 19 '22 21:10

Nicolai Parlog