Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #29943 > unrolled thread

Finding other classes in .jar from running program?

Started byKnute Johnson <nospam@rabbitbrush.frazmtn.com>
First post2014-09-11 18:15 -0700
Last post2014-09-14 01:48 +0200
Articles 5 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Finding other classes in .jar from running program? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2014-09-11 18:15 -0700
    Re: Finding other classes in .jar from running program? Arne Vajhøj <arne@vajhoej.dk> - 2014-09-11 21:28 -0400
      Re: Finding other classes in .jar from running program? Arne Vajhøj <arne@vajhoej.dk> - 2014-09-11 21:39 -0400
        Re: Finding other classes in .jar from running program? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2014-09-11 20:51 -0700
        Re: Finding other classes in .jar from running program? Jan Burse <janburse@fastmail.fm> - 2014-09-14 01:48 +0200

#29943 — Finding other classes in .jar from running program?

FromKnute Johnson <nospam@rabbitbrush.frazmtn.com>
Date2014-09-11 18:15 -0700
SubjectFinding other classes in .jar from running program?
Message-ID<luthf0$7dl$1@dont-email.me>
Is there a way given a program running from a .jar file to find the 
other classes in the .jar file?

Thanks,

knute...

[toc] | [next] | [standalone]


#29944

FromArne Vajhøj <arne@vajhoej.dk>
Date2014-09-11 21:28 -0400
Message-ID<54124c28$0$293$14726298@news.sunsite.dk>
In reply to#29943
On 9/11/2014 9:15 PM, Knute Johnson wrote:
> Is there a way given a program running from a .jar file to find the
> other classes in the .jar file?

You can ask where a given class is loaded from and if it is a jar
file then you can list the content of the jar file.

Arne

[toc] | [prev] | [next] | [standalone]


#29945

FromArne Vajhøj <arne@vajhoej.dk>
Date2014-09-11 21:39 -0400
Message-ID<54124ec4$0$295$14726298@news.sunsite.dk>
In reply to#29944
On 9/11/2014 9:28 PM, Arne Vajhøj wrote:
> On 9/11/2014 9:15 PM, Knute Johnson wrote:
>> Is there a way given a program running from a .jar file to find the
>> other classes in the .jar file?
>
> You can ask where a given class is loaded from and if it is a jar
> file then you can list the content of the jar file.

Something like:

import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class Available {
     private static String getLocation(Class<?> clz) {
         return 
clz.getProtectionDomain().getCodeSource().getLocation().getPath();
     }
     public static void main(String[] args) throws IOException {
         String jarnam = getLocation(Available.class);
         System.out.println(jarnam);
         JarFile jarf = new JarFile(jarnam);
         Enumeration<JarEntry> e = jarf.entries();
         while(e.hasMoreElements()) {
             JarEntry je = e.nextElement();
             String clznam = je.getName().replace('/', '.');
             if(clznam.endsWith(".class")) {
                 System.out.println(clznam);
             }
         }
         jarf.close();
     }
}

Arne

[toc] | [prev] | [next] | [standalone]


#29946

FromKnute Johnson <nospam@rabbitbrush.frazmtn.com>
Date2014-09-11 20:51 -0700
Message-ID<lutqj9$qkh$1@dont-email.me>
In reply to#29945
On 9/11/2014 18:39, Arne Vajhøj wrote:
> On 9/11/2014 9:28 PM, Arne Vajhøj wrote:
>> On 9/11/2014 9:15 PM, Knute Johnson wrote:
>>> Is there a way given a program running from a .jar file to find the
>>> other classes in the .jar file?
>>
>> You can ask where a given class is loaded from and if it is a jar
>> file then you can list the content of the jar file.
>
> Something like:
>
> import java.io.IOException;
> import java.util.Enumeration;
> import java.util.jar.JarEntry;
> import java.util.jar.JarFile;
>
> public class Available {
>      private static String getLocation(Class<?> clz) {
>          return
> clz.getProtectionDomain().getCodeSource().getLocation().getPath();
>      }
>      public static void main(String[] args) throws IOException {
>          String jarnam = getLocation(Available.class);
>          System.out.println(jarnam);
>          JarFile jarf = new JarFile(jarnam);
>          Enumeration<JarEntry> e = jarf.entries();
>          while(e.hasMoreElements()) {
>              JarEntry je = e.nextElement();
>              String clznam = je.getName().replace('/', '.');
>              if(clznam.endsWith(".class")) {
>                  System.out.println(clznam);
>              }
>          }
>          jarf.close();
>      }
> }
>
> Arne
>
>

Thanks Arne, that's exactly what I was looking for.  I did have a little 
hiccup with the getLocation().getPath(), I'm using Windows and there is 
a space character in my path.  Apparently getPath() returns a URL 
encoded string but the URLDecoder fixed it right up.

Thanks very much,

knute...

[toc] | [prev] | [next] | [standalone]


#29973

FromJan Burse <janburse@fastmail.fm>
Date2014-09-14 01:48 +0200
Message-ID<lv2l4r$ofo$1@news.albasani.net>
In reply to#29945
Thx, was doing the other route for ages, i.e. picking a
resources path and unpacking the jar path, i.e.
lastIndexOf('!/'). Now consider changing my code.

I guess the topic is also discuss here:
http://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file

Arne Vajhøj schrieb:
> Something like:
>
> import java.io.IOException;
> import java.util.Enumeration;
> import java.util.jar.JarEntry;
> import java.util.jar.JarFile;
>
> public class Available {
>      private static String getLocation(Class<?> clz) {
>          return
> clz.getProtectionDomain().getCodeSource().getLocation().getPath();
>      }
>      public static void main(String[] args) throws IOException {
>          String jarnam = getLocation(Available.class);
>          System.out.println(jarnam);
>          JarFile jarf = new JarFile(jarnam);
>          Enumeration<JarEntry> e = jarf.entries();
>          while(e.hasMoreElements()) {
>              JarEntry je = e.nextElement();
>              String clznam = je.getName().replace('/', '.');
>              if(clznam.endsWith(".class")) {
>                  System.out.println(clznam);
>              }
>          }
>          jarf.close();
>      }
> }
>
> Arne
>
>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web