1

I use the javadoc @version tag in my classes, however I am not sure how to get the version in the class itself. Let's look at an example...

/**
 * @version 1.0a
 * @author Redandwhite
 */
public class JClass extends JFrame implements ActionListener, KeyListener {
    String version = ...
}

Is there a method/class that can retrieve the version? Ideally, it's as simple as an API call which returns a simple String.

2 Answers 2

5

The javadoc comments are not included in the generated byte code in any form, so there is no way to access the value of the @version tag from Java code (unless you parse the source code of course). There might be a version annotation that can be used to specify the class version instead of or in addition to the javadoc @version tag, and this annotation would be accessible via the Java reflection API (i.e. Class.getAnnotations()).

2
  • 4
    One little caveat: The @deprecated JavaDoc tag (not to be confused with the annotation called @Deprecated) is actually represented in the class file (only its existence, not the optional comment). That's the only place where the content of a comment influences the result of the compilation in Java. Commented Jan 12, 2010 at 12:55
  • that is indeed unfortunate. Thanks for the helpful info Commented Jan 12, 2010 at 19:59
1

As it's already been noted, it's not possible to get to that information.

An alternative solution is to read the Implementation-Version/Specification-Version property of the package.

That value can be defined in the MANIFEST.MF file of a jar file. If it is defined, then it can be queried for every package that's contained in that jar file.

So if you have a class org.example.MyClass and the jar it comes in has the relevant entries in the MANIFEST.MF file, then you can get the version like this:

Class<?> clazz = org.example.MyClass.class
Package pkg = clazz.getPackage();
String version = pkg.getImplementationVersion();
1
  • this seems in line with what i'm looking for.. any idea how to set the VERSION value for the MANIFEST.MF file from within Netbeans? Commented Jan 12, 2010 at 19:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.