Question

Why this piece of java code looks for a file in my Home directory? Why it's not in my eclipse workspace (/home/user/eclipse/workspace/)?

import org.eclipse.emf.common.util.URI;

URI uri = null;
try {
    uri = URI.createURI("../models/task.cm");
    Resource resource = resourceSet.getResource(uri, true);
...
Was it helpful?

Solution

getResource

Resource getResource(URI uri, boolean loadOnDemand)

Returns the resource resolved by the URI.

A resource set is expected to implement the following strategy in order to resolve the given URI to a resource. First it uses it's URI converter to normalize the URI and then to compare it with the normalized URI of each resource; if it finds a match, that resource becomes the result. Failing that, it delegates to allow the URI to be resolved elsewhere. For example, the package registry is used to resolve the namespace URI of a package to the static instance of that package. So the important point is that an arbitrary implementation may resolve the URI to any resource, not necessarily to one contained by this particular resource set. If the delegation step fails to provide a result, and if loadOnDemand is true, a resource is created and that resource becomes the result. If loadOnDemand is true and the result resource is not loaded, it will be loaded before it is returned.

Parameters:
    uri - the URI to resolve.
    loadOnDemand - whether to create and load the resource, if it doesn't 
    already exists. 
Returns:
    the resource resolved by the URI, or null if there isn't one and it's not 
    being demand loaded. 
Throws:
    java.lang.RuntimeException - if a resource can't be demand created. 
    WrappedException - if a problem occurs during demand load.

So, The resource is not found, and an arbitrary resource is created in your home directory.

OTHER TIPS

It should not look for file in workspace directory. It should look for file in parent (because path starts with ..) of your current working directory. Current working directory when you are running from eclipse is the directory of your project.

I guess that your project is under your home directory, so this is the reason.

Actually, it is. In Unix-based shell, ".." represents the directory above the currently working one. So, it is actually looking in the folder surrounding the Eclipse program, or whatever is running.

Home would be represented by "//home"

The current working directory is represented by the value of "."

The User's directory is represented by "~"

And finally, the root is represented by "/"

For example:

Macintosh-2:/ Administration$ cd ~ #!sends me from root to my user directory
Macintosh-2:~ Administration$ cd ../Administration #!sends me from my user directory, to the directory above it, then back down 
Macintosh-2:~ Administration$ cd Administration #!user directory doesn't exist inside my user directory, thus showing ".." represents the directory above the current working one.
-bash: cd: Administration: No such file or directory
Macintosh-2:~ Administration$ cd .. #!moves up a directory
Macintosh-2:Users Administration$ #!now working in surrounding directory.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top