Solved

Get a class from a jar gievn the className

Posted on 2011-09-20
9
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
fibonacci ten numbers 4 54
check java version using powershell 13 292
ejb message driven bean mdb creation steps 2 34
jsp error 6 43
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

763 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