2
votes

I'm learning JavaEE CDI and I created a small application with NetBeans 8.0+Glassfish 4. After upgrading to NetBeans 8.0.1 and Glassfish 4.1 I'm getting a lot of errors reporting that some packages does not exist. For example, I cannot use the following code because I'm getting the message that package javax.enterprise.event does not exist.

package jlacerda;

import javax.inject.Inject;
import javax.enterprise.event.Event;

public class CMensagem 
{
    @Inject
    private Event<Evento> gerarEvento;

    public String getMensagem()
    {
        return "Hello world!";
    }

    public void gerarEvento()
    {
        Evento evento = new Evento();
        evento.setMensagem("Objeto criado a partir da classe CMensagem");

        gerarEvento.fire(evento);
    }
}

This situation also occurs with packages:

import javax.enterprise.inject.Alternative;
import javax.decorator.Decorator;
import javax.decorator.Delegate;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;

If I change the server to Glassfish 4.0 the same code runs as expected and all the packages are correctly imported.

I searched in the NetBeans and Glassfish forums but I did not found any situation like this one.

Thanks in advance for any sugestion that might help me solving this situation.

2

2 Answers

4
votes

With NetBeans 8.0.x you need only to open Project Properties, then go to Libraries section and add "Java EE Web 6 API Library". I have the same issue when migrated from NetBeans 7.4 to 8.0, but the solution is quite simple and obvious.

3
votes

Glassfish 4.1 updated its CDI version to 1.2 (Glassfish 4.0 used CDI 1.1). As you don't give info on how your application is packaged, I'll give you all the points to check:

  1. Check that don't have a cdi-api.jar file in your war

  2. Check you don't have any weld related jar in your war as well

  3. Use version 1.2 of CDI Api to compile your code. Check on the site spec to download the file or change your Maven config like this


<dependency>
    <groupId>javax.enterprise</groupId>
    <artifactId>cdi-api</artifactId>
    <version>1.2</version>
    <scope>provided</scope>
</dependency>

There are no change in the API between 1.1 and 1.2 but all the OSGi config has changed so you might encounter an issue with those changes.