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 9 of 9

Thread: Programmatically Creating Jars

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,607
    Thanks
    24
    Thanked 314 Times in 294 Posts

    Default Programmatically Creating Jars

    I wanted to know if it was possible to, from an executable jar, create an executable jar.

    I am attempting to create a program with several parts.

    Part 1 is an Administrator Control Panel of sorts.
    Then there will be several preceding parts, all with fairly similar designs.

    What will happen is the Administrator will load a text file. Then, the data from the text file will be split up based on specifications. A jar needs to be created for each split where the specific data for each is contained within it (as a text file), as well as the runtime classes and a manifest. These jars will then be distrubted to the appropriate people.

    Tell me if I explained that correct.
    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
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,233
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Programmatically Creating Jars

    You can use the exec to call the jar command...note that if this is a distributed app, jar must be installed on the computer running the app (if this is a problem you could just distribute the executable with the app and point the jar command below to its location).
    Runtime.getRuntime().exec("jar ... ");//run the System level jar command
    Are these jar's intended to be executable?

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Programmatically Creating Jars

    Have you looked at the java classes for jar files and zip files?
    The doc says: "The JarOutputStream class is used to write the contents of a JAR file to any output stream."

  4. The Following User Says Thank You to Norm For This Useful Post:

    copeg (October 4th, 2010)

  5. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,607
    Thanks
    24
    Thanked 314 Times in 294 Posts

    Default Re: Programmatically Creating Jars

    Are these jar's intended to be executable?
    Yes

    Have you looked at the java classes for jar files and zip files?
    The doc says: "The JarOutputStream class is used to write the contents of a JAR file to any output stream."
    Ok, so I'm reading up on that now and I'm a little confused. How does a ZipEntry work? I would assume that you would add Files with that, but I'm not understanding how it works.

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Programmatically Creating Jars

    Did you look at the JarOutputStream class?
    What do you get when you Google it?

  7. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,607
    Thanks
    24
    Thanked 314 Times in 294 Posts

    Default Re: Programmatically Creating Jars

    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/

  8. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,607
    Thanks
    24
    Thanked 314 Times in 294 Posts

    Default Re: Programmatically Creating Jars

    Ok, with the help of examples, I am understanding this. But, I have a question.

    The Administrator Part of this program (which is what will create the jars) will, inside its own jar, have the class files that will be needed to create the User Jars. That Data Files for each User Jar will be created at runtime, and thus be outside the Administrator Jar when it is time to add them.

    I have played around with and edited the current code I have. How can I tell the Administrator Jar to get the class files for the User Jars from within itself? Is it done the same way as reading any old file inside one's own Jar, or is there something special that needs to be done since it is effectively copying the files instead of just reading them?

    Here is the code I have:
    Code to Create Jar:
    import java.io.*;
    import java.util.jar.*;
     
    public class JarCreatingProgram 
    {
     
        public static void main(String[] args) throws IOException
    	{
    		Manifest manifest = new Manifest();
    		manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    		manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, "TestClass");
    		JarOutputStream target = new JarOutputStream(new FileOutputStream("output.jar"), manifest);
    		add(new File("TestClass.class"), target);
    		target.close();
    	}
     
    	private static void add(File source, JarOutputStream target) throws IOException
    	{
    		BufferedInputStream in = null;
    		try{
    			if (source.isDirectory())
    			{
    				String name = source.getPath().replace("\\", "/");
    				if (!name.isEmpty())
    				{
    					if (!name.endsWith("/"))
    						name += "/";
    					JarEntry entry = new JarEntry(name);
    					entry.setTime(source.lastModified());
    					target.putNextEntry(entry);
    					target.closeEntry();
    				}
    				for (File nestedFile: source.listFiles())
    					add(nestedFile, target);
    				return;
    			}
     
    			JarEntry entry = new JarEntry(source.getPath().replace("\\", "/"));
    			entry.setTime(source.lastModified());
    			target.putNextEntry(entry);
    			in = new BufferedInputStream(new FileInputStream(source));
     
    			byte[] buffer = new byte[1024];
    			while (true)
    			{
    				int count = in.read(buffer);
    				if (count == -1)
    					break;
    				target.write(buffer, 0, count);
    			}
    			target.closeEntry();
    		}
    		finally{
    			if (in != null)
    			in.close();
    		}
    	}
    }

    Code for Test Jar Creation:
    import javax.swing.JFrame;
     
    public class TestClass 
    {
        public static void main(String[] args)
        {
        	JFrame frame = new JFrame("Executable Jar");
        	frame.setSize(200,200);
        	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	frame.setVisible(true);
        }    
    }


    Keep in mind that this is not my final code that will be used in my actual program, but rather code that I can test a play with prior to real implementation.

    Any help is appreciated.
    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/

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Programmatically Creating Jars

    How can I tell the Administrator Jar to get the class files for the User Jars from within itself? Is it done the same way as reading any old file inside one's own Jar, or is there something special that needs to be done since it is effectively copying the files instead of just reading them?
    I'd word it a bit differently but your question is understandable.
    A jar doesn't execute, classes execute.
    You need to read a file to copy it. I guess copying requires writing after reading.

    the same way as reading any old file inside one's own Jar
    Yes, I think that would be the way it worked

    Have you tried it yet? The best answer would be a working example.

    EDIT: It worked fine for me.
    Last edited by Norm; October 6th, 2010 at 09:31 AM.

  10. #9
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,607
    Thanks
    24
    Thanked 314 Times in 294 Posts

    Default Re: Programmatically Creating Jars

    I'm actually having to edit the code quite badly to allow me to use InputStreamReader to read from the jar. I am actually having difficulty doing this as I am now unsure how to create the JarEntry since it was previously a File for the constructor. However since I'm going to be attempting to read from the Jar, it needs to be some sort of InputStream, which means I cannot get the path of it to create the JarEntry (or at least i can't figure out how).

    Also, when I ran my jar the first time, it had the IO Error when trying to find the files before terminating. Unforuntately, it doesn't appear to have released rights over the Jar that was executed AND the Jar it attempted to create, which means I cannot delete them or replace them. I would think I just need to terminate the process, but I am unsure what process that is.
    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. Creating a bufferedimage!
    By Vexst in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: June 16th, 2010, 08:05 AM
  2. [SOLVED] frame closing programmatically
    By nasi in forum AWT / Java Swing
    Replies: 9
    Last Post: May 10th, 2010, 03:43 AM
  3. Replies: 3
    Last Post: April 14th, 2010, 07:33 PM
  4. Creating new instance
    By vluong in forum Object Oriented Programming
    Replies: 2
    Last Post: November 28th, 2009, 10:35 PM
  5. creating a gui
    By rsala004 in forum AWT / Java Swing
    Replies: 2
    Last Post: July 21st, 2009, 02:17 AM