"Registered factory required" exception when loading a resource

I get the following exception:

java.lang.RuntimeException: Cannot create a resource for 'file:/home/my_conf.xml'; a registered resource factory is needed 

The explosion code looks like this: resource = resourceSet.....

  ResourceSet resourceSet = new ResourceSetImpl(); Resource resource = null; File f = new File(filename); URI uri = URI.createFileURI(f.getAbsolutePath()); if (!f.exists()) { throw new Exception(filename + " does not exist"); } else { resource = resourceSet.getResource(uri, true); mapPrepConfiguration = (MapPrepConfiguration) resource.getContents().get(0); } 

Does anyone have a clue?

+4
source share
1 answer

If you work offline, you will have to manually register the factories in your factory resource registry.
Add the following line after creating the resource set instance:

 resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xml", new XMLResourceFactoryImpl()); 

See http://wiki.eclipse.org/EMF-FAQ#How_do_I_use_EMF_in_standalone_applications_.28such_as_an_ordinary_main.29.3F

If the problem is not found, depending on your case, there are two possibilities:

  • If you use a static metamodel (the java implementation is generated from your ecore model), you only need to access the corresponding package instance to download it and register it in the global EMF package registry.

Package YourPackageInstance = YourPackage.eInstance;

  • If you use a dynamic metamodel (without generated Java code), you need to register it manually.
 resourceSet.getPackageRegistry().put(yourPackage.getNsURI(), yourPackage); 

With the previous code, you will have to pre-extract EPackage from your ecore model programmatically.

+7
source

All Articles