5

After this question was answered i published java 9 modules tutorials + examples on Github and how to run for future users :

I have the below very simple structure:

src 
│   module-info.java
│ 
└───moduleA
    └───pack1
            Main.java

module-info.java :

module moduleA {

}

Main.java:

package moduleA.pack1;
public class Main{

 public static void main(String[] args){
   System.out.println("Hello Java 11");
 }
}

And i am trying to compile and then run this modular java application which is very simple .

So from the cmd i am running :

Compile

javac --module-source-path src -d out -m moduleA

Run

java  --module-path out -m moduleA/pack1.Main

enter image description here

From IntelliJ it works like charm , i don't know what magic it runs behind .

What am i doing wrong ?

ZhekaKozlov
  • 36,558
  • 20
  • 126
  • 155
GOXR3PLUS
  • 6,877
  • 9
  • 44
  • 93
  • 2
    Side note: While you run your application from IntelliJ, you can see the exact command as the first line in the Run/Debug window it has executed. – Naman Jun 04 '19 at 09:40
  • @Naman Aouu thank you :) => `"C:\Program Files\Java\jdk-11.0.3\bin\java.exe" -Dvisualvm.id=8708960290688 "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3.5\lib\idea_rt.jar=57689:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3.5\bin" -Dfile.encoding=UTF-8 -p C:\Atlassian\JPMS\out\production\JPMS -m moduleA/moduleA.pack1.Main` – GOXR3PLUS Jun 04 '19 at 10:11
  • `-p C:\Atlassian\JPMS\out\production\JPMS -m moduleA/moduleA.pack1.Main` you know your module and module path to specify then. – Naman Jun 04 '19 at 10:13
  • @Naman I took the `src` folder from IntelliJ and pasted it on desktop . `Actually i am following https://www.udemy.com/java-9-new-features-in-simple-way-jshell-jpms-and-more/ tutorial on Udemy , he does them same it runs for him , but i don't know what's wrong in my machine . – GOXR3PLUS Jun 04 '19 at 10:15

3 Answers3

4

--module-source-path is usually used to compile multiple modules at once. But of course, you can compile a single module with it if you want. However, you have to move the source files to the directory with the module name:

src
└───moduleA
    │───module-info.java
    └───moduleA
        └───pack1
            └───Main.java

Also, you should fix the command line which runs your module:

java --module-path out -m moduleA/moduleA.pack1.Main
ZhekaKozlov
  • 36,558
  • 20
  • 126
  • 155
  • @GOXR3PLUS Did you change `moduleA.pack1` to `pack1` in `Main.java`? – ZhekaKozlov Jun 04 '19 at 13:11
  • Yes , it works as expected again using `java --module-path out -m moduleA/pack1.Main` , is it good designing though ? Where is the `module-info.java` going in an application with `Maven packaging structure` , `src/main/java/com.companyName.projectName` – GOXR3PLUS Jun 04 '19 at 13:38
  • 2
    @GOXR3PLUS `module-info.java` should be in `src/main/java` – ZhekaKozlov Jun 04 '19 at 14:43
  • Excellent , thank you :) , i published also this => https://github.com/goxr3plus/java-modules-tutorial – GOXR3PLUS Jun 04 '19 at 14:45
2

The exact error i faced..(i was following the same example) Just make sure the type of file in windows against "module-info" is "JAVA File"

i did edit via notepad++ and selected java lang , changed the type explicitly to java.it worked for me,nothing extra i did as mentioned in some comments. i am pretty sure this must be the case for u also.

This error occurs if javac fails to locate module-info.java, so there should something wrong with that file(in my case it was file type)

PS: please give credits/author info (Durgasoft) since you have copied the notes information from there(in git hub).

Raj
  • 21
  • 1
1

Looks like you are trying to run it from desktop and the file is not present at the desktop folder.

please refer the below link and use relative path to execute

Unable to resolve module using --module-source-path

Kamu
  • 21
  • 5