Reputation: 11
Why does there need to be a forward slash in the path to the class file when using Windows Command Prompt?
Input:
C:\javafun>java --module-path C:\javafun\mod --module jdojo.intro/com.jdojo.intro.Welcome
Output:
Welcome to Java 17!
I've read that when running .jar files there needs to be a forward slash, but I've created a modular .jar file in C:\javafun\lib
directory. Here I am just trying to go straight to the .class file in the C:\javafun\mod\jdojo.intro\com\jdojo\intro\Welcome.class
Why when I change the below to a backslash when using Windows I get the following error? This is confusing to know when to use specific slashes when dealing with file locations, packages or directories.
Input:
C:\javafun>java --module-path C:\javafun\mod --module jdojo.intro\com.jdojo.intro.Welcome
Output:
Error occurred during initialization of boot layer
java.lang.module.FindException: Module jdojo.intro\com.jdojo.intro.Welcome not found
Upvotes: 0
Views: 588
Reputation: 44308
In Windows, file paths need to use the backslash as a separator. However, this is not a file path:
--module jdojo.intro\com.jdojo.intro.Welcome
The forward slash is a designated separator, on all platforms, between the module name and the class name.
The fact that, on non-Windows systems, file paths happen to use the forward slash character as a directory separator, but it’s a different character in Windows, does not mean that all non-file uses of the forward slash will also be different on Windows.
In fact, the documentation for the java command explicitly states that the character between the module name and the class name must be a forward slash:
To launch the main class in a module:
java
[options]-m
module[/
mainclass] [args ...]or
java
[options]--module
module[/
mainclass] [args ...]
No matter what platform you’re running on, the character between a module name and a class name is a forward slash, because it’s not a file name and therefore is not platform dependent.
Upvotes: 2