Solved

Java

Posted on 2008-06-13
3
190 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_Cr earned 50 total points
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

743 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