Solved

Java

Posted on 2008-06-13
3
193 Views
Last Modified: 2013-12-29
Hi All

I would like to have a method which will list all the files in the current jar (i.e. the jar which is running). Is there anyway to do it?

Thanks
Srikanth
0
Comment
Question by:vi_srikanth
3 Comments
 
LVL 13

Accepted Solution

by:
Bart Cremers earned 50 total points
ID: 21784369
You can read the contents of a jar file using the JarFile class.

JarFile jFile = new JarFile(myFile);

Enumeration entries = jFile.entries();

while (entries.hasMoreElements()) {
    JarEntry entry = entries.nextElement();
    ...
}

But the problem lies in finding out which "Jar" you're running from. A Java program is not necessarily running from a Jar file. The program code could be in a plain directory, a jar file, somewhere over a network, or plain loaded from memory or bytecode. It all depends on the way classes are loaded (ClassLoader).

You can get a URL to where from a class is loaded by

this.getClass().getResource("/" + this.getClass().getName().replace('.', '/') + ".class);

but it you'll have to check the URL to find out how it was loaded and if you can possible start listing files from there some way.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
couple of eclipse 5 35
javap not working 8 43
servlet example 11 40
hibernate example for saving data 19 37
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…
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 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 video teaches viewers about errors in exception handling.

813 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

10 Experts available now in Live!

Get 1:1 Help Now