Results 1 to 10 of 10
  1. #1
    lunarbof is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Default How to print the creation date of a jar file

    Hi there,

    I am creating a jar file using eclipse.

    I would like the jar file to print it's creation date whenever I run it (to make sure I am running the right version).

    One way of doing it is to print an hardcoded string representing the jar creation date, but then I have to modify that string manually every time a create the jar.... This is not very convenient and so far I forget more often than not to change the date... so I always end-up wondering if I really have the right version.

    Any ideas ?

    Thanks!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    Do any of the File class methods return the date you are looking for? If so, you could use the getResource() method against the class it is executed in to get a path to the jar file and then use that path in a File() constructor to get the date.

    Try this in a class in a jar file to see what you get:
    System.out.println("resouce loc=" + this.getClass().getResource("SurveyServlet.class") );
    This is from a program/class named SurveyServlet. Obviously its class file must exist somewhere.

    Looking at the response, you'll see how to parse the path of the jar file.

  3. #3
    lunarbof is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Default

    Hi Norm, Thanks for the idea.

    What I understand from your post is that you suggest that I take the last modification date of the jar file.

    In other word the date returned by

    new File("C:\test.jar").lastModified();

    I did the test and here's the limitation of that solution:

    Assume I create the jar file on August 3rd and put it on a web site for people to download. If somebody downloads the file on August 10th, the lastModified() function will return August 10th (even in the jar file was originally packaged on August 3rd).

    What I am looking for is the date at which the jar was packaged. So I think the solution lies more in inserting the date in the jar manifest file at packaging time... but I have no clues as how to do that.

    Thanks anyways for the idea !

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    whenever I run it vs If somebody downloads
    OK, new problem.
    At jar create time, create a file, setting its last mod date and put that into the jar file.
    At execution time look at its ZipEntry for that mod time.

  5. #5
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    You could put the date in as a manifest file entry, and use the Manifest class to read it when necessary. For info on manifests see java.sun.com/j2se/1.3/docs/guide/jar/jar.html#Notes on Manifest and Signature Files.

  6. #6
    lunarbof is offline Member
    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Default

    dlorde,

    your idea is good, but can you be more precise as to how I automatically put the jar creation date in the jar manifest when I create the jar with eclipse.

    I am using eclipse jarpackager utility and I have my jardescription saved, so I only need to right-click on it and say generate jar to get an up-to-date jar file. This is convenient as when my projects has been largely modified, I can reopen the jardescription and include the modifications.

    So is there a way to tell the jarpackager of eclipse to include the creation date in the manifest file? or do I have to write my own script for jar creation and then set up my manifest as I need it ? There must be a way to do it through Eclipse's jarpackager !

    Thanks!

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    how I automatically put the jar creation date in the jar manifest when I create the jar
    I believe that the date on the manifest file in the jar file is as of the creation of the jar file. So if you get a ZipEntry for the manifest file, its time will be the creation time.
    You can easily see the time stamp (Modified column) by opening the jar file in WinZip to verify this.

    put the date in as a manifest file entry
    How to do this in the jar creation script? It would seem that a big and hairy IDE would have that built-in as an option.
    Last edited by Norm; 08-22-2008 at 03:26 AM.

  8. #8
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by Norm View Post
    a big and hairy IDE would have that built-in as an option.
    I can't speak for all IDEs. Netbeans looks big and hairy but actually is built of a lot of smaller modules. Specifically, in Netbeans, the jar/war/ear file is created by an ANT script, using the normal Apache ANT. While NB will generate the ant script for you, you can modify it to do anything you want. You just have to RTFM the ant manual.

    BTW, you can just use the 'jar' program to look inside a jar file, you don't need something like winzip. so check out the man pages for jar.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    On windows its easier to RC on a jar file and ask for WinZip to show you the contents. When looking at the contents of a jar file, Winzip also allows you to view the contents of the manifest file. To view a jar file contents with jar on windows would require a batch file that would call jar and then keep the window open and allow scrolling.
    Linux would have better scripts for doing it.

    The question is: How to make an entry in the manifest with the current date?

  10. #10
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by Norm View Post
    . To view a jar file contents with jar on windows would require a batch file that would call jar and then keep the window open and allow scrolling.

    The question is: How to make an entry in the manifest with the current date?
    When I developed on Windoze, I just kept a cmd window open for things like this. No point in writing scripts for a ten character command

    The rules for that have to be documented in Sun's jar command docs.

    I tend to not trust dates, if you really need a version, have SVN or CVS write a $Revision$ string into the source something like:

    Java Code:
    public static final String versionString = "$Revision";

Similar Threads

  1. How to print text file in java(dotmatrix printer)
    By yoganeethi in forum Advanced Java
    Replies: 4
    Last Post: 12-01-2010, 01:45 PM
  2. How to print a HTML file in browser look using JAVA
    By ammameiya in forum Advanced Java
    Replies: 5
    Last Post: 06-25-2010, 02:36 AM
  3. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 PM
  4. Print a picture file
    By oli001 in forum New To Java
    Replies: 0
    Last Post: 11-26-2007, 01:40 PM
  5. An Introduction to Eclipse PDE - File Creation
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 05-09-2007, 06:40 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
  •