3

I have created a simple Enterprise Application in Netbeans, which works as expected inside Netbeans. The Enterprise Application Client contains the following code:

package clienttest2;

import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import test.TestEJBRemote;

/**
 *
 * @author 
 */
public class Main {
private TestEJBRemote testejb;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws NamingException {
        Properties props = new 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", "computer");
        props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
        InitialContext ctx = new InitialContext(props);
        TestEJBRemote t = (TestEJBRemote) ctx.lookup("java:global/EJBTest/EJBTest-ejb/TestEJB");
        System.out.println(t.getName("Ian"));

        //Context ctx = null;
        //ctx=new InitialContext();
        //TestEJBRemote t = (TestEJBRemote) ctx.lookup("java:global/EJBTest/EJBTest-ejb/TestEJB");
        //System.out.println(t.getName("Ian"));
    }

}

I have tried the following:

java -jar ClientTest.jar

The error I get is: Cannot instantiate class: com.sun.enterprise.naming.SerialInitContextFactory

I have a few questions:

I have JRE 1.8 installed on my PC. Does this mean that I can run all types of Java apps i.e. Java EE and Java SE?

w0051977
  • 15,099
  • 32
  • 152
  • 329

2 Answers2

1

Simply speaking: your code contains quite a list of class names that will all be loaded using reflection. Surprise: all of them need to be in your classpath.

Most likely, your Netbeans project setup lists all the required JARs (probably as they belong to the set of JARs coming with your Java installation).

But when you call java on the command line, you more or less start with a classpath that only contains the "core" java JARs.

In other words: you have to identify those JARs that contain the classes your code lists; and then you have to provide them within your "class path setup" when invoking java on the command line.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Does the JRE contain everything needed to run Java SE and Java EE apps? Thanks. +1 for classpath reference. – w0051977 Sep 30 '16 at 10:36
0

Maybe a duplicate How to setup JNDI for Glassfish 3.1.2 for a stand-alone client?

We can suppose that Glassfish and NetBean share some libs for Java EE.

Anyway you can use http://jarscan.com to scan your netbean directory to find the jar that contains the class you need. And add it to you java command line.

Community
  • 1
  • 1
Mr_Thorynque
  • 1,749
  • 1
  • 20
  • 31