How do I add a compiler flag in Eclipse for a Java Program?

I’m doing a Coursera course that require you to submit programming assignments.

The specification says the following.

We recommend that when testing your solution locally, you use the same
compiler flags for compiling. This will increase the chances that your
program behaves in the same way on your machine and on the testing
machine (note that a buggy program may behave differently when
compiled by different compilers, or even by the same compiler with
different flags).

javac -encoding UTF-8
java -Xmx1024m

I read many answers online instructing how to set flags for C++/C code.

Go to Project > Properties > C/C++ Build > Settings > Cross G++ Compiler > Miscellaneous


But I couldn’t find a similar option (eg – Java Build) in Java.

How do I add this flag when compiling the program.

I’m using Neon version of eclipse.


Eclipse uses its own compiler, not javac; and I don’t think it accepts command line flags the way javac does. But there are many compiler options, which you can find under Window | Preferences | Java | Compiler


To pass arguments to java at run time, click the drop-down triangle next to the Run button on the toolbar (the one that looks like a white triangle inside a green circle). Go down to “Run Configurations”. Here, you can set up a number of options for when you run your program. In particular, flags such as -Xmx1024m can be entered in the “VM Arguments” box on the “Arguments” tab.

As Dawood said, Eclipse use its own compiler, for the difference between javac, please check the thread What is the difference between javac and the Eclipse compiler?

Besides that, I’d like to mention that you must make sure that the java version of your dev environment and execution environment is the same.
For example, if you use switch(String) statement, it works fine with java 1.7 or above but will never work in java 1.6 or below.

The Eclipse Java compiler takes the encoding from the properties of the Java file in the workspace.

Right click on the file and open the Properties. Select the ‘Resource’ tab and look at the ‘Text file encoding’ section. Set this to UTF-8 if necessary.

You can also set the default text file encoding for the whole workspace in the Preference on the ‘General > Workspace’ page.