简体   繁体   中英

When developing eclipse plugin, can I get absolute path of jar files using “Add Jars”?

I'm writing an eclipse plugin. In the plugin, I wrote a function which can make a command string for me. The string contains "-cp jar_path". The jar path is configured from eclipse "add jars" button(In the property config of a project, "add jars" is under "java build path -> libraries".). I need to get the absolute path of jar files, but using "add jars" I can only get the relative path using following code. I know using "add external jars" is ok, but I must make "add jars" working too. Are there any approaches? Another question is: if you right click the added jar under "referenced libraries" and click "show in -> properties", the IDE can get the jar's absolute location. I think maybe IDE get it from workspace/.metadata/.plugins/com.eclipse.core.resources/.projects. But even if it's true, I still can't directly read these files because of the format can't be parsed by general reader.

IPackageFragmentRoot[] roots = JavaCore.create(project).getPackageFragmentRoots();
for(IPackageFragmentRoot root : roots)
{
    IClasspathEntry entry = root.getResolvedClasspathEntry();
    if(IClasspathEntry.CPE_LIBRARY == entry.getEntryKind())
    {    
        root.getRawClasspathEntry();
        String cp = entry.getPath().toOSString();
    }
}

If you have a workpace relative path you can convert it to the absolute location using something like:

IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

IPath rootPath = root.getLocation();

IPath fullPath = rootPath.append("/project/container/file");
solved by myself, thank you, greg-449!
String cp;
if (root.isArchive() && root.isExternal())
    cp = root.getResolvedClasspathEntry().getPath().toOSString();
else if (root.isArchive() && !root.isExternal())
    cp = root.getResource().getLocation().toString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM