自作のプラグインなどに設定ファイルなどがあり、そのディレクトリへのパスを取得したい場合などが結構あります。たとえばLog4jの初期化のためにlog4j.xmlを読み込む、などですね。
そこで、あるプラグインのディレクトリへのパスを取得する方法です。Eclipse2.x系から3.1系、また3.2系といろいろやり方が変わっています。
String pluginDirectory = Platform .resolve(getDefault().getDescriptor().getInstallURL()).getPath();
で取得できます。 ちなみにLog4jの初期化などはAbstractUIPluginのサブクラス(勝手に作られるヤツ)で
public void startup() throws CoreException { super.startup(); String pluginDirectory = null; try { pluginDirectory = Platform .resolve(getDefault().getDescriptor().getInstallURL()) .getPath(); } catch (IOException e) { e.printStackTrace(); } DOMConfigurator.configure(new File(pluginDirectory, "lib/log4j.xml").toString()); }
などとstartup()をOverrideすればよいでしょうか。
Eclipse3.0から上のやり方は deprecated になりました。2.0で
String pluginDirectory = Platform .resolve(getDefault().getDescriptor().getInstallURL()).getPath();
だったのを3.0では
URL entry = getDefault().getBundle().getEntry("/"); String pluginDirectory = Platform.resolve(entry).getPath();
と書き換えます。
http://www3.vis.ne.jp/~asaki/p_diary/diary.cgi?Date=2004-09-05
Eclipse3.2からまたdeprecatedになっちゃいました。今度は、
URL entry = ClientPlugin.getDefault().getBundle().getEntry("/"); String pluginDirectory = FileLocator.resolve(entry).getPath();
みたいですね。FileLocator?は org.eclipse.core.runtime.FileLocator? みたいです。
この記事は
現在のアクセス:13215