Sunday, 13 March 2011

javax.naming.NoInitialContextException

[java] javax.naming.NoInitialContextException: Cannot instantiate class: weblogic.jndi.WLInitialContextFactory


This problem can be solved by adding weblogic.jar to the classpath
<?xml version="1.0" encoding="UTF-8"?>
<project name="ch03-lab01" default="usage" basedir=".">
<property environment="env" />
<property file="env.properties" />
<property name="app.name" value="ch03-lab01" />
<property name="config.dir" value="${basedir}/config" />
<property name="src.dir" value="${basedir}/src" />
<property name="build.dir" value="${basedir}/build" />

<taskdef name="wldeploy" classname="weblogic.ant.taskdefs.management.WLDeploy">
<classpath>
<pathelement location="${weblogic.home}/server/lib/weblogic.jar" />
</classpath>
</taskdef>

<path id="j2ee.classpath">
<pathelement location="${build.dir}" />
<fileset dir="${bea.home}/modules">
<include name="javax.ejb_3.0.1.jar" />
<include name="javax.annotation_1.1.jar" />
<include name="javax.persistence_1.0.0.0_1-0.jar" />
</fileset>
</path>

<target name="usage">
<echo message="build file for ${app.name}, available targets are:" />
<echo message="==================================================" />
<echo message="clean ---------> deletes build directory" />
<echo message="undeply ---------> deletes ear file from server" />
<echo message="all ---------> clean, complile, package and deploy the app" />
</target>

<target name="all">
<antcall target="usage" />
<antcall target="run-client" />
</target>


<target name="clean">
<delete dir="${build.dir}" />
<mkdir dir="${build.dir}" />
<mkdir dir="${build.dir}/client" />
<mkdir dir="${build.dir}/client/META-INF" />
</target>

<target name="compile" depends="clean">
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="j2ee.classpath" includeantruntime="false" />
</target>

<target name="package-ejb" depends="compile">
<jar jarfile="${build.dir}/BankService.jar">
<fileset dir="${build.dir}">
<include name="ejb30/session/**" />
<include name="ejb30/entity/**" />
</fileset>
<metainf dir="${config.dir}">
<include name="persistence.xml" />
</metainf>
</jar>
</target>

<target name="package-client" depends="compile">
<copy todir="${build.dir}/client">
<fileset dir="${config.dir}" includes="jndi.properties" />
</copy>
<jar jarfile="${build.dir}/Client.jar" manifest="${config.dir}/manifest.mf">
<fileset dir="${build.dir}/client" includes="**" />
<fileset dir="${build.dir}">
<include name="ejb30/client/Client.class" />
</fileset>
</jar>
</target>

<target name="package-ear" depends="package-ejb, package-client">
<jar destfile="${build.dir}/BankService.ear"
basedir="${build.dir}"
includes="BankService.jar"/>
</target>


<target name="deploy" depends="package-ear">
<wldeploy user="${admin.username}" password="${admin.password}"
adminurl="t3://${weblogic.hostname}:${weblogic.port}"
debug="true" action="deploy" name="BankService"
source="${build.dir}/BankService.ear" failonerror="${failondeploy}" />
</target>

<target name="undeploy">
<wldeploy user="${admin.username}" password="${admin.password}"
adminurl="t3://${weblogic.hostname}:${weblogic.port}"
debug="true" action="undeploy"
name="BankService" failonerror="${failondeploy}" />
</target>

<target name="run-client" depends="deploy">
<java classname="ejb30.client.Client" fork="yes">
<arg line="3 Belfast Northern"/>
<classpath>
<pathelement location="${build.dir}/Client.jar" />
<pathelement location="${weblogic.home}/server/lib/weblogic.jar" />
</classpath>
</java>
</target>
</project>

No comments:

Post a Comment