2025-02-10

1、在普通Java工程下使用以下代碼就行:

view sourceprint?1 String currentDir=System.getProperty("user.dir");

2、但是,在插件開發中,以上代碼得到的是Eclipse的安裝目錄,如果想得到我們的代碼所在的目錄,則需要

view sourceprint?01 import java.io.File; 

02 import java.io.IOException; 

03 import java.net.MalformedURLException; 

04 import java.net.URL; 

05 import java.security.CodeSource; 

06 import java.security.ProtectionDomain; 

07   

08 public class GetPath { 

09   

10     public static String getPathFromClass(Class cls) throws IOException { 

11         String path = null; 

12         if (cls == null) { 

13             throw new NullPointerException(); 

14         } 

15         URL url = getClassLocationURL(cls); 

16         if (url != null) { 

17             path = url.getPath(); 

18             if ("jar".equalsIgnoreCase(url.getProtocol())) { 

19                 try { 

20                     path = new URL(path).getPath(); 

21                 } catch (MalformedURLException e) { 

22                 } 

23                 int location = path.indexOf("!/"); 

24                 if (location != -1) { 

25                     path = path.substring(0, location); 

26                 } 

27             } 

28             File file = new File(path); 

29             path = file.getCanonicalPath(); 

30         } 

31         return path; 

32     } 

33   

34     private static URL getClassLocationURL(final Class cls) { 

35         if (cls == null) 

36             throw new IllegalArgumentException("null input: cls"); 

37         URL result = null; 

38         final String clsAsResource = cls.getName().replace('.', '/') 

39                 .concat(".class"); 

40         final ProtectionDomain pd = cls.getProtectionDomain(); 

41         if (pd != null) { 

42             final CodeSource cs = pd.getCodeSource(); 

43             if (cs != null) 

44                 result = cs.getLocation(); 

45             if (result != null) { 

46                 if ("file".equals(result.getProtocol())) { 

47                     try { 

48                         if (result.toExternalForm().endsWith(".jar") 

49                                 || result.toExternalForm().endsWith(".zip")) 

50                             result = new URL("jar:"

51                                     .concat(result.toExternalForm()) 

52                                     .concat("!/").concat(clsAsResource)); 

53                         else if (new File(result.getFile()).isDirectory()) 

54                             result = new URL(result, clsAsResource); 

55                     } catch (MalformedURLException ignore) { 

56                     } 

57                 } 

58             } 

59         } 

60         if (result == null) { 

61             final ClassLoader clsLoader = cls.getClassLoader(); 

62             result = clsLoader != null ? clsLoader.getResource(clsAsResource) 

63                     : ClassLoader.getSystemResource(clsAsResource); 

64         } 

65         return result; 

66     } 

67 }

以上代碼可以得到指定類的絕對地址,如果想得到工程地址,隻要把後面的字符串剪掉。

3、下面的代碼

view sourceprint?1 String packageName = this.getClass().getResource("").getPath(); 

2 packageName = packageName.replace("/", "\\");
可以得到所在類的地址,但是在插件開發中得到的並非是絕對地址。在插件開發中可以結合2和3的代碼得到當前工程的絕對地址: view sourceprint?01 String packageName = this.getClass().getResource("").getPath(); 

02 packageName = packageName.replace("/", "\\"); 

03 System.out.println("包名:"+packageName); 

04 String projectPath = null; 

05 try { 

06     String packageFullName = GetPath.getPathFromClass(this.getClass()); 

07     projectPath = packageFullName.substring(0, 

08             packageFullName.indexOf(packageName) + 1); 

09     System.out.println("工程路徑:"+projectPath); 

10 } catch (IOException e1) { 

11     projectPath = null; 

12     e1.printStackTrace(); 

13 }

作者“雙子座的博客”
 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *