'Error : Need to specify class name in environment or system property

I'm new in development of Java EE applications and I'm really confused about this. I've found so many answers but I can't resolve my problem.

This is the error:

javax.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

And this is my code :

public class Test {

    public static void main(String[] args) {
        List<Auto_2G> auto2 = new ArrayList<Auto_2G>();
        GestionCellRef2GRemote t = null;
        Context contxt=null ;
        //GestionCellRefDualmodeRemote p = null;
        //Auto_Dualmode autodualmode = new Auto_Dualmode();
        FileInputStream file=null;
        HSSFWorkbook classeur=null ;
        HSSFSheet feuille=null;
        Row roww=null;

        try {
            try {
                contxt = new InitialContext();
            } catch(NamingException e) {

            }
            t = (GestionCellRef2GRemote) contxt.lookup("/webpfe/GestionCellRef2G!tn.orange.pfe.services.GestionCellRef2GR emote");
            file= new FileInputStream(new File("test.xls"));
            classeur = new HSSFWorkbook(file);
            feuille = classeur.getSheetAt(0);
            int i=1;

            while(roww!=null) {
                roww=feuille.getRow(i);

                for (Auto_2G auto2g:auto2) {
                    auto2g.setBSC_Name(roww.getCell(0).getStringCellValue());
                    auto2g.setBTS_NAME(roww.getCell(2).getStringCellValue());
                    auto2g.setCELL_NAME(roww.getCell(4).getStringCellValue());

                    t.ajout2G(auto2g);
                }
                i++;
            }
            file.close();
        } catch (Exception e) {
            System.out.println("erreur"+e);
        }

        for(Auto_2G auto2g:auto2){
        System.out.println(auto2g.getId());
    }

And this is my JNDI.properties file:

java.naming.factory.url.pkgs=org.jboss.ejb.client.naming
java.naming.factory.initial=org.jboss.naming.remote.client.InitialContextFactory
java.naming.provider.url=http-remoting://localhost:8181
jboss.naming.client.ejb.context=true
jboss.naming.client.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT=false

log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
javax.naming.NamingException: Failed to create remoting connection [Root exception is java.lang.IllegalArgumentException: XNIO001001: No XNIO provider found]
    at org.jboss.naming.remote.client.ClientUtil.namingException(ClientUtil.java:51)
    at org.jboss.naming.remote.client.InitialContextFactory.getInitialContext(InitialContextFactory.java:152)
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:684)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
    at javax.naming.InitialContext.init(InitialContext.java:244)
    at javax.naming.InitialContext.<init>(InitialContext.java:216)
    at tn.orange.pfe.test.Test.main(Test.java:41)
Caused by: java.lang.IllegalArgumentException: XNIO001001: No XNIO provider found
    at org.xnio.Xnio.doGetInstance(Xnio.java:226)
    at org.xnio.Xnio.getInstance(Xnio.java:192)
    at org.jboss.naming.remote.client.EndpointCache.get(EndpointCache.java:47)
    at org.jboss.naming.remote.client.InitialContextFactory.createEndpoint(InitialContextFactory.java:226)
    at org.jboss.naming.remote.client.InitialContextFactory.getOrCreateEndpoint(InitialContextFactory.java:207)
    at org.jboss.naming.remote.client.InitialContextFactory.getOrCreateNamingStore(InitialContextFactory.java:170)
    at org.jboss.naming.remote.client.InitialContextFactory.getInitialContext(InitialContextFactory.java:146)
    ... 5 more
java.lang.NullPointerException
    at tn.orange.pfe.test.Test.main(Test.java:46)
erreurjava.lang.NullPointerException


Solution 1:[1]

This is the code after the update :

  public class Test {

    public static void main(String[] args) {
        List<Auto_2G> auto2 = new ArrayList<Auto_2G>();
        GestionCellRef2GRemote t = null;
        Context contxt=null ;
        //GestionCellRefDualmodeRemote p = null;
        //Auto_Dualmode autodualmode = new Auto_Dualmode();
        FileInputStream file=null;
        HSSFWorkbook classeur=null ;
        HSSFSheet feuille=null;
        Row roww=null;
        Properties prop = new Properties(); 
        prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
        prop.put(Context.PROVIDER_URL, "http-remoting://localhost:8181");
         prop.put("jboss.naming.client.ejb.context", true);
         prop.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
try {

    try{
        contxt = new InitialContext(prop);
    }
    catch (NamingException e)
    {e.printStackTrace();}      

    t = (GestionCellRef2GRemote) contxt.lookup("/GestionCellRef2G!tn.orange.pfe.services.GestionCellRef2GRemote");
    file= new FileInputStream(new File("test.xls"));
    classeur = new HSSFWorkbook(file);
 feuille = classeur.getSheetAt(0);
 int i=1;

 while(roww!=null){
         roww=feuille.getRow(i);
         for(Auto_2G auto2g:auto2){
    auto2g.setBSC_Name(roww.getCell(0).getStringCellValue());
    auto2g.setBTS_NAME(roww.getCell(2).getStringCellValue());
    auto2g.setCELL_NAME(roww.getCell(4).getStringCellValue());
    auto2g.setLAC(Long.parseLong(roww.getCell(10).getStringCellValue()));
    auto2g.setCell_CI(Long.parseLong(roww.getCell(11).getStringCellValue()));
    auto2g.setBCC(Integer.parseInt(roww.getCell(12).getStringCellValue()));
    auto2g.setNCC(Integer.parseInt(roww.getCell(13).getStringCellValue()));
    auto2g.setBand(roww.getCell(14).getStringCellValue());
    auto2g.setBCCH(Long.parseLong(roww.getCell(15).getStringCellValue()));
    //String a =(Integer.parseInt(roww.getCell(12).getStringCellValue()))*10+roww.getCell(13).getStringCellValue();
    //int b = Integer.parseInt(a);
    //auto2g.setBSIC_dec(b);
    //String y=(Integer.parseInt(roww.getCell(13).getStringCellValue()))*8+roww.getCell(12).getStringCellValue();
//  int x= Integer.parseInt(y);
    //auto2g.setBSIC_oct( x);
    auto2g.setNumber_of_2G_Neighbor_Cell_for_GSM_Cell(Integer.parseInt(roww.getCell(28).getStringCellValue()));
    auto2g.setNumber_of_3G_Neighbor_Cell_for_GSM_Cell(Integer.parseInt(roww.getCell(29).getStringCellValue()));
    auto2g.setNumber_of_GSM_TRX(Integer.parseInt(roww.getCell(17).getStringCellValue()));
    auto2g.setBramwidth(65);
    auto2g.setNo_BCCH_Freq_list(Float.parseFloat(roww.getCell(16).getStringCellValue()));
    t.ajout2G(auto2g);
         }

    i++;

    }


    file.close();

} catch (Exception e) {
System.out.println("erreur"+e);
e.printStackTrace();
}


for(Auto_2G auto2g:auto2){
    System.out.println(auto2g.getId());
}

            // Créer un objet classuer



            // Lire la première feuille de ce classuer


            // Créer un Itérateur sur la feuille
            //Iterator<Row> rowIterator = feuille.iterator();



        // insertion en BD

        // p.ajoutDm(autodualmode);

    }

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Evgenij Reznik