0

I am getting the following compilation error in eclipse when trying to instantiate an ObjectFactory:

cannot instantiate the type objectfactory

This compilation error is thrown at the following line:

objectFactory = new ObjectFactory();//throws error: "Cannot instantiate the type ObjectFactory"

The complete code for the calling class is as follows:

package maintest;

import java.io.File;
import javax.naming.spi.ObjectFactory;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class Main {

    private static JAXBContext context;
    private static ObjectFactory objectFactory;

    public static void main(String[] args) {
        try {setUp();} catch (Exception e) {e.printStackTrace();}
        unmarshal();
    }

    protected static void setUp() throws Exception {
        context = JAXBContext.newInstance("generated");
        objectFactory = new ObjectFactory();//throws error: "Cannot instantiate the type ObjectFactory"
    }

    public static <PurchaseOrderType> void unmarshal(){
        Unmarshaller unmarshaller;
        try {
            unmarshaller = context.createUnmarshaller();
            final Object object = unmarshaller.unmarshal(new File("src/test/samples/po.xml"));
        } catch (JAXBException e) {e.printStackTrace();}
    }
}

How can I resolve this error?

CodeMed
  • 9,527
  • 70
  • 212
  • 364
  • 1
    `javax.naming.spi.ObjectFactory` is an interface, not a class. You need to instantiate an implementation of it. – azurefrog Oct 10 '14 at 19:45
  • What JNDI implementation are you using? I would expect its API to have some way to get an object factory. – azurefrog Oct 10 '14 at 19:51
  • What do you need ObjectFactory for (there is no usage in your sample)? Are you sure you want to use the one from the javax.naming.spi package and not one generated by xjc? – Puce Oct 10 '14 at 20:12

2 Answers2

3

My guess is that you imported the wrong ObjectFactory. You probably wanted the one generated by xjc (JAXB related) not the one from javax.naming.spi (service provider interface of JNDI).

Edit

javax.xml.bind.JAXBException: "generated" doesnt contain ObjectFactory.class or jaxb.index

Make sure the "generated" package contains either a ObjectFactory (the one with the @XmlRegistry annotation, not a javax.naming.spi.ObjectFactory implementation) or a jaxb.index file.

You probably can remove javax.naming.spi.ObjectFactory from your code, unless you're implementing a JNDI implementation yourself.

lexicore
  • 42,748
  • 17
  • 132
  • 221
Puce
  • 37,247
  • 13
  • 80
  • 152
  • Did you generate the JAXB classes from an XSD? Then there should also be an ObjectFactory in the same package. Use that one. (But it's only needed if you need to create JAXBElements. Otherwise just remove the ObjectFactory object (not the class) from your code.) – Puce Oct 10 '14 at 20:21
  • If you're writing the JAXB classes yourself rather than generating them, then have a look at the following question: http://stackoverflow.com/questions/5780184/what-is-the-objectfactory-role-during-jaxb-unmarshalling – Puce Oct 10 '14 at 20:23
  • If you generated the classes with the maven-jaxb2-plugin, then Eclipse should see it out-of-the-box, AFAIK. Maybe just try to reimport the project (as Maven Project). – Puce Oct 10 '14 at 20:33
  • The solution was to replace "generated" with the fully qualified name of the package containing ObjectFactory.java. I am marking yours as the answer because yours was the closest to pointing me in the correct direction. Thank you. – CodeMed Oct 10 '14 at 22:29
1

Try the following

    import javax.naming.spi.ObjectFactory;
    import javax.naming.Context;
    import javax.naming.Name;
    import java.util.Hashtable;

    ObjectFactory objFactory = new ObjectFactory() {

    @Override
    public Object getObjectInstance(Object o, Name name, Context cntxt, Hashtable<?, ?> hshtbl) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
};
Sybren
  • 1,071
  • 3
  • 17
  • 51
  • I'm in Netbeans but I've added the imports needed for Name, Context and Hashtable in my answer. – Sybren Oct 10 '14 at 19:53