使用GlassFish 4进行JNDI独立应用程序的EJB 3.1问题4 [英] EJB 3.1 Problems with JNDI Stand-Alone Application with GlassFish 4

查看:133
本文介绍了使用GlassFish 4进行JNDI独立应用程序的EJB 3.1问题4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在独立测试应用程序的JNDI部分遇到问题。我在类路径中有 gf-client.jar jndi-properties.jar 文件。我确保在运行客户端之前在glassfish服务器上部署了最新版本。



独立应用程序:

  public class Main {

/ **
* @param args命令行参数
* /
public static void main(String [] args)throws NamingException {
Context context = new InitialContext();
TestBeanRemote bean =(TestBeanRemote)context.lookup(java:global / server / TestBean);
System.out.println(bean.sayHello(Sarah));
}

}

EJB 3.1应用程序:

  @Stateless(name =java:global / server / TestBean)
公共类TestBean实现TestBeanLocal,TestBeanRemote {

@Override
public String sayHello(String name){
returnHello+ name +!;







 @Remote 
public interface TestBeanRemote {

String sayHello(String name);

例外:

<$ px p> 线程main中的异常javax.naming.NoInitialContextException:无法实例化类:com.sun.enterprise.naming.impl.SerialInitContextFactory [根异常是java.lang.ClassNotFoundException:com.sun .enterprise.naming.impl.SerialInitContextFactory]在javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:674)

。在javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.init(InitialContext.java:244)
at javax.naming.InitialContext。< init>(InitialContext.java:192)
at com.gmail.physicistsarah .servertestclient.core.Main.main(Main.java:23)
导致:java.lang.ClassNotFoundException:com.sun.enterprise.naming.impl.SerialInitContextFactory $ b $ java.net.URLClassLoader $ 1 .run(URLClassLoader.java:372)在java.net.URLClassLoader上
$ 1.run(URLClassLoader.java:361)$ java
ecurity.AccessController.doPrivileged(本地方法)$ b $在java.net.URLClassLoader.findClass(URLClassLoader.java:360)
在java.lang.ClassLoader.loadClass(ClassLoader.java:424)
在sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:308)$ b $在java.lang.ClassLoader.loadClass(ClassLoader.java:357)
在java.lang.Class.forName0(本地方法)
在java.lang.Class.forName(Class.java:340)
在com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:72)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:61)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:672)
... 4 more
Java结果:1

编辑:好像它实际上在第一行中断了初始上下文被声明。我遵循 https://glassfish.java.net/javaee5/ejb/上的说明EJB_FAQ.html ,根据它,调用 InitialContext 的构造函数时不需要参数,所以这种行为对我来说很神秘。我使用的是NetBeans 8 IDE,我有 gf-client.jar jndi-properties.jar 使用project / properties / libraries选项卡附加到项目。注:这些罐子只附加到独立客户端。



EDIT2:我创建了一个新的客户端项目,引用了它的合适的服务器项目,现在我得到运行时出现新的异常:

  C:\ Users \ PC \Documents\NetBeansProjects\Server Client \ nbproject\build-impl.xml:990:C:\ Users \ PC \GlassFish_Server\glassfish\domains\Server\generated\xml\Server客户端不存在。 
at org.apache.tools.ant.types.AbstractFileSet.getDirectoryScanner(AbstractFileSet.java:484)
at org.apache.tools.ant.taskdefs.Copy.execute(Copy.java:487)
在org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
在sun.reflect.GeneratedMethodAccessor197.invoke(来源不明)
。在sun.reflect.DelegatingMethodAccessorImpl。 invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils。 java:106)
在org.apache.tools.ant.Task.perform(Task.java:348)
在org.apache.tools.ant.Target.execute(Target.java:435)
at org.apache.tools.ant.Target.performTasks(Target.java:456)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
在org.apache.tools.ant.Project.executeTarget(Project.java:1364)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org .apache.tools.ant.Pro ject.executeTargets(Project.java:1248)
位于org.apache.tools.ant.module.bridge.impl.BridgeImpl.run(BridgeImpl.java:286)
位于org.apache.tools。 ant.module.run.TargetExecutor.run(TargetExecutor.java:555)
at org.netbeans.core.execution.RunClassThread.run(RunClassThread.java:153)

设置为:引用服务器项目的服务器客户端项目(运行上述相同的客户端代码)以及客户端运行时的服务器项目调用运行。



编辑12/6/14:
尝试了教程,但我得到了:

 线程main中的异常java.lang.RuntimeException:javax.naming.NoInitialContextException:无法实例化类:com.sun.enterprise.naming.impl.SerialInitContextFactory [根异常是java .lang.ClassNotFoundException:com.sun.enterprise.naming.impl.SerialInitContextFactory]在test.java.application.TestJavaApplication.main(TestJavaApplication.java:28)

引起的:javax.nami ng.NoInitialContextException:无法实例类:com.sun.enterprise.naming.impl.SerialInitContextFactory [根异常是抛出java.lang.ClassNotFoundException:com.sun.enterprise.naming.impl.SerialInitContextFactory]在javax.naming中的
。 spi.NamingManager.getInitialContext(NamingManager.java:674)
在javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
在javax.naming.InitialContext.init(InitialContext.java:244)
at javax.naming.InitialContext。< init>(InitialContext.java:192)
at test.java.application.TestJavaApplication.main(TestJavaApplication.java:23)
导致: java.lang.ClassNotFoundException:com.sun.enterprise.naming.impl.SerialInitContextFactory $ b $ java.net.URLClassLoader $ 1.run(URLClassLoader.java:372)
at java.net.URLClassLoader $ 1.run (URLClassLoader.java:361)$ java.util.AccessController.doPrivileged(本地方法)
在java.net.URLClassLoader.findClass上
(URLClassLoader.java:360)$ b $ at java.lang .ClassLoade r.loadClass(ClassLoader.java:424)
at sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
在java.lang.Class.forName0(本地方法)$ b $在java.lang.Class.forName(Class.java:340)
在com.sun.naming.internal.VersionHelper12。 loadClass(VersionHelper12.java:72)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:61)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java: 672)
... 4 more
Java结果:1

事情是,它不是说它找不到名称或任何东西,而是说它无法找到某个类,我猜它是JNDI的一部分。这封信遵循了这些指示。我将发布我使用的所有代码。我只是想说明我要干净安装NetBeans和GlassFish。
链接: https://www.dropbox.com/sh/cp6jho1bmhqczpw/AAAH7eWVwWJAxa99kg9DL -qJa

解决方案

我已经实现了一个类似的独立客户端,使用NetBeans和GlassFish进行Java EE研讨会,以下代码:

  InitialContext ctx = new InitialContext(); 
GreetingBeanRemote ref =(GreetingBeanRemote)ctx.lookup(java:global / JWS2_JEE5_Server / JWS2_JEE5_Server-ejb / GreetingBean);
String result = ref.greet(Hello);

请注意JNDI名称,它使用格式 java:global /< app-name> /< module-name> /< bean-name> ,这可能会帮助您使用正确的名称,如果问题没有解决。在这方面尝试的另一件事是在 @Stateless 注释中使用 mappedName 属性(例如 @Stateless(mappedName =MySessionBean)),适用于GlassFish。



关于您的例外情况,它表明命名本身是不是问题,而是更多的是你部署的类路径或人工制品。我在Netbeans中的设置如下:


  • 包含独立客户端的客户端项目
  • 服务器项目,包含EJB实现

  • 包含EJB远程接口的Lib项目

  • 客户端项目正在引用lib项目

  • Lib项目引用了javaee-api jar



也许这些信息有助于正确设置它。






编辑14/07/11:

下面是关于如何使用Netbeans(8)创建上述场景的分步教程: b
$ b

要求:带有GlassFish 4的Netbeans EE版(8) JDK 1.7或以上


  1. 新项目/ Java EE /企业应用程序:MyEnterpriseApplication,创建EJB模块,Java EE 6,GlassFish 4新增项目/ Java / Java类库:MyJavaLibrary

  2. 新项目/ Java / Java应用程序:MyJavaApplic在MyJavaApplication:Properties / Libraries /添加项目中添加项目:MyJavaLibrary,从GlassFish安装库文件夹中添加JAR:gf-client.jar
  3. in
  4. MyEnterpriseApplication-ejb:新建SessionBeanHelloBean,创建远程接口,选择项目MyJavaLibrary
    < >在MyEnterpriseApplication-ejb中:编辑HelloBean,在MyJavaApplication中添加[2]
  5. :编辑MyJavaApplication,在主要
    运行MyEnterpriseApplication - >检查GF控制台输出中的正确部署

  6. 运行MyJavaApplication - >在控制台输出中查看成功调用

[1]

 字符串sayHello(String name); 

[2]

  @Override 
public String sayHello(String name){
returnHello+ name;
}

[3]

  try {
InitialContext context = new InitialContext();
HelloBeanRemote helloBean =
(HelloBeanRemote)context.lookup(java:global / MyEnterpriseApplication / MyEnterpriseApplication-ejb / HelloBean);
System.out.println(helloBean.sayHello(Geziefer));
} catch(NamingException ex){
}


I'm having a problem with the JNDI section of a stand-alone test application. I have the gf-client.jar and the jndi-properties.jar file on the class path. I made sure that the latest version was deployed on the glassfish server before running the client as well.

Stand-Alone Application:

public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws NamingException {
    Context context = new InitialContext();
    TestBeanRemote bean = (TestBeanRemote) context.lookup("java:global/server/TestBean");
    System.out.println(bean.sayHello("Sarah"));
}

}

EJB 3.1 Application:

@Stateless(name = "java:global/server/TestBean")
public class TestBean implements TestBeanLocal, TestBeanRemote {

@Override
public String sayHello(String name) {
    return "Hello " + name + "!";
}
}


@Remote
public interface TestBeanRemote {

String sayHello(String name);
}

Exception:

    Exception in thread "main" javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.enterprise.naming.impl.SerialInitContextFactory [Root exception is java.lang.ClassNotFoundException: com.sun.enterprise.naming.impl.SerialInitContextFactory]
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:674)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
    at javax.naming.InitialContext.init(InitialContext.java:244)
    at javax.naming.InitialContext.<init>(InitialContext.java:192)
    at com.gmail.physicistsarah.servertestclient.core.Main.main(Main.java:23)
Caused by: java.lang.ClassNotFoundException: com.sun.enterprise.naming.impl.SerialInitContextFactory
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:340)
    at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:72)
    at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:61)
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:672)
    ... 4 more
    Java Result: 1

EDIT: Well it seems that it actually breaks on the very first line when the initial context is declared. I'm following the instructions on https://glassfish.java.net/javaee5/ejb/EJB_FAQ.html and, according to it, no arguments are required on the call to the constructor of InitialContext so this behavior is mysterious to me. I'm using the NetBeans 8 ide and I have the files gf-client.jar and jndi-properties.jar appended to the project using the project/properties/libraries tab. NOTE: these jars are appended to the stand alone client only.

EDIT2: I created a new client project referencing the server project in it's properites, and now I'm getting a new exception when run:

  C:\Users\PC\Documents\NetBeansProjects\Server Client\nbproject\build-impl.xml:990: C:\Users\PC\GlassFish_Server\glassfish\domains\Server\generated\xml\Server Client does not exist.
at org.apache.tools.ant.types.AbstractFileSet.getDirectoryScanner(AbstractFileSet.java:484)
at org.apache.tools.ant.taskdefs.Copy.execute(Copy.java:487)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
at sun.reflect.GeneratedMethodAccessor197.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:435)
at org.apache.tools.ant.Target.performTasks(Target.java:456)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1248)
at org.apache.tools.ant.module.bridge.impl.BridgeImpl.run(BridgeImpl.java:286)
at org.apache.tools.ant.module.run.TargetExecutor.run(TargetExecutor.java:555)
at org.netbeans.core.execution.RunClassThread.run(RunClassThread.java:153)

The setup is: the server client project referencing the server project (running same client code above) and the server project which is running at the time of the client's call to run.

EDIT 12/6/14: Tried the tutorial, but I got:

Exception in thread "main" java.lang.RuntimeException: javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.enterprise.naming.impl.SerialInitContextFactory [Root exception is java.lang.ClassNotFoundException: com.sun.enterprise.naming.impl.SerialInitContextFactory]
at test.java.application.TestJavaApplication.main(TestJavaApplication.java:28)
Caused by: javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.enterprise.naming.impl.SerialInitContextFactory [Root exception is java.lang.ClassNotFoundException: com.sun.enterprise.naming.impl.SerialInitContextFactory]
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:674)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.init(InitialContext.java:244)
at javax.naming.InitialContext.<init>(InitialContext.java:192)
at test.java.application.TestJavaApplication.main(TestJavaApplication.java:23)
Caused by: java.lang.ClassNotFoundException: com.sun.enterprise.naming.impl.SerialInitContextFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:340)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:72)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:61)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:672)
... 4 more
Java Result: 1

The strange thing is that it's not saying that it couldn't find the name or anything, it's saying that it can't find a certain class, which I guess is a part of JNDI. The instructions were followed to the letter. I'm going to post all the code that I used. I'm just about at the point that I'm going to do a clean install of NetBeans and GlassFish. Link: https://www.dropbox.com/sh/cp6jho1bmhqczpw/AAAH7eWVwWJAxa99kg9DL-qJa

解决方案

I have implemented a similar standalone client using Netbeans and GlassFish for a Java EE workshop and used the following code for it:

    InitialContext ctx = new InitialContext();
    GreetingBeanRemote ref = (GreetingBeanRemote) ctx.lookup("java:global/JWS2_JEE5_Server/JWS2_JEE5_Server-ejb/GreetingBean");
    String result = ref.greet("Hello");

Note the JNDI name, which uses the format java:global/<app-name>/<module-name>/<bean-name>, which might help you using the correct name, if that ist the problem. Another thing to try in this regard is using the mappedName attribute int the @Stateless annotation (e.g. @Stateless(mappedName="MySessionBean")), which works for GlassFish.

Concerning your exception, it suggests that the naming itself is not the problem, but more the classpath or the artefact you deployed. My setup in Netbeans is as follows:

  • Client project, containing the standalone client
  • Server project, containing the EJB implementation
  • Lib project, containing the EJB remote interface
  • Client project is referencing the lib project
  • Lib project is referencing the javaee-api jar

Maybe the information helps in setting it up correctly.


EDIT 14/07/11:

Here's a step-by-step tutorial on how to create the above scenario with Netbeans (8):

Requirements: Netbeans EE edition (8) with GlassFish 4 installed, JDK 1.7 or above

  1. New project/Java EE/Enterprise Application: "MyEnterpriseApplication", create EJB module, Java EE 6, GlassFish 4
  2. New project/Java/Java Class Library: "MyJavaLibrary"
  3. New project/Java/Java Application: "MyJavaApplication"
  4. in MyJavaApplication: Properties/Libraries/Add project: MyJavaLibrary, Add JAR: gf-client.jar from GlassFish installation lib folder
  5. in MyEnterpriseApplication-ejb: New SessionBean "HelloBean", create remote interface, select project MyJavaLibrary
  6. in MyJavaLibrary: edit "HelloBeanRemote", add [1]
  7. in MyEnterpriseApplication-ejb:edit "HelloBean", add [2]
  8. in MyJavaApplication: edit "MyJavaApplication", add [3] in main
  9. run MyEnterpriseApplication -> check correct deployment in GF console output
  10. run MyJavaApplication -> see successful call in console output

[1]

String sayHello(String name);

[2]

@Override
public String sayHello(String name) {
   return "Hello " + name;
}

[3]

try {
    InitialContext context = new InitialContext();           
    HelloBeanRemote helloBean =
        (HelloBeanRemote) context.lookup("java:global/MyEnterpriseApplication/MyEnterpriseApplication-ejb/HelloBean");
    System.out.println(helloBean.sayHello("Geziefer"));
} catch (NamingException ex) {          
}

这篇关于使用GlassFish 4进行JNDI独立应用程序的EJB 3.1问题4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆