Solved

Get a class from a jar gievn the className

Posted on 2011-09-20
9
244 Views
Last Modified: 2012-05-12
Hi Experts,
I need to create a method that  returns a class<?>  from a jarFile given a className :
Class<?>  getClassFromJar (JarFile jar, className){
      Return the Class / Null if not found
}
e.g.
Class<?> clazz = getClassFromJAr ( MyBundle.jar, “org.kal.os.factory.class”);
0
Comment
Question by:bachra04
  • 7
  • 2
9 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36569215
You mean that this jar is not in your classpath ?

Otherwise you could use Class.forName(...)
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36569280
0
 
LVL 2

Author Comment

by:bachra04
ID: 36569281
Sorry not to be clear, the jar contains multiple classes including className

So the method will extract all classes and if the class

matches the given className, it returns it.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 47

Expert Comment

by:for_yan
ID: 36569285
This is how you can do it with JCL:

JarClassLoader jcl = new JarClassLoader();

  //Loading classes from different sources
  jcl.add("myjar.jar");
  jcl.add(new URL("http://myserver.com/myjar.jar"));
  jcl.add(new FileInputStream("myotherjar.jar"));
  jcl.add("myclassfolder/");

  //Recursively load all jar files in the folder/sub-folder(s)
  jcl.add("myjarlib/");

  JclObjectFactory factory = JclObjectFactory.getInstance();

  //Create object of loaded class
  Object obj = factory.create(jcl, "mypack.MyClass");

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36569295


So do you need this class for using it in the same code after you extract it?
Or you just need to extract it as a file?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36569303
If you need to use it in your code then
the above snippet from JCL shows how to create instance of such class
0
 
LVL 2

Author Comment

by:bachra04
ID: 36569318
I need to extract it as a file
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36569393
To extract as a file - jar is just a zip file - so
the same way you extract it from zip file should work
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36569400
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
more than one jdk and one jre 1 50
Desingning Refactoring existing code 2 26
Java: The Public Class Main 4 32
java example issue 3 23
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question