0

We'd like to have the version of the java source code, in our case the svn $Id$ string, embedded in the generated class file.

We'd like to be able to determine this information from a static inspection of the class file, preferably by running the strings or what command.

A naive attempt to declare a private final static String variable set to this value inside each class didn't result in a legible string embedded in the class file.

5
  • How does your version string look like? three bytes 1.0 are very easy to overlook in a binary file. Commented Jan 17, 2013 at 20:31
  • See also What is a serialVersionUID and why should I use it?. That might prove interesting. Commented Jan 17, 2013 at 20:36
  • @Jan thanks. I was testing with an unexpanded "$Id$" string, once I checked it into svn and got it back in expanded form I noticed it in the output of the strings command Commented Jan 17, 2013 at 22:01
  • Do you need the version in the class file? or do you want your java app to return the version number? typically this would be put in the jar/war/ear manifest file. Commented Jan 17, 2013 at 22:29
  • We aren't using _ar packaging mechanism so it would need to be in each class file Commented Jan 29, 2013 at 20:38

2 Answers 2

2

You said ... preferably by running the strings or what command ...

Let's assume you have something like this in your code:

private static final String SVN_ID = 
    "$Id: SvnIdDemo.java 1081 2008-09-30 19:03:23Z john $";

The following Perl one-liner ...

$ perl -nwe 'print "$1\n" if /.*(\$Id:[^\$]+\$).*/' SvnIdDemo.class

... prints the SVN ID string to STDOUT.

$Id: SvnIdDemo.java 1081 2008-09-30 19:03:23Z john $
Sign up to request clarification or add additional context in comments.

1 Comment

The given Perl syntax works on a Unix like system. On Windows you cannot use single-quotes for the mini script. You need double-quotes instead which requires you to escape the inner double-quotes with a backslash.
1

You could add a method to the bottom of each class with a predefined name. Say you used:

public String extractChaimGeretzVersionNumber() {
    return "$Id$";
}

Then you find the version with a program that loads the class and, via reflection, calls the magic method and retrieves the version.

You would either have to have code that inserted the method to the .java files before building the .class and .jar files OR you could have a build step that checked that the magic method was already in every one of them. Fail the build if not found.

1 Comment

When I did this sort of thing one time, I would just put something in the jar file that tied to the version control. So you tag VC, extract all the source, build the tag into the manifest (or have the build script write a class that returns the tag from a static method), and build the jar. If you needed to know what version of a class file is present, get the tag from the jar (or have it displayed on the screen or logged) and go retrieve the code versions from the version control.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.