我刚开始学习EJB,我编写了一个简单的bean和客户端应用程序。以下是代码:
接口:
package ejb;
import javax.ejb.Remote;
@Remote
public interface MySessionRemote {
String getResult();
}
豆:
import javax.ejb.EJB;
import javax.ejb.Stateless;
@Stateless
public class MySession implements MySessionRemote {
// @EJB
//private static MySessionRemote mySession1;
@Override
public String getResult() {
return "Bean";
}
}
客户申请:
package entappclient;
import javax.naming.*;
import ejb.MySessionRemote;
import java.util.Properties;
import javax.ejb.EJB;
public class Main {
// @EJB private static MySessionRemote mySession;
public static void main(String[] args) {
// TODO code application logic here
new Main().Save();
}
public Context getContext() throws javax.naming.NamingException {
Properties props = new Properties();
props.setProperty("java.naming.factory.initial",
"com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty("java.naming.factory.url.pkgs",
"com.sun.enterprise.naming");
props.setProperty("java.naming.factory.state",
"com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
return new InitialContext(props);
}
public void Save () {
//System.out.println(mySession1.getResult());
try {
Context jndiContext = getContext();
System.out.println("0");
MySessionRemote ref = ySessionRemote)jndiContext.lookup("java:global/EntAppEJB- ejb/MySession!ejb.MySessionRemote");
System.out.println("1");
System.out.println(ref.getResult());
} catch (Exception e ) {
System.out.println("Save"+e);
}
}
}
当我在NetBeans 8下运行它时,一切正常。结果是:
0
1
豆
但是当我尝试使用:appclient - client path /EENTAppClient.jar从命令行运行它时,它不起作用。结果是:
0
Savejavax.naming.NamingException: Lookup failed for 'java:global/EntAppEJB-ejb/M
ySession!ejb.MySessionRemote' in SerialContext[myEnv={java.naming.factory.initia
l=com.sun.enterprise.naming.SerialInitContextFactory, org.omg.CORBA.ORBInitialHo
st=localhost, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.J
NDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Ro
ot exception is javax.naming.NamingException: ejb ref resolution error for remot
e business interfaceejb.MySessionRemote [Root exception is java.lang.ClassNotFou
ndException: ejb.MySessionRemote]]
任何人都可以帮我找出为什么我可以在NetBeans下运行它,但它不能从命令行运行。
祝你好运
的Marcin