Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2014
    Posts
    2
    Rep Power
    0

    Question Using JarOutputStream to create a runnable jar file

    How does one use java.util.jar.JarOutputStream to create a jar file on button press? This new jar file, I want to make it runnable so it performs a specific action on run...

    Anyone know how I can accomplish this?

    Thanks

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Using JarOutputStream to create a runnable jar file

    Hi,

    jar files are just a simple archive (a ZIP archive to be exact) that contains a set of files. So you can use the JarOutputStream to simply add the required files to your JAR file (So all classes and the metafile META-INF/MANIFEST.MF which describes what should be started when it is called).

    So it should be straight forward. At which point do you need help? Can you provide us code that causes trouble?

    With kind regards,

    Konrad

  3. #3
    Join Date
    Apr 2014
    Posts
    2
    Rep Power
    0

    Default Re: Using JarOutputStream to create a runnable jar file

    I can't really figure out what to start :(

    This is where I've gotten so far:

    Java Code:
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    
    JarOutputStream target = new JarOutputStream(new FileOutputStream("name_of_file.jar"), manifest);
    	//other code in here?
    target.close();

  4. #4
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Using JarOutputStream to create a runnable jar file

    Hi,

    you got already the important classes so you could read the documentation or simply search on google for more details.
    What is missing:
    - You have to set more Attributes. To have it executable, you need the Main-class attribute. You could check JAR File Specification for this part.
    - You need to add your classes and maybe other required files to your jar. You create JarEntry objects and add them to your JarOutputStream. You have to make sure to use / as divider for directories inside the jar and when you create an entry that should be a directory, then you can construct it with a / at the end of the name. So the pattern is to call addNextEntry, then you write the content to the stream (if it is a file) and then you call closeEntry.

    Konrad

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,615
    Rep Power
    5

    Default Re: Using JarOutputStream to create a runnable jar file

    My recommendation would be to first, manually create a runnable jar, writing down the steps that you took to do so. Then when you get it working, you can focus on implementing those steps using the JDK classes.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 1
    Last Post: 03-17-2014, 02:03 AM
  2. Replies: 4
    Last Post: 12-31-2013, 04:11 PM
  3. Replies: 0
    Last Post: 06-07-2013, 01:39 AM
  4. Replies: 3
    Last Post: 05-07-2012, 06:07 PM
  5. Java: Make runnable java-file
    By MrBambey in forum New To Java
    Replies: 4
    Last Post: 08-10-2009, 06:40 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •