Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Getting List of Files Within Jar

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Getting List of Files Within Jar

    Ok, so I need to do something that, in theory, should be easy, but I haven't found anything useful online.

    I will have an executable jar. When that jar is executed (via the main .class file inside of it), I need to be able to get a list of the files inside that jar.

    The reason I am having problems finding a solution online for this is because the all of the solutions that are sort of about this assume at least one of two things:
    1) The jar name will never change
    2) The jar directory will never change

    That is not that case for my situation. I know, for a fact, that the jar directly will change AND I would assume the jar name will also eventually change. Because of that, I need to be able to access the jar without using the directory referencing.

    Essentially, I am trying to get the list of all of the .class files in the jar that is being executed so I use the get stream of the file, provided it meets certain naming criteria. The files will not be in any directories inside the jar.

    Can anyone help me out here? This has been an issue for me for like 2 weeks now and I have no idea where to start.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Getting List of Files Within Jar

    Ok, well naturally I just happen to figure it out as soon as I posted the question.

    Here is the code for anyone in the future who has this same issue:
    //Where "CreatingJar" is the name of the class this group of code is inside of
    JarInputStream in = new JarInputStream(CreatingJar.class.getProtectionDomain().getCodeSource().getLocation().toURI().toURL().openStream());
    			JarEntry entry = in.getNextJarEntry();
    			ArrayList<String> entryList = new ArrayList<String>();
    			while(entry!=null)
    			{
    				entryList.add(entry.toString());
    				entry = in.getNextJarEntry();
    			}
    in.close();

    Enjoy
    Last edited by aussiemcgr; October 13th, 2010 at 09:45 AM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Java program which can list all files in a given directory
    By JavaPF in forum Java Programming Tutorials
    Replies: 8
    Last Post: July 9th, 2013, 03:38 AM
  2. hlp files
    By vgenopoulos in forum Java Theory & Questions
    Replies: 1
    Last Post: July 9th, 2010, 09:11 AM
  3. jar files
    By bguy in forum Java Theory & Questions
    Replies: 2
    Last Post: November 23rd, 2009, 06:37 PM
  4. exe files
    By subhvi in forum Java Theory & Questions
    Replies: 17
    Last Post: September 3rd, 2009, 09:43 AM