Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

supposed compilation errors when using java 17 modules in visual studio code #3376

Open
bumby123 opened this issue Nov 4, 2023 · 7 comments
Labels

Comments

@bumby123
Copy link

bumby123 commented Nov 4, 2023

Environment
  • Operating System: windows 11
  • JDK version: "17.0.7" 2023-04-18 LTS
  • Visual Studio Code version: 1.84.0 user setup
  • Java extension version: v.0.25.15

When I create the following code I get a compilation error saying that certain packages do not exist. The steps I followed are:

1. create a project in java in my case it is called OCP JAVA 17

2. create a folder called 12. Modules (within this folder is the directory with program codes and modules written in Java 17)

3. create a folder called feeding

4. inside the feeding folder, create a module-info.java file with the following code
module zoo.animal.feeding {
    exports zoo.animal.feeding;
}
5. inside the feeding folder, create a package or folders in the following order zoo/animal/feeding and add the Task.java file with the following code
package zoo.animal.feeding;

public class Task {
    public static void main(String... args) {
        System.out.println("All fed!");
    }
}
6. compile Task.java class and module-info.java with following command in terminal opening from 12.Modules folder
   javac -p
   mods -d
   feeding
   feeding/zoo/animal/feeding/Task.java feeding/*.java

7. create a folder called mods and run the following command in the terminal to package our compiled code from the 12.Modules folder
   jar -cvf
   mods/zoo.animal.feeding.jar -C
   feeding/ .

8. run the program by entering the following command in the terminal from 12.Modules
   java -p
   mods
   -m
   zoo.animal.feeding/zoo.animal.feeding.Task
   must be printed in console All fed!

9. create a folder called care with the following file module-info.java with the code
module zoo.animal.care {
    exports zoo.animal.care.medical; //here is vsc supposed compilation error

    requires zoo.animal.feeding;
}
10. inside the care folder create 2 packages/folders in the following order zoo/animal/care/details and zoo/animal/care/medical

11. in the zoo/animal/care/details package add the following file HippoBirthday.java
// HippoBirthday.java
package zoo.animal.care.details;

import zoo.animal.feeding.*;

public class HippoBirthday {
    private Task task;
    public static void main(String... args) { //mio
        System.out.println("Feliz cumple"); //mio
    }
}
12. in the zoo/animal/care/medical package add the following Diet.java file
// Diet.java
package zoo.animal.care.medical;

public class Diet {
    public static void main(String... args) { //mio
        System.out.println("Diet"); //mio
    }
}
13. compile and package what is inside the care folder, in the terminal from 12.Modules with the following command
    javac -p
    mods
    -d
    care
    care/zoo/animal/care/details/_.java
    care/zoo/animal/care/medical/_.java
    care/module-info.
    java

jar -cvf mods/zoo.animal.care.jar -C care/ . 14) run the classes that are inside the zoo.animal.care.medical package (which vsc says does not exist) in terminal from 12.Modules java -p mods -m zoo.animal.care/zoo.animal.care.medical.Diet the word Diet should be printed in the console

If the program runs properly, there should be no reason for vsc to mark an error in red, such as in the module-info.java file in the care folder that says that the package zoo.animal.care.medical does not exist. Your help please

attached screenshots

imagen imagen

VS Code version: Code 1.84.0 (d037ac0, 2023-11-01T11:29:04.398Z) OS version: Windows_NT x64 10.0.22621 Modes:
System Info
Extensions (15)
A/B Experiments

@maxandersen
Copy link
Contributor

I find it hard to follow your example - can you attach a zip with a reproducer?

@bumby123
Copy link
Author

bumby123 commented Nov 7, 2023

Good morning Max, thank you for your help, I will send you the link to the repository with the complete project and instructions to execute it.

https://github.com/bumby123/modules-java-17-problem-vsc

@snjeza snjeza added the upstream label Nov 7, 2023
@bumby123
Copy link
Author

bumby123 commented Nov 7, 2023

Amm what is upstream Eclipse issue? There is not solution for vsc? Is that for each module-info. java file I would have create separate projects? Amm There will be some way to disable that false compilation error because in the end the code does work, it's just those red lines that cause confusion

@snjeza
Copy link
Contributor

snjeza commented Nov 7, 2023

Amm what is upstream Eclipse issue

See eclipse-jdt/eclipse.jdt.core#1465

There is not solution for vsc? Is that for each module-info. java file I would have create separate projects?

Right.

@bumby123
Copy link
Author

bumby123 commented Nov 9, 2023

I understand the truth, creating independent projects for each module-info seems a bit inappropriate. Maybe there is an option in the visual studio code to disable build error warnings only in specific project folders, perhaps by manipulating the settings or launch json?

@stephan-herrmann
Copy link

As I happened to find this issue just now, I tried the steps in the description using ecj instead of javac, and there's only one warning I see in all this:

----------
1. WARNING in xxx/modules-java-17-problem-vsc-master/care/zoo/animal/care/details/HippoBirthday.java (at line 7)
        private Task task;
                     ^^^^
The value of the field HippoBirthday.task is not used
----------
1 problem (1 warning)

Other than that compilation and running works flawlessly.

Apparently, this is not a problem of the compiler, but of how vscode configures the "project". Is a project in vscode directly mapped to what jdt calls a project? How does vscode invoke the compiler, from which directory, which options are being passed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants