URL Provider- Websphere-无法通过java读取属性文件

时间:2011-12-14 19:17:21

标签: java url provider

我正在尝试在Webspehere上配置URL提供程序。我正在按照IBM网站提供的步骤进行操作:

1)在websphere admin中配置URL提供程序 2)更新了web.xml 3)更新了ibm-web-bnd.xm 4)读取在步骤1中设置的URL提供程序 5)加载属性文件

http://www.ibm.com/developerworks/websphere/library/techarticles/0502_botzum/0502_botzum.html

通过java代码读取URL提供程序时出错。

以下是代码段:

import java.net.URL
import java.net.MalformedURLException
import javax.naming.Context
import javax.naming.Intialcontext
import javax.naming.NamingException

 public class Test
 {
   public static void main ()
   {
     try
     {
       Context ctx = new IntialContext();
       String propertyFile = (String)ctx.lookup("java:comp/env/url/TestProjectWSURL");
       URL url = new URL(propertyFile);
       System.out.println(url.getPath());
     }
     catch (NamingException e)
     {
       e.printStackTrace();
     }
     catch (NamingException e)
     {
       e.printStackTrace();
     }
   }
 }

1 个答案:

答案 0 :(得分:0)

由于堆栈跟踪不可用,我的猜测是您根据以下代码获得了类转换异常

String propertyFile = (String)ctx.lookup("java:comp/env/url/TestProjectWSURL");

您应该将其强制转换为URL而不是String。以下是正确的方法(从您提供的链接)

InitialContext initCtx = new InitialContext();

URL url = (java.net.URL) initCtx.lookup(jndi);
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();