Finding the path to a jar from a Class inside it?

后端 未结 2 1338
忘了有多久
忘了有多久 2020-12-10 23:27

Plugin x.y.z is supposed to run on top of a Java project and generate some Java-Code. This code will need classes available in the Plugin\'s jar at build and run time. Hence

2条回答
  •  旧巷少年郎
    2020-12-10 23:57

    We use this to find the location of a class:

    public static URL getLocation(final Class cls) {
      final ProtectionDomain pd = cls.getProtectionDomain();
      final CodeSource cs = pd.getCodeSource();
      return cs.getLocation();
    }
    

    Not sure where it came from.

提交回复
热议问题