Results 1 to 6 of 6
  1. #1
    msphelix is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default How to create a jar file without changing the development directory structure

    Hi,
    I'm attempting to figure out creating jar files of programs and have a problem.

    Given the following directory structure:
    MyProgram
    ....bin (class files for myprogram)
    ........mypackage
    ............myprogram.class (package mypackage;)
    ....images (images used by myprogram)
    ........myimage01.jpg
    ........myimage02.jpg
    ....src (source files for myprogram)
    ........mypackage
    ............myprogram.java (package mypackage;)
    ...........(accesses the image files as "images.myimage01.jpg")

    What is the best way to create a jar file for this program in DOS not thru eclipse or any other tool?

    I have tried every combination of
    at what level to attempt it;
    where to place the manifest file;
    how to format the name for the main class in the manifest file (Main-Class: mypackage.myprogram or bin.mypackage.myprogram)
    how to incorporate the image directory;

    The only way I have succeeded is to do the following:
    create the following directory with the following contents
    ....jarcreation
    ........images
    ............myimage01.jpg
    ............myimage02.jpg
    ........myprogram.java
    ........manifest.txt (Main-Class: myprogram)
    modify myprogram.java
    ....remove the package statement;
    cd in DOS to directory jarcreation
    compile myprogram: jarcreation>javac myprogram.java so there's a class file in the directory
    call jarcreation>jar cvmf manifest.txt myprogram.jar *
    Now I have a jar file that I can run with:
    ....jarcreation> java -jar myprogram.jar

    I believe the top directory structure is the accepted standard structure for a Java program so isn't there a way to create a jar file from that structure?
    Am I missing something about the jar command?
    Please be very specific about
    what directory I should be at in DOS;
    how the main class should be named in the manifest file;
    where the manifest file should go;
    how the main class should be named in the jar creation command;
    how to include the Images directory.

    Thanks for the help.
    Last edited by msphelix; 11-11-2012 at 12:30 AM.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: How to create a jar file without changing the development directory structure


  3. #3
    msphelix is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: How to create a jar file without changing the development directory structure

    Post 2
    Thanks, but that is not the issue here. I can take the the original directory structure and remove it from the bin and src directories so the directory structure is as follows:

    MyProgram
    ....images (images used by myprogram)
    ........myimage01.jpg
    ........myimage02.jpg
    ....mypackage
    ........myprogram.class
    ........myprogram.java (package mypackage;)
    ........(accesses the image files as "images.myimage01.jpg")
    ....MyProgramManifest.txt

    MyProgramManifest.txt contains this line:
    Main-Class: mypackage.myprogram

    and it works fine.

    It's when I separate the class and java files into separate directories, bin and src, that I can't figure out where to place the manifest file and what it should say.
    With the original directory structure in the original post, I have tried
    saying Main:Class: bin.mypackage.myprogram in the manifest file and
    moving the original manifest file to bin and creating the jar file there.

    Can you or anyone tell me a way to do what I want?

    Also, am I wrong in thinking that, once I have created a working jar file, MyProgram.jar, that correctly displays the image files in images when run from the MyProgram directory, I should be able to copy MyProgram.jar to a different directory and run it from there? If MyProgram.jar should work in any directory, then I have another problem - MyProgram.jar will run fine but does not display the images. Any ideas on that issue?
    Last edited by msphelix; 11-18-2012 at 11:55 PM.

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: How to create a jar file without changing the development directory structure

    If your manifest file is really called "MyProgramManifest.txt" then it wont work at all. See Understanding the Default Manifest (The Java™ Tutorials > Deployment > Packaging Programs in JAR Files)

    You can include your source files, that's not a problem. The jar just ignores them at runtime. You probably aren't referencing your images and whatnot within your program correctly. If your package declared in the code or did you just drop your code in a folder?

  5. #5
    msphelix is offline Member
    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Default Re: How to create a jar file without changing the development directory structure

    Post 3
    Thank you for replying.
    1.
    Why is calling my manifest file MyProgram.txt wrong? See Setting an Application's Entry Point http://docs.oracle.com/javase/tutori...ar/appman.html which clearly says:
    "
    We first create a text file named Manifest.txt with the following contents:
    Main-Class: MyPackage.MyClass
    We then create a JAR file named MyJar.jar by entering the following command:
    jar cfm MyJar.jar Manifest.txt MyPackage/*.class
    "
    This is exactly what I am doing.

    Maybe I wasn't clear enough -see my post 2 from yesterday:
    with the directory structure there, including the MyProgramManifest.txt file, I can create jar file MyProgram.jar from the MyProgram directory using the following command:

    MyProgram>jar -cvmf MyProgramManifest.txt MyProgram.jar Images mypackage/ *.class

    and can run it from the MyProgram directory using the following command:

    MyProgram>java -jar MyProgram.jar

    I was trying to figure out how to do this with the directory structure from my original post which separates the class files and java source files into separate directories, bin and src. I'm beginning to believe that it's not possible to do it from that directory structure.
    2.
    I guessed that I'm not referencing the images correctly. As is shown in the directory structure in my second post, the one without the bin and src directories, the images are in a directory called Images at the same level as the directory mypackage which contains the source and class files. The class are in the mypackage package and MainPanel references the image files as new ImageIcon("images\\myimage01.jpg");
    This does work when I am running MyProgram.jar from the MyProgram directory but if I copy it into any other directory, it runs but the images do not display.
    Is there a way to reference the images so the jar file works whereever I put it?
    Last edited by msphelix; 11-19-2012 at 12:07 AM.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: How to create a jar file without changing the development directory structure

    Quote Originally Posted by msphelix View Post
    See Setting an Application's Entry Point Setting an Application's Entry Point (The Java™ Tutorials > Deployment > Packaging Programs in JAR Files) which clearly says:
    "
    We first create a text file named Manifest.txt with the following contents:
    Main-Class: MyPackage.MyClass
    We then create a JAR file named MyJar.jar by entering the following command:
    jar cfm MyJar.jar Manifest.txt MyPackage/*.class
    "
    This is exactly what I am doing.
    No it isn't. The manifest file must have exactly the name given in the tutorial. Not MyProgram.txt.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 5
    Last Post: 11-24-2010, 11:57 AM
  2. Zip Directory Structure
    By freestatedon in forum New To Java
    Replies: 2
    Last Post: 02-15-2010, 07:11 AM
  3. Directory Structure
    By rummy in forum New To Java
    Replies: 1
    Last Post: 01-21-2010, 01:03 PM
  4. Replies: 0
    Last Post: 03-18-2009, 02:35 PM

Posting Permissions

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