Java论坛网»Java技术»在做一个EJB例子时,出现了这样的问题

在做一个EJB例子时,出现了这样的问题

问?:
run.HelloWorldClient:
[java] Exception in thread "main" 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
[java] at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
[java] at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
[java] at javax.naming.InitialContext.getNameInNamespace(InitialContext.java:497)
[java] at ejb3.test.client.HelloWorldClient.main(HelloWorldClient.java:13)

请问,InitialContext要怎么配置才能解决这个问题
答!: 1:
这个问题也是太抽象了。。。。容器是什么?错误一贴就可以解决所以的问题啊。。。。
昏。。。。。。。
答!: 2:
主函数格式应为:public static void main(String[]args){}
答!: 3:
主函数没错啊, 
这个是主函数:

package ejb3.test.client;

import javax.naming.InitialContext;
import ejb3.test.session.HelloWorld;
public class HelloWorldClient
{
public static void main(String[] args) throws Exception
{
InitialContext ctx = new InitialContext();
HelloWorld helloWorld = (HelloWorld) ctx.lookup("HelloWorldBean/remote");
System.out.println(helloWorld.echo("Hello World, Joeyta"));
}
}
答!: 4:
这个是我写的EJB:

package ejb3.test.session;

public interface HelloWorld {
public String echo(String msg);
}

package ejb3.test.session;

import javax.ejb.Local;
@Local
public interface HelloWorldLocal extends HelloWorld {
}

package ejb3.test.session;

import javax.ejb.Remote;
@Remote
public interface HelloWorldRemote extends HelloWorld {
}

package ejb3.test.session;

import javax.ejb.Stateless;
@Stateless
public class HelloWorldBean implements HelloWorldRemote, HelloWorldLocal {
public String echo(String msg) {
System.out.println("Joeyta try Hello World.");
return msg;
}
}
答!: 5:
最后是Ant的配置:


<project name="HelloWorld" default="run.HelloWorldClient" basedir=".">
<property environment="env" />
<property name="jboss.home" value="${env.JBOSS_HOME}" />
<property name="classes.dir" value="bin" />

<path id="classpath">
<fileset dir="${jboss.home}/client">
<include name="**/*.jar" />
</fileset>
<pathelement location="${classes.dir}" />
</path>

<target name="clean">
<delete file="${basedir}/HelloWorld.jar" />
<delete file="${jboss.home}/server/ejb3/deploy/HelloWorld.jar" />
</target>

<target name="ejbjar" depends="clean">
<jar jarfile="HelloWorld.jar">
<fileset dir="${classes.dir}">
<include name="ejb3/test/session/*.class" />
</fileset>
</jar>
<copy file="HelloWorld.jar " todir="${jboss.home}/server/ejb3/deploy" />
</target>

<target name="run.HelloWorldClient" depends="ejbjar">
<java classname="ejb3.test.client.HelloWorldClient" fork="yes" dir=".">
<classpath refid="classpath" />
</java>
</target>
</project>
答!: 6:
可以参看:http://www.blogjava.net/qiyadeng/archive/2006/02/04/29547.html
InitialContext ctx = new InitialContext();这句你注意,根据容器的厂商不一样,写法有点不一样。。。。。。
答!: 7:
你少了jndi的环境,加上jndi.properties文件
InitialContext ctx = new InitialContext();
如果你在jboss下,是没有错的。
上面说的是jndi初始化异常,不信你可以设置断点,
执行InitialContext ctx = new InitialContext();就会异常

相关JAVA教程:
aop实现权限管理的讨论,希望大家积极参与!
请教一个dom4j解析xml的问题
新手请教:为什么我的validator框架不验证?
大家帮我看看,在spring环境下做单元测试怎么通不过呀,错误日志
客户端调用Sun Application Server 的EJB问题
myeclipse中启动jboss错误!
APPFUSE中的问题
一个长久未解决的问题
如何从资源文件给Struts的标签初值?
一个建表的问题
调试roller2.3版的blog问题
eclipse+weblogic 开发j2ee时怎么生成ejb-jar.xml等描述符文件?