Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java Language
Expertise: Beginner
Feb 1, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Generating a Compiler Warning

I am looking for a way to get the javac compiler to generate a warning message that I specify in my code when it is compiling my code. I use the #warning directive in gcc to tell me about things I need to come back to later and fix up, but there seem to be no compiler directives for javac. Is there any way around this?

Unlike C, Java has no preprocessor and cannot make use of preprocessor directives such as gcc's #warning. The closest you can come to achieving the same effect is to write a shell script that preprocesses your .java files. I would suggest using a unique comment like:

/*warning: Don't hard code the constant. */
int foobar = 5;
The shell script should scan through a .java looking for the specially formatted comments, and output their line numbers and contents. Perl would do nicely for this task. Then set up your build procedure to call your shell script immediately before invoking the Java compiler. That way you'll get your warnings at compile time. It's extremely klugey, but it appears to be the only way to do what you want with Java.
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date