[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2207
  • Last Modified:

Exceptionjavax.naming.NoInitialContextException: Need to specify class name in...

When i try to run my application, i get the error message Exceptionjavax.naming.NoInitialContextException: Need to specify class name in environment or system property,, or as an applet parameter, or in an application resource file: java.naming.factory.initial.

The line that throws the exception is Object ref = ctx.lookup("demo.CalculatorRemote");

Under project properties  - Run i have VM Options set to -Djava.security.auth.login.config=C:/glassfishv3/glassfish/lib/appclient/appclientlogin.conf

appclientlogin.conf contains


default {
      com.sun.enterprise.security.auth.login.ClientPasswordLoginModule required debug=false;
};
certificate {
      com.sun.enterprise.security.auth.login.ClientCertificateLoginModule required debug=false;
};

I am trying to follow the programmatic logon from a Stand-Alone Client located at: http://psecheresse.spaces.live.com/blog/cns!492816F77B21DC3D!178.entry

What am i doing wrong?
Source code:

package programmaticlogintest;

import com.sun.appserv.security.ProgrammaticLogin;
import javax.naming.InitialContext;
import demo.CalculatorRemote;
import javax.rmi.PortableRemoteObject;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        System.out.println("Hello World!");
        // TODO code application logic here
        ProgrammaticLogin pm = new ProgrammaticLogin();
        pm.login("endasil", "hej123"); //user and password of a user mapped to admin role

        InitialContext ctx;
        try {
            
            ctx = new InitialContext();            
            
            Object ref = ctx.lookup("demo.CalculatorRemote");
            javax.swing.JOptionPane.showMessageDialog(null, "Try2");
            CalculatorRemote calc = (CalculatorRemote) PortableRemoteObject.narrow(ref, CalculatorRemote.class);            
            javax.swing.JOptionPane.showMessageDialog(null, "2 + 2 = " + calc.add(2, 2));
            
          //  javax.swing.JOptionPane.showMessageDialog(null, calc.hello("Mr Demo"));
        } catch (Exception ex) {
                        
            javax.swing.JOptionPane.showMessageDialog(null, "Exception" +ex.toString());
        }
    }
}

Open in new window

0
endasil
Asked:
endasil
  • 12
  • 9
3 Solutions
 
mnrzCommented:
you need to init the InitialContext

http://java.sun.com/products/jndi/tutorial/beyond/env/context.html

/ Initial environment with various properties
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,  
    "com.sun.jndi.fscontext.FSContextFactory");

// Call the constructor
Context ctx = new InitialContext(env);

Open in new window

0
 
a_bCommented:
You need to specify the lookup properties when you create the initial context - new InitialContext.

Try -
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, <INITIAL_CONTEXT_FACTORY - Your lookup params here>);
 properties.put(Context.PROVIDER_URL, <PROVIDER_URL - Your lookup params here>);
context = new InitialContext(properties);
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
endasilAuthor Commented:
Thanks to both of you for responding.

Mnrz, when trying with your first code with env.put(Context.INITIAL_CONTEXT_FACTORY,  "com.sun.jndi.fscontext.FSContextFactory"); i get the exception:  Exceptionjava.security.AcessControlException: access denied (java.util.PropertuPermission user.dir read) exception thrown at ctx = new InitialContext(env);.

When adding env.put(Context.PROVIDER_URL, "localhost:1099"); from your link i get a exceptionjavax.naming.invalidnameexception: localhost: 1099 [root exception is java.netMalformedURLException: unknown protoctocol: localhost


a_b i don't understand what i should set as my lookup params, but i  appreciate that you are taking your time to assist me. I just started learning this.
0
 
mnrzCommented:
Well, I just wanted to show you the door, the second link I posted is telling the whole story about how to init the context.

Anyway, as an example look at following snippet code, you need to know the port for ORB in Glassfish it is usually 3700 but it depends on application server you are using.

Also if you are going to use InitialContxt as remote call you have to place ip address of the server instead of "localhost" and "127.0.0.1"



 Hashtable props = new Hashtable();
 props.put("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
 props.put("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
            props.put("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
            props.put("java.naming.provider.url", "localhost");
            props.put("org.omg.CORBA.ORBInitialHost", "127.0.0.1");
            props.put("org.omg.CORBA.ORBInitialPort", "3700");

Context ctx = new InitialContext(props);

Open in new window

0
 
endasilAuthor Commented:
Right, i read trough the document you posted, experimented a bit but did not get anywhere. Anything i try seem to throw an exception at InitialContext. Trying the example you give now give the error i posted in the code window below. "java.lang.ClassNotFoundException: org.glassfish.internal.api.Globals" I assume i need to add some class to my project for this to work, but how do i figure out what class from the information given?




Java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at com.sun.javaws.Launcher.executeApplication(Launcher.java:1799)
	at com.sun.javaws.Launcher.executeMainClass(Launcher.java:1745)
	at com.sun.javaws.Launcher.doLaunchApp(Launcher.java:1507)
	at com.sun.javaws.Launcher.run(Launcher.java:129)
	at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NoClassDefFoundError: org/glassfish/internal/api/Globals
	at com.sun.enterprise.naming.impl.SerialInitContextFactory.&lt;init&gt;(SerialInitContextFactory.java:74)
	at com.sun.enterprise.naming.SerialInitContextFactory.&lt;init&gt;(SerialInitContextFactory.java:58)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
	at java.lang.Class.newInstance0(Class.java:355)
	at java.lang.Class.newInstance(Class.java:308)
	at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:654)
	at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
	at javax.naming.InitialContext.init(InitialContext.java:223)
	at javax.naming.InitialContext.&lt;init&gt;(InitialContext.java:197)
	at programmaticlogintest.Main.main(Main.java:42)
	... 9 more
Caused by: java.lang.ClassNotFoundException: org.glassfish.internal.api.Globals
	at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
	at com.sun.jnlp.JNLPClassLoader.findClass(JNLPClassLoader.java:332)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
	... 22 more

Open in new window

0
 
mnrzCommented:
Yes. Which application server your trying to log in remotely? The example I posted is for Sun Glassfish App server, if yours is the same, you need to add some jar files to your library. These Jar files are in application server lib folder or something like that

I am not sure about the name of jar files, but I think their names contains "client" try to find them and add to your classpath
0
 
endasilAuthor Commented:
I have added every jar file from the glassfish directory with a name containing the word "client" but still get the same error. I added a list of all the libs that i have added to the project.





<jar href="ProgrammaticLoginTest.jar" main="true"/>
<jar href="lib/Login-ejb.jar"/>
<jar href="lib/gf-client.jar"/>
<jar href="lib/security.jar"/>
<jar href="lib/appserv-rt.jar"/>
<jar href="lib/javaee.jar"/>
<jar href="lib/common-util.jar"/>
<jar href="lib/fscontext.jar"/>
<jar href="lib/glassfish-naming.jar"/>
<jar href="lib/orb-connector.jar"/>
<jar href="lib/orb-enabler.jar"/>
<jar href="lib/orb-iiop.jar"/>
<jar href="lib/javax.persistence.jar"/>
<jar href="lib/javax.enterprise.deploy.jar"/>
<jar href="lib/javax.servlet.jsp.jstl.jar"/>
<jar href="lib/jaxb-api-osgi.jar"/>
<jar href="lib/javax.security.auth.message.jar"/>
<jar href="lib/javax.security.jacc.jar"/>
<jar href="lib/webservices-api-osgi.jar"/>
<jar href="lib/bean-validator.jar"/>
<jar href="lib/jsf-impl.jar"/>
<jar href="lib/javax.servlet.jsp.jar"/>
<jar href="lib/javax.management.j2ee.jar"/>
<jar href="lib/javax.resource.jar"/>
<jar href="lib/weld-osgi-bundle.jar"/>
<jar href="lib/javax.jms.jar"/>
<jar href="lib/jstl-impl.jar"/>
<jar href="lib/javax.servlet.jar"/>
<jar href="lib/javax.ejb.jar"/>
<jar href="lib/javax.annotation.jar"/>
<jar href="lib/mail.jar"/>
<jar href="lib/javax.transaction.jar"/>
<jar href="lib/jsr311-api.jar"/>
<jar href="lib/jsf-api.jar"/>
<jar href="lib/web-core.jar"/>
<jar href="lib/appclient-connector.jar"/>
<jar href="lib/appclient-server-core.jar"/>
<jar href="lib/appclient.security.jar"/>
<jar href="lib/deployment-client.jar"/>
<jar href="lib/gf-client-module.jar"/>
<jar href="lib/gf-client.jar"/>
<jar href="lib/monitoring-scripting-client-cli.jar"/>
<jar href="lib/monitoring-scripting-client.jar"/>
<jar href="lib/pkg-client.jar"/>

Open in new window

0
 
mnrzCommented:
"lib/appserv-rt.jar"
"lib/javaee.jar"

0
 
endasilAuthor Commented:
Thank you for the suggestion, but sadly they are both added added and the problem remains.
 Can't understand that it should be so hard to create a simple stand alone application communicating with a EJB...
0
 
endasilAuthor Commented:
Some more info that could be of use.

I am using glassfish 3.01, jdk 1.6.0.21 and netbeans 6.9.1 if there are any relevante differences between the version your using and i'm using when developing.

The error message only occurs in ctx = new InitialContext(env); if i have the glassfish-naming.jar added to the project. It will occur regardless if i have  InitialContext();  or  InitialContext(env);

If i do not have glassfish-naming.jar added to the project,   InitialContext()  will pass without error. Then  it will throw the .NoInitialContextException exception at ctx.lookup("demo.CalculatorRemote");

If i do not have glassfish-naming.jar it will complain about not finding SerialInitContextFactory (since it is located in glassfish-naming.jar)

I am very grateful and impressed that  you have not given up and keep trying to solve the problem.
0
 
mnrzCommented:
well I asked you what is your application server and which version?



have a look here:

https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html

it's said  for v3 you need a gf-clent.jar  
0
 
mnrzCommented:
Ok so go to above link it is all about glassfish... hope it helps
0
 
endasilAuthor Commented:
Yes i read that link tried it before i attempted the standalone client example. just like the standalone example it suggest that i should use InitialContext() without passing parameters. Doing that will cause the NoInitialContextException when trying to do a  lookup (regardless if i do ctx.lookup("java:global/Calculator/Login-ejb") orctx.lookup("demo.CalculatorRemote") ).

According to the responses here NoInitialContextException is caused by not passing in anything in InitialContext(), that i am explicitly told not to do in the documentation. And it takes me back to step 1.
0
 
mnrzCommented:
ok.... As it is said in this website you need following Jarfile and maybe all other jar files in that folder

$GLASSFISH_HOME/modules/gf-client.jar

but I suggest you find and add following jar files, these files might be placed in different folders other than LIB

appserv-rt.jar
appserv-deployment-client.jar
appserv-ext.jar

and please comment out the provider_url

Also remove other unnecessary jar files because it makes it hard to find proper jar files
0
 
endasilAuthor Commented:
appserv-deployment-client.jar and appserv-ext.jar seem to be  libs that only exist under glassfish2

So the following libs are included:
Login-ejb (to get access to the remote interface of my EJB)
appserv-rt
javaee
glassfish-naming.jar

Do you know what classes are used in the appserv-ext and appserv-deployment-client?

Attached the updated code below. (Error occurs at ctx = new InitialContext(env); as usual)

package programmaticlogintest;

import javax.naming.InitialContext;
import demo.CalculatorRemote;
import java.util.Hashtable;
import javax.naming.NamingException;

public class Main {   
    public static void main(String[] args) throws NamingException {               
        Hashtable env = new Hashtable();        
        env.put("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory"); 
        env.put("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
        env.put("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");        
        env.put("org.omg.CORBA.ORBInitialHost", "127.0.0.1");
        env.put("org.omg.CORBA.ORBInitialPort", "3700");
        InitialContext ctx;
        
        try {
             javax.swing.JOptionPane.showMessageDialog(null, "before InitialContext");
            ctx = new InitialContext(env);
            javax.swing.JOptionPane.showMessageDialog(null, "After initialContext");
            CalculatorRemote calc = (CalculatorRemote) ctx.lookup("java:global/Calculator/Login-ejb");
            javax.swing.JOptionPane.showMessageDialog(null, "After lookup");            

        } catch (Exception ex) {
            javax.swing.JOptionPane.showMessageDialog(null, "Exception" +ex.toString());
        }
    }
}

Open in new window

0
 
mnrzCommented:
This is really strange! First why you didn't add gf-client.jar? please add it as well

as last idea, I am not sure but It might be that it needs JAXB jar files, can you please after testing with gf-client.jar, search for JAXB jar files and add them to your classpath.

Also make sure your added jar files are loaded by JVM. I means copying is not enough you need to add new files in your IDE (Netbeans)

one more thing to do is that, all the jar files can be opened with ZIP softwares, if you have WinRAR, it has a facility to search for a specific file within all zip files... so you can search the missing class, "Globals.class", go to folder that all your jar files are placed and open one of them wih winRar or something and then using WinRAR go one level up and then press a find button (or anything like that) and search for file named Globals.class if it finds that file it will show you the name of jar file as well, this means you have it but it is not loaded so you need to tell your IDE to load it
also if you find it check the path, it should be the same path as the exception says:
org/glassfish/internal/api/

if you didn't find it then try to search under the glassfish installed folder, if you didn't find it again, try downloading the JAXB jar files, if it fails again, I suggest your go to Sun's forum (which is now Oracle of course :) ) and post a thread over
0
 
mnrzCommented:
as the last solution, this is Glassfish forum:

http://forums.java.net/jive/forum.jspa?forumID=56&start=0
0
 
mnrzCommented:
Please inform us if you managed to resolve the issue or not
Thanks
0
 
endasilAuthor Commented:
I was not able to solve it on glassfish3. Today i tried uninstalling glassfish3 and install glassfish 2 instead, and with that have no problem making this program work. So it seem to be something that is different with glassfish3...
0
 
mnrzCommented:
Good. Maybe the Oracle tries to embed their database into glassfish :) (just kidding)


0
 
endasilAuthor Commented:
I did not find a way to solve my issues on glassfish 3, had to abandon that platform and instead work on glassfish 2 and got it working there. Even if the issue was not solved completely  Mnrz provided me with alot of valuable informaiton and deserv some points for that.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 12
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now