Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Java

Posted on 2008-06-13
3
Medium Priority
?
204 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
A solution for Fortify Path Manipulation.
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

609 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