This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Module path and module name?

 
Ranch Hand
Posts: 127
5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am not exactly sure how Java module search for the module file in the directory.
If my module name (not filename) is different than the directory name, how do I compile and run using --module-path?

See below:

Step #1: compile OK! modules-info.java & SimpleMathFuncs.java. (These files have no dependencies on module. So I don't use --module-path.)
javac -d appmodules\appfuncs
appsrc\appfuncs\modules-info.java
appsrc\appfuncs\appfuncs\simplefuncs\SimpleMathFuncs.java


Step #2: To compile module-info.java & MyModAppDemo.java
This time program requires appfuncs module. So we need module path here.
javac --module-path appmodules -d appmodules\appstart
appsrc\appstart\module-info.java
appsrc\appstart\appstart\mymodappdemo\MyModAppDemo.java

This time appstartNEW module requires appfuncsNEW module. So I use --module-path appmodules.
But it gives  me error saying that appstartNew module cannot find appfuncsNEW module.
So I change the --module-path appmodules\appfuncs, then it will find appfuncsNEW module and COMPILE. Why is that?


Step #3: run application MyModAppDemo.
Then this command is ok.
java --modules-path appmodules
-m appstartNEW/appstart.mymodappdemo.MyModAppDemo

Why the --module-path "appmodules" work this time. Why not --module-path appmodules\appstart?


---------------------------------------------------------------------
// Module definition for the functions module
// Stores under directory appsrc\appfuncs
// module-info.java




// Module definition for the main application module
// Stores under directory appsrc\appstart
// module-info.java



My program hierarchy.
 
Bartender
Posts: 369
44
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While "--module-path" looks the same in both cases, don't forget that the first time you are using javac command's --module-path option and in the second, you are using the java command's --module-path option. They are not the same thing.

If you look for the description of each in the Oracle Tools Reference you will see that they are described differently:
javac command

--module-path path or -p path
Specifies where to find application modules.

java command:

--module-path modulepath... or -p modulepath
A semicolon (;) separated list of directories in which each directory is a directory of modules.


They are performing difference functions, so they don't necessarily work the same. It appears they do not. I couldn't find any description of how the javac or java commands search for modules, but then, it is not really necessary to know the mechanics  

I hope that helps!
 
author & internet detective
Posts: 40847
829
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

henry leu wrote:Step #1: compile OK! modules-info.java & SimpleMathFuncs.java. (These files have no dependencies on module. So I don't use --module-path.)
javac -d appmodules\appfuncs
appsrc\appfuncs\modules-info.java
appsrc\appfuncs\appfuncs\simplefuncs\SimpleMathFuncs.java



Is the exact command you ran? You  have module-info.java in the file layout description in your post, but modules-info.java here.
 
henry leu
Ranch Hand
Posts: 127
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:

henry leu wrote:Step #1: compile OK! modules-info.java & SimpleMathFuncs.java. (These files have no dependencies on module. So I don't use --module-path.)
javac -d appmodules\appfuncs
appsrc\appfuncs\modules-info.java
appsrc\appfuncs\appfuncs\simplefuncs\SimpleMathFuncs.java



Is the exact command you ran? You  have module-info.java in the file layout description in your post, but modules-info.java here.



Sorry, that was a typo.

It should be module-info.java.

I ran with module-info.java.
 
henry leu
Ranch Hand
Posts: 127
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still cannot resolved this problem.
 
Jeanne Boyarsky
author & internet detective
Posts: 40847
829
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure, but I think it is because yo uare just compiling the first module and not jarring it up. So when you try to compile the second module, it doesn't match how the book presents it.

You can see the command to jar (and all commands used in the modules chapter of the book here)
 
henry leu
Ranch Hand
Posts: 127
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:I'm not sure, but I think it is because yo uare just compiling the first module and not jarring it up. So when you try to compile the second module, it doesn't match how the book presents it.

You can see the command to jar (and all commands used in the modules chapter of the book here)



Yes, by making .class to JAR file, the program can compile and run.

But I thought Java also allows program to compile and run it in loose classes.

 
Jeanne Boyarsky
author & internet detective
Posts: 40847
829
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

henry leu wrote:But I thought Java also allows program to compile and run it in loose classes.


In general does. I've never tried that with modules though hence my suspicion of that being the reason.

Without looking anything up, I suspect it isn't a module if it isn't a jar. Which makes sense because modules are supposed to be self contained. Unlike a non-modular program which can be one giant collection fo files on the file system.
 
Jj Roberts
Bartender
Posts: 369
44
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

henry leu wrote:I still cannot resolved this problem.


Which problem can't you resolve? I thought your program was compiling and running.

Jeanne Boyarsky wrote:In general does. I've never tried that with modules though hence my suspicion of that being the reason.
Without looking anything up, I suspect it isn't a module if it isn't a jar. Which makes sense because modules are supposed to be self contained. Unlike a non-modular program which can be one giant collection fo files on the file system.

Is this an example from your book? Anyway, it is possible to compile and run a modular project like Henry demonstrates. Here is a verifiable example:
Project structure:Main.java:Module main's module-info.java:Print.java:Module printer's module-info.java:

Notice that like Henry's example, the module-info.java classes are not in a directory which matches the module name, meaning we can't compile with --module-source-path. We can compile the .java files individually. Starting with printer, we I'll compile it by specifying all of the .java files:I actually can (on Linux anyway) make it easier for myself by using find to find the .java files:
Now in addition to the src folder I have the following in my current working directory: Now I can compile module main:Now I have this:
I can run it withOutput: Hello
 
henry leu
Ranch Hand
Posts: 127
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Edit: quoted text removed]

Hi JJ,

Thanks for your illustration. It is the same procedure like mine.

Yes, I cannot use "--module-source-path" here because my module  name is not the same as the module folder name.

And this is what I want to point out. Because in Jeanne's book, she has different name for module and folder.


My original questions are:


1. --module-path in my step #2 see above for my original post
   When my module name (not module filename) are the same with the module folder, I can just tell the javac compiler like this "--module-path appmodules" to point to the source root folder that is one level up from my module folder. I don't need to tell javac compile to point to the module folder appfuncs "--module-path appmodules\appfuncs". This will work and compile.

2. But when my module names are different than the module folder name (module name is appfuncsNEW and appstartNEW. Module folders are appfunc and appfuncs), I can't say "--module-path appmodules". Compiler says cannot find module appfuncsNEW. I cannot just point to the source root folder which is one level up the module folder. I need to point one level down to the module folder. So I have to say "--module-path appmodule\appfuncs". This way javac compiler can find my appfuncsNEW module.

3. Also for java JVM, when I run the program using "java", it doesn't matter if my module name is the same as the folder or different than the folder name. I can always use "--module-path appmodule". I don't know how JVM dig into my module folders this way even if names are different.

I just want to know what is happening here.

I've read a lot of resources about Java module. They all use the same module name and same folder name.

I just want to know the theory behind this.


Thanks,

Henry
 
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry, just a note on your posts: it is unnecessary to quote all of the proceeding post.  Just press the Post Reply button.  Learn WhenToQuote (that's a link).
 
Jj Roberts
Bartender
Posts: 369
44
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so I understood you correctly the first time. I don't know where the mechanics of how java and javac search for modules is laid out. I suspect you might just have to leave it at "that's interesting, I wonder why".
 
henry leu
Ranch Hand
Posts: 127
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have found solution to my problems.

I visited Jeanne Boyarsky's website and saw her post about a book that she recommended while she took the Java OCP 11 exam. This book is called "The Java Module System" by Nicolai Parlog.
I bought the book. This is a very detailed oriented book. But for a Java beginner like me and I'm also new to this module topic, I could not digest it all. So I contacted the author Nicolai. And he was so kind to answer my question.

I would like to share his explanation below.


*******************************************************

* both `javac` and `java` can handle "exploded JAR files" (that's how
  `appmodules\appfuncs` and `appmodules\appstart` are called) on the
  module path
* `javac` requires folder names to match module names
* `java` doesn't care about folder names, only about the module names
  in the module descriptors

I rarely deal with exploded JARs (in the book, I always use `jar` to
package one modules' class files before compiling the next one) and when
I did, I apparently always had folder names that matched module names.
Hence, I never noticed the discrepancy between the last two bullet
points, but they explain what you observe.

In your example, module names (e.g. appfuncsNEW) and folder names (e.g.
`appfuncs`) don't match and so when compiling appstartNew with
`--module-path appmodules`, the compiler sees that appstartNew requires
appfuncsNew.

(1) It checks the "blank" module path `appmodules` but there's
    no `module-info.java` in there, so
(2) it creates the path `appmodules/appfuncsNEW` from module path
    and module name, but can't find it

Apparently it doesn't scan the folders in `appmodules` for module
descriptors and gives up instead. When you change the compiler command
to `--module-path appmodules/appfuncs`, step (1) finds the module and it
compiles.

The JVM you launch with `java --modules-path appmodules` is apparently
cleverer in searching for modules and scans the module path for folders
with module descriptors in there and so it finds modules regardless of
the names of the folders that contain them.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic