试图实例一个ObjectFactory当我得到在eclipse下面的编译错误:无法实例类型的ObjectFactory
cannot instantiate the type objectfactory
此编译错误是在下面的行抛出:
objectFactory = new ObjectFactory();//throws error: "Cannot instantiate the type ObjectFactory"
完整代码为如下呼叫类别:
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();}
}
}
如何解决此错误?
'javax.naming.spi.ObjectFactory'是一个接口,不是类。你需要实例化它的一个实现。 – azurefrog 2014-10-10 19:45:08
您使用的是什么JNDI实现?我希望它的API有一些方法来获得对象工厂。 – azurefrog 2014-10-10 19:51:43
你需要什么ObjectFactory(在你的示例中没有用法)?你确定要使用javax.naming.spi包中的那个,而不是xjc生成的吗? – Puce 2014-10-10 20:12:38