This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
In C and C++ compilers there are often preprocessor commands. I am trying to duplicate some of the functionality I'm used to in other languages. Examples of things I would like to do - - Be able to reference the build/compile time in my code - Same as above but for the date - Reference the line number the compiler is at I know there are others, but I can't think of specifics right now. Some pseudo code in C/C++ demonstrating the above might be
Where __CTIME__ and #COMP_TIME are the compile time parameters. Any thoughts?
Of course, u can use java.util.GregorianCalendar class to do things with the Time and Date stuff... Java does have java.lang.Runtime class to deal with the low level commands as well... But there is a limitation on how low Java can dig into the system...
I realize I can do date and time things, but the idea here is that the compiler is replacing, statically, those variable names. If today was Monday, 11 Jan 2004, then the __CTIME__ would be replaced during the compile only with that value. Its nothing that happens at runtime per se. Its not something which is done very often, but when you need it, its handy to have.
Ko Ko Naing
Joined: Jun 08, 2002
I guess u might have to use Java Native Interface to apply the C/C++ native variables in the program... I'm not that sure... But there are experts in the Other Java APIs forum who can help u in dealing with those native stuff...
As Java has no preparser such constructs are not available. In C(++) the construct you mention would technically NOT give you the compilation time but the time the preparser was run at. This does not have to be the compilation time (it is possible to run only the preparser, and in large projects or on slow computers it is quite possible that there is a considerable (in machine terms) gap of several seconds if not longer between the preparser running and the compiler running.
We are using Ant to do this. Basically, we do have a template file named Version.src with some java code declaring constants and placeholders for the actual values. The Ant script will copy the file to a Version.java file, thereby replacing the placeholders with actual values (using filters). Than the code gets compiled and other classes are able to refer to the constants with the current values. Did that help?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus