---
title: "使用jndi lookup ejb遇到的问题"
author: "jacky.jihao"
date: "2009-01-11T22:10:18Z"
tags: []
slug: "jndi-lookup-ejb"
draft: false
lastmod: "2009-01-11T22:10:18Z"
id: 314779
url: "https://jihao.iteye.com/blog/314779"
---

使用一个Main测试EJB的时候报这个错

代码：

```
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import cn.heapstack.HelloStateless;

public class Main {

	public static void main(String[] args) {
		Context context;
		try {
			context = new InitialContext();
			Object o = context.lookup("HelloStatelessBean");

			int j = ((HelloStateless) o).add(1);
			System.out.println(j);

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

}
```

异常：  
Exception in thread "main" java.lang.ClassCastException: org.jnp.interfaces.NamingContext  
at Main.main(Main.java:15)

分析原因，使用JBoss - jmx-console - JNDIView 进去  
Global JNDI Namespace查出来应该是这样的

+- MyHelloStatelessBean (class: org.jnp.interfaces.NamingContext)

| +- remote (proxy: $Proxy90 implements interface cn.heapstack.HelloStateless,interface org.jboss.ejb3.JBossProxy,interface javax.ejb.EJBObject)

 

所以正确的名称应该是 Object o = context.lookup("HelloStatelessBean/remote");