?
Solved

Java

Posted on 2008-06-13
3
Medium Priority
?
197 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
[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
3 Comments
 
LVL 13

Accepted Solution

by:
Bart Cremers earned 200 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

Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn different types of Android Layout and some basics of an Android App.
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 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:
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

770 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