eclipse rcp 开发如何得到当前工作空间的路径?
2个回答
2015-12-27 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517181
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
一、Rcp程序获取程序安装路径
[c-sharp] view plaincopy
String path = null;
Location location = Platform.getInstallLocation();
if (location != null) {
URL url = location.getURL();
path = url.getPath();
}
二、获取Plug-in中的资源的绝对路径
[c-sharp] view plaincopy
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.osgi.framework.Bundle;
public class TestGetFullPath {
//filePath 为资源的相对路径
public static String getFullPath(String pluginId, String filePath) {
if (pluginId == null || filePath == null) {
throw new IllegalArgumentException();
}
// if the bundle is not ready then there is no file
Bundle bundle = Platform.getBundle(pluginId);
if (!isReady(bundle)) {
return null;
}
// look for the file (this will check both the plugin and fragment
// folders
URL fullPathString = find(bundle, filePath);
if (fullPathString == null) {
try {
fullPathString = new URL(filePath);
} catch (MalformedURLException e) {
return null;
}
}
if (fullPathString == null) {
return null;
}
try {
fullPathString = FileLocator.toFileURL(fullPathString);
} catch (IOException e) {
e.printStackTrace();
}
return fullPathString.getPath();
}
public static boolean isReady(Bundle bundle) {
return bundle != null && isReady(bundle.getState());
}
public static boolean isReady(int bundleState) {
return (bundleState & (Bundle.RESOLVED | Bundle.STARTING
| Bundle.ACTIVE | Bundle.STOPPING)) != 0;
}
public static URL find(Bundle bundle, String path) {
if (bundle == null) {
return null;
}
return FileLocator.find(bundle, new Path(path), null);
}
}
[c-sharp] view plaincopy
String path = null;
Location location = Platform.getInstallLocation();
if (location != null) {
URL url = location.getURL();
path = url.getPath();
}
二、获取Plug-in中的资源的绝对路径
[c-sharp] view plaincopy
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.osgi.framework.Bundle;
public class TestGetFullPath {
//filePath 为资源的相对路径
public static String getFullPath(String pluginId, String filePath) {
if (pluginId == null || filePath == null) {
throw new IllegalArgumentException();
}
// if the bundle is not ready then there is no file
Bundle bundle = Platform.getBundle(pluginId);
if (!isReady(bundle)) {
return null;
}
// look for the file (this will check both the plugin and fragment
// folders
URL fullPathString = find(bundle, filePath);
if (fullPathString == null) {
try {
fullPathString = new URL(filePath);
} catch (MalformedURLException e) {
return null;
}
}
if (fullPathString == null) {
return null;
}
try {
fullPathString = FileLocator.toFileURL(fullPathString);
} catch (IOException e) {
e.printStackTrace();
}
return fullPathString.getPath();
}
public static boolean isReady(Bundle bundle) {
return bundle != null && isReady(bundle.getState());
}
public static boolean isReady(int bundleState) {
return (bundleState & (Bundle.RESOLVED | Bundle.STARTING
| Bundle.ACTIVE | Bundle.STOPPING)) != 0;
}
public static URL find(Bundle bundle, String path) {
if (bundle == null) {
return null;
}
return FileLocator.find(bundle, new Path(path), null);
}
}
展开全部
.IWorkspace workspace = ResourcesPlugin.getWorkspace();
WorkspaceRoot root = workspace.getRoot();
这两句是得到eclipse插件运行时产生的工作空间的根,再进一步root.getProjects()可以得到运行时工作空间下建立的所有工程;
Platform.getInstanceLocation().getURL().getPath();这句代码也可以得到runtimeworkspace;
WorkspaceRoot root = workspace.getRoot();
这两句是得到eclipse插件运行时产生的工作空间的根,再进一步root.getProjects()可以得到运行时工作空间下建立的所有工程;
Platform.getInstanceLocation().getURL().getPath();这句代码也可以得到runtimeworkspace;
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询