EJB com Jboss = erro

Estou tendo um erro e nao sei onde como resolver

[code]

public void inserirTest (TestForm testForm) throws NamingException {

		InitialContext ctx = getInitialContext();
		TestInterface test= (testInterface) ctx.lookup("myEjb/TestBean/remote");
		test.salvar(testForm);

}

private static InitialContext getInitialContext() throws NamingException{
		Properties env = new Properties();
		env.put(Context.SECURITY_PRINCIPAL, "guest");
		env.put(Context.SECURITY_CREDENTIALS, "guest");
		env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.security.jndi.JndiLoginInitialContextFactory");
		env.put(Context.PROVIDER_URL, "jnp://localhost:1099");
		return new InitialContext(env);
	
}[/code]

erro: java.lang.ClassNotFoundException: org.jboss.security.jndi.JndiLoginInitialContextFactory at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at com.sun.naming.internal.VersionHelper12.loadClass(Unknown Source) at javax.naming.spi.NamingManager.getInitialContext(Unknown Source) at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source) at javax.naming.InitialContext.init(Unknown Source) at javax.naming.InitialContext.<init>(Unknown Source) at br.stefanini.bo.ClienteBo.getInitialContext(ClienteBo.java:28) at br.stefanini.bo.ClienteBo.inserirCliente(ClienteBo.java:16) at br.stefanini.aplicacao.ClienteAction.inicio(ClienteAction.java:24) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270) at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187) at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431) at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236) at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196) at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432) at javax.servlet.http.HttpServlet.service(HttpServlet.java:637) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Unknown Source)

[quote=shenn]Estou tendo um erro e nao sei onde como resolver

[code]

public void inserirTest (TestForm testForm) throws NamingException {

		InitialContext ctx = getInitialContext();
		TestInterface test= (testInterface) ctx.lookup("myEjb/TestBean/remote");
		test.salvar(testForm);

}

private static InitialContext getInitialContext() throws NamingException{
		Properties env = new Properties();
		env.put(Context.SECURITY_PRINCIPAL, "guest");
		env.put(Context.SECURITY_CREDENTIALS, "guest");
		env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.security.jndi.JndiLoginInitialContextFactory");
		env.put(Context.PROVIDER_URL, "jnp://localhost:1099");
		return new InitialContext(env);
	
}[/code]

erro: java.lang.ClassNotFoundException: org.jboss.security.jndi.JndiLoginInitialContextFactory at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at com.sun.naming.internal.VersionHelper12.loadClass(Unknown Source) at javax.naming.spi.NamingManager.getInitialContext(Unknown Source) at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source) at javax.naming.InitialContext.init(Unknown Source) at javax.naming.InitialContext.<init>(Unknown Source) at br.stefanini.bo.ClienteBo.getInitialContext(ClienteBo.java:28) at br.stefanini.bo.ClienteBo.inserirCliente(ClienteBo.java:16) at br.stefanini.aplicacao.ClienteAction.inicio(ClienteAction.java:24) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270) at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187) at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431) at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236) at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196) at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432) at javax.servlet.http.HttpServlet.service(HttpServlet.java:637) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Unknown Source)

[/quote]

pelo erro da para saber que não foi encontrada a classe que você colocou no seu INITIAL_CONTEXT_FACTORY.

dando uma pesquisada rápida da para se saber qual classe vocÊ poderia chamar ao invés desta, testa ai :wink:

tipo eu pesquisei no google e achei isso

http://docs.oracle.com/javase/jndi/tutorial/basics/prepare/initial.html

fiz uma substituição nesse metodo:

[code]private InitialContext getInitialContext() throws NamingException{
/*
org.jboss.security.jndi.JndiLoginInitialContextFactory
weblogic.jndi.WLInitialContextFactory
com.sun.jndi.fscontext.RefFSContextFactory
com.sun.jndi.ldap.LdapCtxFactory

	 jnp://localhost:1099
	 */
		Hashtable env = new Hashtable();
		//Properties env = new Properties();
		env.put(Context.SECURITY_PRINCIPAL, "guest");
		env.put(Context.SECURITY_CREDENTIALS, "guest");
		env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
		env.put(Context.PROVIDER_URL, "ldap/localhost:1099"); // erro aqui agora..
		return new InitialContext(env);
	
}[/code]

Porem agora esta dando erro no env.put(Context.PROVIDER_URL, “ldap/localhost:1099”);

java.net.MalformedURLException: Not an LDAP URL: ldap//localhost:1099

eu estou começando no ejb agora, e não encontrei um lugar que explica o que são significa essas linhas

[code]

env.put(Context.SECURITY_PRINCIPAL, “guest”);
env.put(Context.SECURITY_CREDENTIALS, “guest”);
env.put(Context.INITIAL_CONTEXT_FACTORY, “com.sun.jndi.ldap.LdapCtxFactory”);
env.put(Context.PROVIDER_URL, “ldap/localhost:1099”); [/code]

olá amigo, segue um exemplo que fiz pra vc ver como usar um client remoto pra char um ajb no server glassfish ou jboss


package br.com.ejb.client;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;

import br.com.ejb.remote.IBeanRemote;
/** 
 * EJBClient que acessa um modulo ejb publicado (deploy) no servidor glassfish ou jboss
 * exemplo segue para acesso de uma aplicação standard Java SE com Swing etc...
 * @author Paulo
 *
 */
public class EJBClient {

	public static void main(String[] s) throws Exception {
		EJBClient t = new EJBClient();
		t.test();

	}

	private void test() throws Exception {

		Context ctx = null;

		try {

			// Properties para RMI no server glassfish
			Properties props = new Properties();
			props.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
			props.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
			props.setProperty("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
			props.setProperty("org.omg.CORBA.ORBInitialHost", "127.0.0.1"); // ip do seu server
			props.setProperty("org.omg.CORBA.ORBInitialPort", "3700"); // porta RMI Default
			
			// Você precisa criar o contexto exposto no server
			ctx = new InitialContext(props);

			// properties para RMI no JBOSS obs.: precisa incluir o jar jboss
			// client
			/*
			 * Hashtable environment = new Hashtable();
			 * environment.put(Context.INITIAL_CONTEXT_FACTORY,
			 * "org.jnp.interfaces.NamingContextFactory");
			 * environment.put(Context.URL_PKG_PREFIXES,
			 * "org.jboss.naming:org.jnp.interfaces");
			 * environment.put(Context.PROVIDER_URL, "jnp://10.200.1.21:1099");
			 * // remote machine IP InitialContext context = new
			 * InitialContext(environment);
			 */

			System.out.println("Lookup EJB... Isso Demora muito pra carregar o tempo todo");
			IBeanRemote ejb = (IBeanRemote) ctx.lookup(IBeanRemote.class.getName()); // Passe o nome da interface remota do seu bean

			System.out.println("Chamando métodos do EJB...");
			Long resultadoSoma = ejb.soma(2L, 2L);
			Long resultadoSobtrai = ejb.subtrai(10L, 5L);
			
			System.out.printf("[INFO] - IBeanRemote.soma(): %s\n", resultadoSoma.toString());
			System.out.printf("[INFO] - IBeanRemote.subtrai(): %s\n", resultadoSobtrai.toString());

		} catch (Exception e) {
			e.printStackTrace();
		}

		finally {
			if (ctx != null) {
				System.out.println("Closing context ...");
				ctx.close();
				System.out.println("Done Closing contex");
			}
		}

		System.out.println("..........É Noiz!!");
		System.exit(0);
	}

}

Espero ter ajudado
Fallow

eu estava terminando o exemplo aqui que estava fazendo e vi que o paulo1911 ja tinha postado um… (puxa vida estamos inclusive acostumados a usar calculadora hein…rs)

bom de qualquer jeito segue abaixo… depois do almoço eu vejo se descubro como que eu permito download aqui no forum… testado no jboss 6.

a interface local pra esse caso nem precisava…

[code]package pacote;
import javax.ejb.Remote;

@Remote
public interface CalculadoraRemote {

public int soma(int a, int b);
public int multiplicacao(int a, int b);
public int divisao(int a, int b);
public int subtracao(int a, int b);

}
[/code]

[code]package pacote;
import javax.ejb.Local;

@Local
public interface CalculadoraLocal {

public int soma(int a, int b);
public int multiplicacao(int a, int b);
public int divisao(int a, int b);
public int subtracao(int a, int b);

}
[/code]

[code]package pacote;

import javax.ejb.Stateless;

@Stateless(mappedName=“calculadoraStateless”)
public class Calculadora implements CalculadoraRemote, CalculadoraLocal {

public Calculadora() {
}

@Override
public int soma(int a, int b) {
	return a + b;
}

@Override
public int multiplicacao(int a, int b) {
	return a * b;
}

@Override
public int divisao(int a, int b) {
	return a * b;
}

@Override
public int subtracao(int a, int b) {
	return a / b;
}

}
[/code]

nos clientes eu usei servlets uma exemplificando usar a injeção via anotação e outra via jndi como no seu exemplo, caso seu não seja no servidor lembre-se do jar como nosso amigo disse:

[code]package pacote.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pacote.CalculadoraRemote;

/**

  • Servlet implementation class TesteAnotacao
    */
    public class TesteAnotacao extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @EJB
    private CalculadoraRemote calculadora;

    public TesteAnotacao() {
    super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    processRequest(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    processRequest(request, response);
    }

    private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

     PrintWriter pw = response.getWriter();
     pw.print(calculadora.soma(2, 3));
     
     pw.flush();
     pw.close();
    

    }

}
[/code]

[code]package pacote.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pacote.CalculadoraRemote;

/**

  • Servlet implementation class TesteIC
    */
    public class TesteIC extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public TesteIC() {
    super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    processRequest(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    processRequest(request, response);
    }

    private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

     PrintWriter pw = response.getWriter();
     
     try {
     	InitialContext ctx = getInitialContext();
     	CalculadoraRemote calculadora = getCalculadora(ctx);
     	pw.print(calculadora.soma(2, 3));
     } catch (NamingException e) {
     	pw.print("deu erro <br />");
     	e.printStackTrace(pw);
     }
     
     pw.flush();
     pw.close();
    

    }

    private InitialContext getInitialContext() throws NamingException{
    Properties env = new Properties();
    env.put(Context.SECURITY_PRINCIPAL, “guest”);
    env.put(Context.SECURITY_CREDENTIALS, “guest”);
    env.put(Context.INITIAL_CONTEXT_FACTORY, “org.jnp.interfaces.NamingContextFactory”);
    env.put(Context.PROVIDER_URL, “jnp://localhost:1099”);
    return new InitialContext(env);
    }

    private CalculadoraRemote getCalculadora(InitialContext ctx) throws NamingException{
    return (CalculadoraRemote) ctx.lookup(“calculadoraStateless”);
    }

}
[/code]

depois de volta eu vejo se coloco os projetos do eclipse aqui…

conforme prometido:

opa vlw ae galera isso vai ajudar muito, eu vo dar uma olhada e qualquer duvida eu volto a postar aqui =D

maior_abandonado eu importei seus projetos aqui, porem todos os projetos deram erro no build path na aba libraries no JRE System Library [jdk1.6.0_25] (unbound)

resolvi esse erro, porem agora em todos os projetos estao com o mesmo erro (deve ser configuração)

na aba markets (aquela que fica perto do console)

esta acusando

Faceted Projetc Problem

Target runtime JBoss 6.x Runtime is not defined.

alguem sabe o problema ?!

Botão direito sobre o projeto, Properties, Java Build Path, Aba Libraries, Clica no JBOSS e Edit, escolhe a versão que vc está usando e OK.