Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Automatic Version Identifier  (Read 2191 times)
0 Members and 1 Guest are viewing this topic.
Offline kevglass

JGO Kernel


Medals: 120
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Posted 2004-07-10 17:52:19 »

I'm not sure if this is a common knowledge thing that I've not picked up in the past but here we go.. For those of us who have used C and C++ the idea of having a string generated based on the compilation date of the code is pretty normal. Its really useful for identifing the version of the software the end user has. In C this is done with a preprocessor macro that gets converted into today date.

I've been wanting something like this for Java for a while and this morning I had an idea which I'm now using happily. I simple grab the date of the "main" class file from my game java like this:

1  
2  
3  
4  
5  
6  
7  
URL versionUrl = getClass().getClassLoader().getResource("org/newdawn/grav2/GameWindow.class");
try {
                // update the title of the window based on the version
     title += " ("+new Date(versionUrl.openConnection().getLastModified())+")";
} catch (Exception e) {
      title += " (Version Undetermined)";
}


Maybe theres a better way that I don't already know about? I quite like this tho since it gives you a different date even if you just repackage your jar.

Kev

Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #1 - Posted 2004-07-11 02:32:33 »

Nice.

What I do is use the Version info in the JAR Manifest file.  I generate the JAR with Ant so I have Ant put the build date and other version info from Ant properties (supplied on ant command line) into the manifest version line.  Then I extract that info at runtime.

I think this does the trick:
1  
2  
3  
4  
String appVersion =  new JarFile("test.jar").
   getManifest().
       getMainAttributes().
         getValue("Specification-version");

Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #2 - Posted 2004-07-11 10:04:30 »

Quote
Nice.

What I do is use the Version info in the JAR Manifest file.  I generate the JAR with Ant so I have Ant put the build date and


IME this is usually the best way to do it, since it's compatible with all auto-build systems (shrug; I don't use ANT, but anything that's able to auto-build a JAR should have a macro to let you do this stuff!). Our build system uniquely versions every build with an incremented version number automatically, so it's trivial.

The only problem is that the JAR file manifest is a shoddy hacked-together afterthought, and has some big holes in it's design. It's OK at first, but for large projects it quickly becomes useless, and you have to start inventing lots of extra proprietary meta-data because Sun didn't Sad.

So, if anyone knows of slightly-less-proprietary package-versioning system for java, I beg you to enlighten me! Smiley

malloc will be first against the wall when the revolution comes...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kevglass

JGO Kernel


Medals: 120
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #3 - Posted 2004-07-11 10:23:02 »

OOI, how do you get hold of the jar file when you're running in webstart? You can't incidently look up meta-data type information from the jar file using the classpath (since its not really in the classpath).

Kev

Offline jnz

Innocent Bystander




java.lang.Illeg alAccessExcepti on


« Reply #4 - Posted 2004-07-11 17:59:04 »

This should work with both Applets and WebStart applications (off the top of my head... haven't actually tested:)):
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
      public String getAppVersion(Class cls) throws IOException {
            String classFile = cls.getName().replaceAll("\\.", "/") + ".class";
            //System.out.println("class file:" + classFile);
           URL classLocation = cls.getResource(classFile);
            //System.out.println("class location: " + classLocation);            
           JarFile jarFile = ((JarURLConnection)classLocation.openConnection()).getJarFile();
            Manifest mf = jarFile.getManifest();
            String appVersion = mf.getMainAttributes().getValue(Attributes.Name.SPECIFICATION_VERSION);
            //System.out.println("application version: " + appVersion);
           
            return appVersion;
      }

#EMG820702
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #5 - Posted 2004-07-11 23:15:00 »

Hmmm... Kevin...

versionUrl.openConnection().getLastModified()

"Returns the value of the last-modified header field. The result is the number of milliseconds since January 1, 1970 GMT."

So we get a long which accurately tells us the date (pretty ok-ish if you use .toHexString()). However, that new Date() thingy (and it's toString()) produces a date which is somewhat difficult to compare. Eg I get it in "CEST" - my local timezone.

Something like this would be better:
1  
2  
3  
4  
5  
import java.text.DateFormat;
[...]
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.SHORT,Locale.ENGLISH);
df.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(df.format(new Date(versionUrl.openConnection().getLastModified())));


The locale was explictly set in order to get identical formatting.

Now I get:
Jul 11, 2004 10:56 PM

Instead of:
Mon Jul 12 00:56:48 CEST 2004 (and everyone would get something else here)

getLastModified() with toHexString and toUpperCase looks like this:
FDB06C3780 (also ok and comparable)

Anyways... great idea Smiley

弾幕 ☆ @mahonnaiseblog
Offline kevglass

JGO Kernel


Medals: 120
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #6 - Posted 2004-07-12 07:36:37 »

Erg, fair point Wink Thanks for the point out.

Will fix,

Kev

Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #7 - Posted 2004-07-12 18:17:36 »

Well, the documentation wasn't that specific (acutally it was pretty generic and vague... returns a String representation blabla Roll Eyes).

Oh and that hex timestamp (FDB06C3780)... I'll send that value together with highscores. So if a change comes (which invalidates older scores), I'll be able to filter those scores out. I don't think that something like that could happen, but you can never know Wink

I also implemented error codes for version missmatch, because it's a thing which just will happen ("offline allowed" is to blame btw). Hm... eventually I should put another version number into it (which indicates changes in the scoring system)? Smiley

弾幕 ☆ @mahonnaiseblog
Offline AndersDahlberg

Junior Member





« Reply #8 - Posted 2004-07-16 16:26:12 »

Quote
This should work with both Applets and WebStart applications (off the top of my head... haven't actually tested:)):
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
      public String getAppVersion(Class cls) throws IOException {
            String classFile = cls.getName().replaceAll("\\.", "/") + ".class";
            //System.out.println("class file:" + classFile);
           URL classLocation = cls.getResource(classFile);
            //System.out.println("class location: " + classLocation);            
           JarFile jarFile = ((JarURLConnection)classLocation.openConnection()).getJarFile();
            Manifest mf = jarFile.getManifest();
            String appVersion = mf.getMainAttributes().getValue(Attributes.Name.SPECIFICATION_VERSION);
            //System.out.println("application version: " + appVersion);
           
            return appVersion;
      }



Even better, if using ant/other automatic build system, is IMO:
1  
2  
3  
4  
5  
public String getAppVersion(Class clazz) {
    Package p = clazz.getPackage();
    // or is it p.getImplementationVersion()?
   return (p == null) ? null : p.getSpecificationVersion();
}


Haven't tested in applets, works in webstart though IIRC
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (26 views)
2014-07-23 16:59:36

Riven (25 views)
2014-07-23 12:16:32

Riven (19 views)
2014-07-23 12:07:15

Riven (22 views)
2014-07-23 11:56:16

ctomni231 (51 views)
2014-07-17 21:55:21

Zero Volt (46 views)
2014-07-17 14:47:54

danieldean (37 views)
2014-07-17 14:41:23

MustardPeter (40 views)
2014-07-16 14:30:00

Cero (56 views)
2014-07-15 15:42:17

Riven (55 views)
2014-07-14 09:02:53
HotSpot Options
by dleskov
2014-07-07 18:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-13 15:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-13 15:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 02:13:37

HotSpot Options
by Roquen
2014-05-15 00:59:54

HotSpot Options
by Roquen
2014-05-06 06:03:10

Escape Analysis
by Roquen
2014-04-29 13:16:43

Experimental Toys
by Roquen
2014-04-28 04:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!