Solved

Get a class from a jar gievn the className

Posted on 2011-09-20
9
240 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now