實際上獲得外掛程式的“安裝目錄”這樣的說法有點不對,因為Eclipse下的外掛程式全部都安裝在Plugins下面。
但是,多數人只要一想到要找一個外掛程式的位置,那就會搜尋“Eclipse 外掛程式 目錄”,所以題目我也就這麼寫了。
作為一個菜鳥,如果我要用一個之前沒有試過的東西我一般都會先建立一個很小的工程測試這個方法是事正確。
所以我就在Eclipse下面建立了一個Java工程(注意,這是禍根,後面你會明白)。
然後加了一個具有main方法的java檔案,並把
org.eclipse.core.runtime(Platform的方法都在這裡面)
org.eclipse.equinox.common(org.eclipse.core.runtime.IStatus在這裡面)
org.eclipse.osgi
等JAR引進來
然後使用
Bundle bundle = Platform.getBundle("com.core.runtime");if(bundle != null)System.out.println(bundle.toString());else System.out.println("return null!");
這段代碼來獲得外掛程式的目錄,結果死活都是null。
找啊找的,後來找到eclipse的論壇上去了,上面居然有一個和我一樣問題的人,然後才明白了。
要讓這個Platform.getBundle起作用,osgi這個架構一定要運行起來。p這是原地址:點擊開啟連結
當然了,外掛程式的作用是擴充Eclipse平台,如果你是一個Java程式(不是Eclipse外掛程式),就應該沒有“外掛程式”這一類的資源可言了吧,都已經編譯成了class的了。
意思就是Platform.getBundle這個方法只能在外掛程式開發中使用,而我一開始時建立的是Java工程,所以一直得到null。
初級看法,有誤請修正。
On 01/13/2011 09:57 AM, Tom Brandenburg wrote:> And is there a way to access resources in a plugin with a workspace> project and execution as a Java application?I'm not sure what you mean. Resources (as appropriate) can still be kept with classes and loaded via the standard java loading mechanism, like getClass().getResourceAsStream(*).Running as a "java app" means running a bunch of jars. SWT and JFace can be run as a bunch of java apps (OSGI bundles are valid java jars), but thing that depend on core.runtime and Platform cannot exist without running the OSGi framework.What exactly are you trying to do (or how did you get in this scenario)? If you are writing a plugin, simply use the OSGi Bundle.getEntry(*) mechanism. Why do you think you need to run your bundle as a java app?PW