Can javac do macro (conditional) compilation?

R

RC

In C/C++ we can do

for example:

#ifdef VERSION_1
// write version 1 codes here
....
#else
// write version 2 codes here
....
#endif



Then when we compile the code with -D option

gcc -DVERSION_1 input_filename.c -o output_filename
This will compile the codes in version 1

gcc input_filename.c -o output_filename
This will compile the codes in version 2

I am just wonder can I use -J option in javac?
If I can, then how do I write the syntax in java codes?
 
O

Owen Jacobson

In C/C++ we can do

for example:

#ifdef VERSION_1
// write version 1 codes here
...
#else
// write version 2 codes here
...
#endif

Then when we compile the code with -D option

gcc -DVERSION_1 input_filename.c -o output_filename
This will compile the codes in version 1

gcc input_filename.c -o output_filename
This will compile the codes in version 2

I am just wonder can I use -J option in javac?
If I can, then how do I write the syntax in java codes?

No. However, as I understand it, final static constant-expression
booleans can be used to eliminate code inside if statements at compile
time.

If you absolutely need a macro preprocessor you have to invoke it
yourself as part of your build, and then feed its output to javac.
Think very hard before doing this.
 
L

Lew

Java is neither.

Owen said:
No. However, as I understand it, final static constant-expression
booleans can be used to eliminate code inside if statements at compile
time.

Why would one need to? Why not just use regular "if" constructs?
If you absolutely need a macro preprocessor you have to invoke it
yourself as part of your build, and then feed its output to javac.
Think very hard before doing this ...

then don't.

What would be the point? The primary use of conditional processing is to
account for different environments, a moot point with Java. Everything else
it's used for is more naturally expressed in Javaworld with properties.

This is the point of saying that Java is neither C nor C++. The whole idea of
conditional processing is irrelevant to Java.
 
R

Robert Klemme

Java is neither.




Why would one need to? Why not just use regular "if" constructs?

Or rather use interfaces with different implementations and make use of
polymorphism. I think that's the most used idiom in Java land.
then don't.

What would be the point? The primary use of conditional processing is
to account for different environments, a moot point with Java.
Everything else it's used for is more naturally expressed in Javaworld
with properties.

Another path that's often chosen in Java is using interfaces with
different implementations depending either on environment, configuration
(properties etc.) or runtime conditions.
This is the point of saying that Java is neither C nor C++. The whole
idea of conditional processing is irrelevant to Java.

Yeah, stop thinking in C(++).

Kind regards

robert
 
O

Owen Jacobson

In C/C++ we can do

for example:

#ifdef VERSION_1
// write version 1 codes here
...
#else
// write version 2 codes here
...
#endif

<offtopic>
To add to my earlier comments, you can do that in C and C++, but you
shouldn't. It misleads most programmers into thinking they can still
build "VERSION 1" of their software when, in fact, they can't. Not
from source code that is not what was originally released as version
1.

If behaviour has to change between versions, *remove the old
behaviour* and replace it with the new one. If you discover a need to
provide both behaviours, it will either be in the same build (in which
case that preprocessor trick won't work) or it won't (in which case
you can check out the version 1 source code and build that).
</offtopic>
 
R

RC

I agree with you. The reason I came up this idea is
I have two versions of code. I want to do a demo to the
users. An user can open a window to run two programs.
I want to see which version they prefer/like. Then in
the final build, only one version will be check in.
 
L

Lew

RC said:
I agree with you. The reason I came up this idea is
I have two versions of code. I want to do a demo to the
users. An user can open a window to run two programs.
I want to see which version they prefer/like. Then in
the final build, only one version will be check in.

Use what Robert Klemme suggested:
Another path that's often chosen in Java is using interfaces with different implementations depending either on environment, configuration (properties etc.) or runtime conditions.

Polymorphism rules.
 
R

Roedy Green

In C/C++ we can do

for example:

#ifdef VERSION_1
// write version 1 codes here
...
#else
// write version 2 codes here
...
#endif

1. the C++ precompiler works fine on Java. That is how we handled it
at Athena/Integer back in the 90s.

2. The compiler is clever so ifs get optimised out of existence if you
insert different static final configuration constants with COPY to
some configuration class prior to compile. Have a look at how I do
that at
http://mindprod.com/products1.html#BULK where I copy in a
configuration file onto CustConfig.java prior to compile.

3. there are preprocessors.
See http://mindprod.com/jgloss/preprocessor.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top