原因很简单;相应的 EMF 模型尚未在工作区中注册。为此,必须在加载模型之前执行以下代码:
import org.eclipse.e4.ui.model.application.impl.ApplicationPackageImpl;
ApplicationPackageImpl.init();
下面是一个完整的代码示例,用于在独立的 main 方法中加载 Eclipse 4 应用程序模型:
import org.eclipse.e4.ui.internal.workbench.E4XMIResourceFactory;
import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.impl.ApplicationPackageImpl;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
public class LoadAppModel {
private static ResourceSet resourceSet = new ResourceSetImpl();
public static void main(String[] args) {
ApplicationPackageImpl.init();
URI uri = URI
.createURI("file:///Users/marco/git/pharmacy_at/at.medevit.ecrit.pharmacy_at.application/Application.e4xmi");
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
.put("e4xmi", new E4XMIResourceFactory());
Resource res = resourceSet.getResource(uri, true);
MApplication app = (MApplication) res.getContents().get(0);
System.out.println(app.getElementId());
}
}