試圖實例一個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