How to set up a java module to run directly (not via a .jar file) without having to specify a main class

How does one set up a java program that can be run as a module simply by doing java -m mymodule?

I know this is possible because I have seen it in, e.g., the jwebserver documentation; you can run this by doing java -m jdk.httpserver. However there doesn’t seem to be any evidence in the jdk source code about specifying the ‘main’ class of the module at any point, neither in the module-info.java file, nor in any of the main-containing classes of the module (of which there are two, not one).

How does the module know which main-containing class to pick as the main entry point for running the module? Is there some sort of convention that’s not well documented? (e.g. if a package in the module contains a class called Main.java, is this special in any way? and should there only be one Main.java file over all packages in the module?)

In https://dev.java/learn/modules/intro/ there’s an example of a module run without specifying the main class in the commandline, but it just says:

To launch a modular app, run the java command with a module path and a so-called initial module – the module that contains the main method

but without clarifying how this main method (and its respective class/package) is detected or specified for the module as the specific package/class to be used as the main entry point; and there’s no requirement for a module’s packages to not have more than one such main-containing class.

Please note the question is not about .jar files or specifying a main class for a jar file containing a module, but for modules themselves. (https://dev.java/learn/modules/building/ has good information for specifying main classes for jar files with modules, but this is not what I’m asking here).

11

Answering own question. I found the explanation here: https://youtu.be/C5yX-elG4w0?feature=shared&t=1857

There are two ways to create something that can be launched as java -m <modulename> without specifying the main class (i.e. without having to do java -m <modulename>/<mainclass> explicitly:

  • The first is to create a modular jar file, where the –main-class is specified; this allows you then to run the module as java -p <jarfile>.jar -m <modulename> syntax (as an alternative to java -jar <jarfile>.jar).

    However, if one inspects the default jre, there is no jdk.httpserver.jar file or similar to be found, nor are we loading one onto the module path. So this is not what is happening in the example in the question.

  • The second is to create your own jre environment (i.e. resulting in a ‘java’ command) by using jlink. This allows you to ‘bake’ the modules right into your runtime environment (i.e. java --list-modules will list <modulename> as one of the modules contained in the jre itself).

    Modules can be provided to jlink in two ways: as normal compiled module root folders in the module path, or as .jmod files. In the latter case, one can additionally have the ‘mainclass’ baked into the module-info.class file contained in the .jmod file, by passing the --main-class option to the jmod command during creation of the .jmod file.

    When this is the case, running java -m <modulename> via your new jre will indeed run the module baked into the jre, without that module necessarily appearing anywhere in the generated jre directory structure (not even as a .jmod file).

Since the jdk.httpserver is baked into the default jre environment (which can be confirmed by doing java --list-modules), this allows java -m jdk.httpserver to run directly. The implication here is that the java command itself was generated via a jdk.httpserver.jmod file, which specified the mainclass. We can confirm this is indeed the case by running jmod describe jdk.httpserver.jmod, which yields (among other lines) the line:

main-class sun.net.httpserver.simpleserver.Main

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật