关注
我是用JB新建的EJBClient来测试的,这种情况是要写环境文件吧

解决方案 »

  1.   

    源码如下:
    import FundManagerBean.*;
    import javax.naming.*;
    import javax.rmi.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.text.*;
    import java.util.*;
    import javax.ejb.*;
    import java.rmi.*;public class FundManagerClient extends JFrame implements ActionListener
    {
    double balance = 0;
    JTextField amount = new JTextField(10);
    JButton addFunds = new JButton("Add Funds");
    JButton withdrawFunds = new JButton("Withdraw Funds");
    String msg = "Current account balance: ";
    String strBal = "0";
    JLabel status;
    FundManager manager;
    NumberFormat currencyFormatter;

    public FundManagerClient()
    {
    super("Fund Manager");
    }

    public static void main(String[] args)
    {
    new FundManagerClient().init();
    }

    public void init()
    {
    buildGUI();

    //匿名类
    addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent evt)
    {
    System.exit(0);
    }
    });

    addFunds.addActionListener(this);
    withdrawFunds.addActionListener(this);

    createFundManager();
    try
    {
    currencyFormatter = NumberFormat.getCurrencyInstance();
    String currencyOut = currencyFormatter.format(0);
    status.setText(msg + currencyOut);
    }
    catch(Exception re)
    {
    re.printStackTrace();
    }
    pack();
    show();
    }

    public void buildGUI()
    {
    GridBagLayout gl = new GridBagLayout();
    GridBagConstraints gc = new GridBagConstraints();
    Container f_container = getContentPane();

    f_container.setLayout(gl);
    gc.fill = GridBagConstraints.BOTH;
    JLabel f_label = new JLabel("Enter Amount");
    gl.setConstraints(f_label, gc);
    f_container.add(f_label);

    gc.gridwidth = GridBagConstraints.REMAINDER;
    gl.setConstraints(amount, gc);
    f_container.add(amount);

    gl.setConstraints(addFunds, gc);
    f_container.add(addFunds);
    gl.setConstraints(withdrawFunds, gc);
    f_container.add(withdrawFunds);

    status = new JLabel(msg);
    gl.setConstraints(status, gc);
    f_container.add(status);
    }

    public void actionPerformed(ActionEvent e)
    {
    String str = amount.getText();
    if (str.equals(""))
    return;
    try
    {
    if (e.getSource() == addFunds)
    {
    balance = (double)manager.addFunds(balance,
    Double.parseDouble(amount.getText()));
    currencyFormatter = NumberFormat.getCurrencyInstance();
    strBal = currencyFormatter.format(balance);
    status.setText(msg + strBal);
    }
    else if (e.getSource() == withdrawFunds)
    {
    balance = (double)manager.withdrawFunds(balance,
    Double.parseDouble(amount.getText()));
    currencyFormatter = NumberFormat.getCurrencyInstance();
    strBal = currencyFormatter.format(balance);
    status.setText(msg + strBal);
    }
    }
    catch(Exception re)
    {
    re.printStackTrace();
    }
    }

    private static InitialContext ctx;

    private static InitialContext getWebSphareContextInfo()
    {
    InitialContext ctx = null;
    try
    {
    Properties prop = new Properties();
    prop.put(Context.INITIAL_CONTEXT_FACTORY, 
    // "com.ibm.ejs.ns.jndi.CNInitialContextFactory");
    "com.ibm.websphere.naming.WsnInitialContextFactory");
    prop.put(Context.PROVIDER_URL, "iiop://localhost:2809");

    ctx = new InitialContext(prop);
    }
    catch(NamingException ne)
    {
    System.out.println(ne);
    System.exit(0);
    }
    catch(Exception e)
    {
    System.out.println(e);
    System.exit(0);
    }
    return ctx;
    }

    public void createFundManager()
    {
    try
    {
    InitialContext ctx = getWebSphareContextInfo();
    Object objref = ctx.lookup("FundManagerHome");

    FundManagerHome home = (FundManagerHome)PortableRemoteObject.narrow(
    objref, FundManagerHome.class);
    manager = home.create();
    }
    catch(NamingException ne)
    {
    System.out.println("Naming Exception caught: " + ne);
    System.exit(0);
    }
    catch(CreateException ce)
    {
    System.out.println("Create Exception caught: " + ce);
    System.exit(0);
    }
    catch(RemoteException re)
    {
    System.out.println("Remote Exception caught: " + re);
    System.exit(0);
    }
    catch(Exception e)
    {
    System.out.println("Exception caught: " + e);
    System.exit(0);
    }
    }
    }
      

  2.   


    Object objref = ctx.lookup("FundManagerHome");
    这1句抛出的异常
      

  3.   

    prop.put(Context.PROVIDER_URL, "iiop://localhost:2809");一句改用t3试一试:prop.put(Context.PROVIDER_URL, "t3://localhost:2809");服务亲听端口应该为 2809
      

  4.   


    Object objref = ctx.lookup("FundManagerHome");
    这1句抛出的异常假如你的Bean的名字叫FundMangerBean,那么上句话改成
    Object objref = ctx.lookup("FundManagerBean");
    试试