Ciao a tutti ragazzi! Sto scrivendo un semplice esercizio per ejb ma mi da sempre questo errore (ho provato anche con programmi funzionanti su altri computer ma sul mio persiste sempre questo errore che ora vi allego)
Premetto che sto usando netbeans con glassfish
L'errore è:
codice:javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662) at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313) at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350) at javax.naming.InitialContext.lookup(InitialContext.java:417) at fornitoriclientejb.FornitoriClientEJB.main(FornitoriClientEJB.java:32) CODICE: // classe FornitoriClientEJB package fornitoriclientejb; import esame.FornitoreEJBRemote; import java.util.logging.Level; import java.util.logging.Logger; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; public class FornitoriClientEJB { public static final Logger log = Logger.getGlobal(); public static void main(String[] args){ try { log.info("Inizio..."); Context ctx; ctx = new InitialContext(); log.info("Contesto creato "); log.info("faccio lookup"); FornitoreEJBRemote ejbf = (FornitoreEJBRemote)ctx.lookup("java:global/Fornitori/FornitoreEJB!esame.FornitoreEJBRemote"); log.info("tutto apposto"); } catch (NamingException ex) { Logger.getLogger(FornitoriClientEJB.class.getName()).log(Level.SEVERE, null, ex); } } } // classe DatabasePopulator package esame; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.annotation.sql.DataSourceDefinition; import javax.ejb.EJB; import javax.ejb.Singleton; import javax.ejb.Startup; @Singleton @Startup @DataSourceDefinition( className ="org.apache.derby.jdbc.EmbeddedDataSource", name ="java:global/jdbc/EsameDS", user ="APP", password ="APP", databaseName ="EsameDB", properties ={"connectionAttributes =;crate=true"} ) public class DatabasePopulator { @EJB private FornitoreEJB ejb; private Fornitore f1,f2; @PostConstruct private void populatedDB(){ f1 = new Fornitore("123", "nome1", "cognome1", 23, 5); f2 = new Fornitore("3456", "nome2", "cognome2", 3, 1); ejb.createFornitore(f1); ejb.createFornitore(f2); } @PreDestroy private void clearDB(){ ejb.deleteFornitore(f1); ejb.deleteFornitore(f2); } } //classe databaseProducer package esame; import javax.enterprise.inject.Produces; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; public class DatabaseProducer { @Produces @PersistenceContext(unitName = "FornitoriPU") private EntityManager em; } // classe Fornitoreejbpackage esame; import java.util.List; import javax.ejb.LocalBean; import javax.ejb.Stateless; import javax.inject.Inject; import javax.persistence.EntityManager; import javax.persistence.TypedQuery; @Stateless @LocalBean public class FornitoreEJB implements FornitoreEJBRemote { @Inject private EntityManager em; @Override public List<Fornitore> findALLFornitore() { TypedQuery<Fornitore> query = em.createNamedQuery("Fornitore.findALL",Fornitore.class); return query.getResultList(); } @Override public List<Fornitore> findOrdermax(int max) { TypedQuery<Fornitore> query = em.createNamedQuery("Fornitore.findMaxOrder",Fornitore.class); query.setParameter(3,max); return query.getResultList(); } @Override public Fornitore findID(String id) { TypedQuery<Fornitore> query = em.createNamedQuery("Fornitore.findID",Fornitore.class); query.setParameter(1, id); return query.getSingleResult(); } @Override public Fornitore findCognome(String cognome) { TypedQuery<Fornitore>query = em.createNamedQuery("Fornitore.findCognome",Fornitore.class); query.setParameter(2, cognome); return query.getSingleResult(); } @Override public Fornitore createFornitore(Fornitore f) { em.persist(f); return f; } @Override public Fornitore updateFornitore(Fornitore f) { return em.merge(f); } @Override public Fornitore incrementaOrdini(String id, int i) { Fornitore f = this.findID(id); f.setValoteTotOrdini(f.getValoteTotOrdini()+i); return f; } @Override public void deleteFornitore(Fornitore f) { em.remove(em.merge(f)); } } //classe fornitore package esame; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.validation.constraints.NotNull; @Entity @NamedQueries({ @NamedQuery(name="Fornitore.findALL",query="SELECT f FROM Fornitore f"), @NamedQuery(name="Fornitore.findID",query="SELECT f FROM Fornitore f WHERE f.id=?1"), @NamedQuery(name="Fornitore.findCognome",query="SELECT f FROM Fornitore f WHERE f.cognome =?2"), @NamedQuery(name="Fornitore.findMaxOrder",query="SELECT f FROM Fornitore f WHERE f.maxOrder >?3") }) public class Fornitore implements Serializable { @Id private String id; @NotNull private String nome; @NotNull private String cognome; @NotNull private int maxOrder; @NotNull private int valoteTotOrdini; public Fornitore() { } public Fornitore(String id, String nome, String cognome, int maxOrder, int valoteTotOrdini) { this.id = id; this.nome = nome; this.cognome = cognome; this.maxOrder = maxOrder; this.valoteTotOrdini = valoteTotOrdini; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getNome() { return nome; } public void setNome(String nome) { this.nome = nome; } public String getCognome() { return cognome; } public void setCognome(String cognome) { this.cognome = cognome; } public int getMaxOrder() { return maxOrder; } public void setMaxOrder(int maxOrder) { this.maxOrder = maxOrder; } public int getValoteTotOrdini() { return valoteTotOrdini; } public void setValoteTotOrdini(int valoteTotOrdini) { this.valoteTotOrdini = valoteTotOrdini; } }
Aiuto!!

Rispondi quotando
