views:

27

answers:

1

I am writing an Eclipse plugin where I want to read a file within the project and do something on it. For example the file is located under : Project testplugin and path : com/flow/FlowMain.java I want to programmatically read this file and add some code in it. What I trying is :

String base = Platform.getBundle(config.getPluginId()).getEntry("/").toString(); String relativeUri = "com/flow/FlowMain.java"; File f = new File(base+relativeUri);

This obviously fails because the value of "base+relativeUri" returns : entry://1079.fwk5184781/com/flow/FlowMain.java

So how do I go about getting the complete file path from within the plugin ?

A: 

'entry' is a protocol defined by equinox, so you can get the real path using org.eclipse.core.runtime.FileLocator.toFileURL(URL).

Kane
From the post suggested by Fabian, I am trying to get the root workspace and locating the actual file. I get an NPE doing this. The root workspace itself seems to be coming as null. I get NPE when I try System.out.println(resourceInRuntimeWorkspace.getName()); One thing to clarify that I am running this code "as an eclipse application"
Ved
Another problem I face is that the file resides in the project folder so the path should be "projectname"+/com/flow/FlowMain.java. Now how do I get the name of the current project and append it to the path ? Is there a way to get it from IWorkspaceRoot ?
Ved
one thing you must pay attention, the bundle instance obtained via Platform.getBundle(config.getPluginId()) is the bundle in your running target platform.For example, it's your plug-in project in your workspace. You can get the absolute path of the project folder when executing the code in eclipse. However it's a jar file in the target platform, say as the bundle 'org.eclipse.core.runtime'. When you trying to get its location, the jar will be extracted to osgi data folder.
Kane
try below code snippet, it helps you understand,URL base = Platform.getBundle("org.eclipse.equinox.p2.core").getEntry("OSGI-INF/eventBus.xml");System.out.println(base);System.out.println(FileLocator.toFileURL(base));
Kane