您的位置:首页 > 其它

EJB_Client结合Weblogic

2015-06-04 14:08 417 查看
EJB客户端代码,服务端代码已经有博文了,已经部署到了weblogic上面

客户端代码建立的JAVA_PROJECT

package example.ejb;

import java.util.Properties;

import javax.naming.Context;

import javax.naming.InitialContext;

import org.luzhen.ejb.Ejb_Service;

public class TestWebLogicEJB {

public static void main(String[] args) throws Exception {

Properties properties=new Properties();

//指定InitialContext的实现类名称。这里使用weblogic作为服务器。所以相应的值为weblogic.jndi.WLInitialContextFactory。

properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");

//连接jndi服务器的url。这里weblogic运行在本机,并且端口号为7001。所以值设置为t3://localhost:7001。

properties.setProperty(Context.PROVIDER_URL,"t3://localhost:7001");

//通过jndi访问指定resource的凭证名称。简单理解为访问这个resource时的用户名。如果这个resource没有设定访问策略,则可以不设。

properties.setProperty(Context.SECURITY_PRINCIPAL,"weblogic");

//通过jndi访问指定resource时与凭证相对应的密码。简单理解为访问这个resource时与用户名相对应的密码。如果这个resource没有设定访问策略,则可以不设。

properties.setProperty(Context.SECURITY_CREDENTIALS,"luzhen123");

Context context = new InitialContext(properties);

Ejb_Service sessionejb = (Ejb_Service)context.lookup("HelloEJB#org.luzhen.ejb.Ejb_Service");

String s = sessionejb.saySomething("EJB");

System.out.println(s);

// SwapEJB swap = (SwapEJB)context.lookup("SwapEJBBean#example.ejb.SwapEJB");

// System.out.println(swap.invoke("test"));

}

}

此时运行java application会报错:

Exception in thread "main" java.lang.NoClassDefFoundError: weblogic/security/acl/UserInfo

at weblogic.jndi.WLInitialContextFactory.getInitialContext(WLInitialContextFactory.java:117)

at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:684)

at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)

at javax.naming.InitialContext.init(InitialContext.java:242)

at javax.naming.InitialContext.<init>(InitialContext.java:216)

at example.ejb.TestWebLogicEJB.main(TestWebLogicEJB.java:23)

Caused by: java.lang.ClassNotFoundException: weblogic.security.acl.UserInfo

at java.net.URLClassLoader$1.run(URLClassLoader.java:366)

at java.net.URLClassLoader$1.run(URLClassLoader.java:355)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(URLClassLoader.java:354)

at java.lang.ClassLoader.loadClass(ClassLoader.java:425)

at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)

at java.lang.ClassLoader.loadClass(ClassLoader.java:358)

... 6 more

经过查看错误得知:

于是查看官网文档解决方法如下:






Creating a wlfullclient.jar for JDK 1.7 client applications

Use the following steps to create a wlfullclient.jar file for a JDK 1.7 client application:

Change directories to the
server/lib
directory.
cd WL_HOME/server/lib  这里WL_HOME就是我们的D:\Oracle\Middleware\wlserver_12.1


Use the following command to create wlfullclient.jar in the
server/lib
directory:
java -jar wljarbuilder.jar


You can now copy and bundle the
wlfullclient.jar
along
with
cryptoj.jar
with
client applications. The
wlfullclient.jar
and
cryptoj.jar
must
be kept in the same directory as the
wlfullcient.jar
references
cryptoj.jar
in
its manifest Class-Path.

Add the
wlfullclient.jar
to
the client application's classpath.

打开CMD:
Microsoft Windows [版本 6.2.9200]

(c) 2012 Microsoft Corporation。保留所有权利。

C:\Users\lenovo>d:

D:\>cd D:\Oracle\Middleware\wlserver_12.1\server\lib

D:\Oracle\Middleware\wlserver_12.1\server\lib>java -jar wljarbuilder.jar
成功生成wlfullclient.jar 然后将该jar包 加入到EJB Client工程build path下 即可,运行效果如下:
控制台打印出了:EJB

注意:一定要切换盘符号到d:盘 因为我们的weblogic按照目录在d盘

否则如果不切换盘符的话 运行会报错 效果如下2种情况:
情况1:
Microsoft Windows [版本 6.2.9200]

(c) 2012 Microsoft Corporation。保留所有权利。

C:\Users\lenovo>cd D:\Oracle\Middleware\wlserver_12.1\server\lib

C:\Users\lenovo>java -jar wljarbuilder.jar
Error: Unable to access jarfile wljarbuilder.jar

情况2:
Microsoft Windows [版本 6.2.9200]

(c) 2012 Microsoft Corporation。保留所有权利。

C:\Users\lenovo>java -jar D:\Oracle\Middleware\wlserver_12.1\server\lib\wljarbui

lder.jar

This utility is used to build a single, more easily transported jar out of multi

ple jars

Usage: java com.bea.jarbuilder.JarBuilder [options]

where options include:

-usage get usage information for JarBuilder

-help get usage information for JarBuilder

JarBuilder must be run from the server/lib directory under WL_HOME

Exception in thread "main" java.lang.RuntimeException

at com.bea.jarbuilder.JarBuilder.showUsageError(JarBuilder.java:193)

at com.bea.jarbuilder.JarBuilder.processArgs(JarBuilder.java:134)

at com.bea.jarbuilder.JarBuilder.main(JarBuilder.java:49)

以上就是没有切换盘符导致的错误 注意这里

还有这里
要将服务端的EJB程序的Ejb_Service.java接口 打成jar包加入到客户端EJB的build
path中
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: