Skip to content
Advertisement

VSCode Maven “X.java is not on the classpath of project Y”

Currently I have an issue with Maven is Visual Studio Code where it reports errors related to the classpath.

The project structure is as following:

project
| subproject1
--| src/main/java
  | src/main/resources
  | pom.xml
| subproject2
--| src/main/java
  | src/main/resources
  | pom.xml
| pom.xml

And the pom.xml of project has:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>nl.svenar.powerranks</groupId>
  <artifactId>powerranks</artifactId>
  <version>1.0</version>

  <packaging>pom</packaging>
  
  <modules>
    <module>subproject1</module>
    <module>subproject2</module>
  </modules>
  
  <build>
    <defaultGoal>clean compile package</defaultGoal>
  </build>
</project>

This POM only serves as ‘dummy’ in order to compile each sub project

And in the pom.xml of each subproject has the following:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>nl.svenar.powerranks</groupId>
  <artifactId>powerranks</artifactId>
  <version>2.0.0.248-bukkit-SNAPSHOT</version>

  <name>PowerRanks</name>

  <properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <src.dir>src/main/java</src.dir>
  <rsc.dir>src/main/resources</rsc.dir>
  </properties>

  <build>
    <defaultGoal>clean compile package</defaultGoal>
  <finalName>${project.name}</finalName>
  <sourceDirectory>${src.dir}</sourceDirectory>
    <resources>
      <resource>
        <directory>${rsc.dir}</directory>
        <filtering>true</filtering>
      </resource>
    </resources>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
          <configuration>
            <source>${java.version}</source>
            <target>${java.version}</target>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <version>2.3.1</version>
          <configuration>
            <outputDirectory>~/servers/java/bukkit/plugins/</outputDirectory>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <repositories>
    ... 
  </repositories>

  <dependencies>
    ... 
  </dependencies>

</project>

When going to any Java source file in a sub project I get the following error: SOURCENAME.java is not on the classpath of project PROJECTNAME, only syntax errors are reportedJava(32)

How can I add the src directory of each sub project to the classpath so it will report compile errors? When I tell VSCode to report compilation errors anyway everything is highlighted in red because it can’t find the dependencies.

When I run mvn compile package it compiles the sub projects successfully. Only VSCode is complaining.

Advertisement

Answer

In each sub project the parent needs to be defined as well.

...
  <parent>
    <groupId>nl.svenar.powerranks</groupId>
    <artifactId>powerranks</artifactId>
    <version>1.0</version>
  </parent>
...

And now VSCode works as expected.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement