gpt4 book ai didi

java - 无法实例化类型 objectfactory

转载 作者:行者123 更新时间:2023-12-01 18:25:36 28 4
gpt4 key购买 nike

尝试实例化 ObjectFactory 时,我在 Eclipse 中收到以下编译错误:

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();}
}
}

如何解决此错误?

最佳答案

我的猜测是您导入了错误的 ObjectFactory。您可能想要由 xjc(JAXB 相关)生成的一个,而不是 javax.naming.spi(JNDI 的服务提供者接口(interface))生成的一个。

编辑

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

确保“生成的”包包含 ObjectFactory(带有 @XmlRegistry 注释的那个,而不是 javax.naming.spi.ObjectFactory 实现)或 jaxb.in​​dex 文件。

您可能可以从代码中删除 javax.naming.spi.ObjectFactory,除非您自己实现 JNDI 实现。

关于java - 无法实例化类型 objectfactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26306894/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com