|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

08-20-2008, 07:33 PM
|
|
Member
|
|
Join Date: Aug 2008
Posts: 4
|
|
|
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!
|
|

08-20-2008, 07:53 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
|
|
|
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.
|
|

08-21-2008, 06:53 AM
|
|
Member
|
|
Join Date: Aug 2008
Posts: 4
|
|
|
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 !
|
|

08-21-2008, 04:04 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
|
|
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.
|
|

08-22-2008, 02:54 AM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 7
|
|
|
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.
|
|

08-22-2008, 05:11 AM
|
|
Member
|
|
Join Date: Aug 2008
Posts: 4
|
|
|
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!
|
|

08-22-2008, 05:23 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
|
|
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 05:26 AM.
|
|

08-22-2008, 08:48 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 533
|
|
Originally Posted by Norm
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.
|
|

08-22-2008, 03:57 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
|
|
|
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?
|
|

08-22-2008, 08:12 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 533
|
|
Originally Posted by Norm
. 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:
public static final String versionString = "$Revision";
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|