'Eclipse Marker Resource does not exist

for my custom editor I want to add a marker, but got an exception. The exception is Resource '/c/runtime-EclipseApplication/HelloWorld/hello.me' does not exist. But res is not null, any idea?

   public static IPath getPathFromEditorInput(IEditorInput input) {
      if (input instanceof ILocationProvider)
         return ((ILocationProvider) input).getPath(input);

      if (input instanceof IURIEditorInput) {
         URI uri = ((IURIEditorInput) input).getURI();
         if (uri != null) {
            IPath path = URIUtil.toPath(uri);
            if (path != null)
               return path;
         }
      }

      return null;
   }

   @Override
   protected void editorSaved() {
      IPath path = getPathFromEditorInput(getEditorInput());
      try {
         createMarker(ResourcesPlugin.getWorkspace().getRoot().getFile(path), 2);
      }
      catch (CoreException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      super.editorSaved();
   }

public static IMarker createMarker(IResource res, int line) throws CoreException {
      IMarker marker = null;
      // note: you use the id that is defined in your plugin.xml
      if (res != null) {
         marker = res.createMarker("com.mymarker");
         marker.setAttribute(IMarker.SEVERITY, 0);
         marker.setAttribute(IMarker.CHAR_START, 10);
         marker.setAttribute(IMarker.CHAR_END, 20);
         marker.setAttribute(IMarker.LOCATION, "Snake file");
         marker.setAttribute(IMarker.MESSAGE, "Syntax error");
      }
      return marker;

   }

Here is snippet from plugin.xml

<extension point="org.eclipse.core.resources.markers"  
            id="com.mymarker"  
            name="ME errors">  
        <super type="org.eclipse.core.resources.problemmarker" />  
        <super type="org.eclipse.core.resources.textmarker" />  
        <persistent value="true" />
    </extension>


Solution 1:[1]

ResourcesPlugin.getWorkspace().getRoot().getFile(path) requires a path which is relative to the workspace root, you are giving it an absolute file path.

Use getFileForLocation for a full path:

ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path)

Also note that if the IEditorInput is an instance of IFileEditorInput then you can get the IFile using IFileEditorInput.getFile()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1