Zheng
Zheng

Reputation: 169

Error on "package does not exist" while using Maven

I am using Maven to build a HelloWorld Program. But I failed on

mvn package

Error messages are:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-    plugin:3.1:compile (default-compile) on project QuickStart: Compilation failure: Compilation failure:
[ERROR] /Users/xxx/java/QuickStart/src/main/java/hello/Greeting.java:[3,43] package org.hibernate does not exist
[ERROR] /Users/xxx/java/QuickStart/src/main/java/hello/Greeting.java:[8,9] cannot find symbol
[ERROR] symbol:  class Session 
[ERROR] location: class hello.Greeting

And I find that if I compile Greeting.java manually, everything will work fine, what's the reason ? Thank you.

Greeting.java is so simple:

package hello;

import org.hibernate.Session;

public class Greeting {

    public Greeting() {
        Session session;
    }

}

and my pom.xml is copied from spring.io:

  <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>QuickStart</groupId>
    <artifactId>QuickStart</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>


    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.0.7.Final</version>
        </dependency>
    </dependencies>


</project>

Upvotes: 1

Views: 4255

Answers (3)

Gonen I
Gonen I

Reputation: 6107

While it does not appear to be the problem in your case, these symptoms can also happen when your dependency's scope is runtime, for example:

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.0.7.Final</version>
        <scope>runtime</scope>
    </dependency>

it should of course be changed to

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.0.7.Final</version>
    </dependency>

Upvotes: 1

jhonata.sobrinho
jhonata.sobrinho

Reputation: 41

Actually, the org.springframework.context.support is not in any of the packages you put in the pom.xml. You need to add the spring-context-support dependency to run this code.

<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context-support</artifactId>
   <version>${spring-framework-version}</version>
</dependency>

Upvotes: 0

Mateusz Sroka
Mateusz Sroka

Reputation: 2335

Try to add this dependency:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
</dependency>

Upvotes: 1

Related Questions