Solved

Java

Posted on 2008-06-13
3
191 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 3 50
Error trying to install RTMT Win7 5 41
Windows 10 and JDK 7 7u85 4 31
javap not working 8 36
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

910 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

21 Experts available now in Live!

Get 1:1 Help Now