views:

144

answers:

3

Is there a Java equivalent to the C & C++ compile-time constants __DATE__ and __TIME__. I need to print compile time and version info of the program being compiled.

Thanks

kodev19

+1  A: 

Not directly in the code, no, but you can use Ant to do this.

See the "wombat" examples on this page.

Jeff
+1  A: 

The Ant TStamp task is of use here. It'll create a timestamp during build time for insertion into properties files or code.

Sets the DSTAMP, TSTAMP, and TODAY properties in the current project. By default, the DSTAMP property is in the format "yyyyMMdd", TSTAMP is in the format "hhmm", and TODAY is in the format "MMMM dd yyyy". Use the nested element to specify a different format.

These properties can be used in the build-file, for instance, to create time-stamped filenames, or used to replace placeholder tags inside documents to indicate, for example, the release date. The best place for this task is probably in an initialization target.

I tend to use this to build a properties file containing build times and dates, the user and host, a version number etc. and then my application reads that properties file and logs the related info (I'm not normally in an environment where I'd worry about people fiddling with this info).

Brian Agnew
+1  A: 

As far as i know, there is nothing like that. But on a running JVM you can get a few informations straight away from the jar using something like the code below (here, the information comes from the Manifest file put in the jar at compilation time (which ever your build system is, Ant or Maven or anything else). Feel free to adapte it (different output, and so on).

    public String getVersionfinal Class classe) {
 String version = null;
 String shortClassName = classe.getName().substring(classe.getName().lastIndexOf(".") + 1);
 try {
  ClassLoader cl = this.getClass().getClassLoader();
  String threadContexteClass = classe.getName().replace('.', '/');
  URL url = cl.getResource(threadContexteClass + ".class");
  if ( url == null ) {
   version = shortClassName + " $ (no manifest)";
  } else {
   String path = url.getPath();
   String jarExt = ".jar";
   int index = path.indexOf(jarExt);
   SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
   if (index != -1) {
    String jarPath = path.substring(0, index + jarExt.length());
    File file = new File(jarPath);
    String jarVersion = file.getName();
    JarFile jarFile = new JarFile(new File(new URI(jarPath)));
    JarEntry entry = jarFile.getJarEntry("META-INF/MANIFEST.MF");
    version = shortClassName + " $ " + jarVersion.substring(0, jarVersion.length()
      - jarExt.length()) + " $ "
      + sdf.format(new Date(entry.getTime()));
    CloseHelper.close(jarFile);
   } else {
    File file = new File(path);
    version = shortClassName + " $ " + sdf.format(new Date(file.lastModified()));
   }
  }
 } catch (Exception e) {
  version = shortClassName + " $ " + e.toString();
 }
 return version;
}

will output once used something like (here for the StringUtils.class available in the commons-lang-2.4.jar compiled on 15 march 2008 20:43) :

StringUtils $ commons-lang-2.4 $ 15/03/2008 20:43:16

Sergio