Results 1 to 13 of 13

Thread: ".jar" files

  1. #1
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default ".jar" files

    Hello everyone

    I want to learn how to create ".jar" archives by using the jar program in Command Prompt. It will be great if someone could give me an example of how a folder of ".class" files, including the class with the main() method, can be placed into a ".jar" file using the Command Prompt for Windows.

    Thank you. ;)
    Last edited by tim; 01-30-2008 at 09:34 AM.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  2. #2
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    I would also like to know how to do this. It is to my understanding that, if i turn a program into a .jar, i will be able to send it to friends and they can execute my program via double-click (if they have JRE). So, yeah, if someone could help us out that would be awesome!

  3. #3
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Refresh thread

    Hello everyone.

    I just want to refresh my question for gibsonrocker800 and me. :D

    Thank you. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  4. #4
    Cnu
    Cnu is offline Member
    Join Date
    Feb 2008
    Posts
    13
    Rep Power
    0

    Default

    let us assume you have 3 java files in a folder say Cnu
    as
    One.java
    Two.java
    Three.java

    and code is as follows.
    One.java
    =======
    class One
    {}

    Two.java
    ======
    class Two
    {
    }
    Three.java
    ========
    class Three
    {
    public static void main(String[] args)
    {
    System.out.println("Three");
    }}

    place all these files in CNU folder after that compile all java classes.

    after that create a jar file called example.jar as

    c:\cnu\jar -cf example.jar

    after that create a file called manifest file .manifest file is a file where we store information about the classes having main().
    maniClass.txt(manifest file)
    ========
    Main-Class : Three

    (Note Three is the name of the class having main().You can mention either Three or Three.class but not Three.java)
    after that
    c:\cnu\jar -cmf example.jar *.class mainClass.txt
    if we want to execute our jar files type the follwoing command
    c:\cnu\java -jar example.jar
    then u will get output as "Three"

    Try this one

  5. #5
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Thank you

    Thank you, Cnu, that really helps! :)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  6. #6
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Extra information

    Hello everyone.

    I'm sorry for the double post, but I've done some reading about this topic in a Wikipedia article. I've learned that is important to end your manifest file with either a new line or carriage return.

    Thank you. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  7. #7
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    Hey guys, here's how I'm doing it. Without any newline. You can set the manifest right in the creation of the jar

    So, say i have two classes: Example, and ExampleTester (which has main)

    at the command line, we set the path to the location of these classes. Then we say

    Java Code:
    jar cfe ExampleJar.jar ExampleTester ExampleTester.class Example.class
    the ExampleJar.jar is the new Jar to be created

    ExampleTester specifies the main class.

    ExampleTester.class and Example.class specify the two files to put in the jar.


    This works perfectly for me!

  8. #8
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    The Deployment trail in the tutorial has a good lesson on jar files: Lesson: Packaging Programs in JAR Files.

  9. #9
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Manifest

    Hello gibsonrocker800

    The reason that I want to use a self-defined manifest file, is because I might want to add other information, like libraries for example Java3D or XOM. :p You also made a duplicate post. Just edit the second one and delete it. :D
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  10. #10
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    Sorry for the double-post tim, my internet was being slow and not working so i clicked post twice.

    But yeah about the manifest, i had trouble doing it that way. It didn't work for me. I don't know, i'll try it out again.

  11. #11
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Manifest

    Quote Originally Posted by gibsonrocker800
    Sorry for the double-post tim, my internet was being slow and not working so i clicked post twice.

    But yeah about the manifest, i had trouble doing it that way. It didn't work for me. I don't know, i'll try it out again.
    No problem. I have a slow connection, so I know have you feel. My manifest file is located in the class path folder and it looks like this:
    Java Code:
    Main-Class: pack.Main
    Note that it ends with a new line. All my classes are in a folder called "pack", hence they are part of the "pack" package. My batch file looks like this:
    Java Code:
    SET PATH=C:\Program Files\Java\jdk1.6.0_01\bin\
    SET CLASSPATH=C:\java\
    cd c:\java\pack\
    javac *.java
    cd c:\java
    jar -cfm run.jar manifest.MF pack\*.class
    java -jar run.jar
    cd c:\java\
    I hope this helps you gibsonrocker800! Cheers :D
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  12. #12
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    A few questions on this

    How do you create a manifest file (with .MF)
    How do you put a newline in the file.
    I'm confused about where to put the files.

  13. #13
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Hello gibsonrocker800. :p

    Quote Originally Posted by gibsonrocker800
    How do you create a manifest file (with .MF)
    I use notepad in Windows XP. I then save it as a normal text file. Make sure that in your folder options, under the "view" tab, that the "Hide extendsions for known file types" is not ticked. You should see the name of the manifest file to be "manifest.txt". Just rename it to "manifest.MF". I think that you can also pass a normal ".txt" file as a manifest file.
    Quote Originally Posted by gibsonrocker800
    How do you put a newline in the file.
    When you typed your last line and the cursor is still on that line, press "enter". In Java, this will look like a String instance: "Main-Class: pack.Main\n"
    Quote Originally Posted by gibsonrocker800
    I'm confused about where to put the files.
    See my attachment and then you can see where I have all my files. Just extract it in your C drive.

    Good luck. ;)
    Attached Files Attached Files
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. Hwlp with "Open", "Save", "Save as..."
    By trill in forum New To Java
    Replies: 3
    Last Post: 11-02-2010, 10:26 AM
  2. Windows ".exe" files
    By tim in forum New To Java
    Replies: 3
    Last Post: 12-28-2007, 10:25 PM
  3. Replies: 1
    Last Post: 07-27-2007, 11:59 PM
  4. Replies: 1
    Last Post: 07-25-2007, 11:55 PM
  5. Replies: 1
    Last Post: 07-16-2007, 07:32 AM

Posting Permissions

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