Setting up Eclipse for Java

03 Dec 2018 - written by Hardy Scheel

Eclipse uses its own integrated Java compiler. You do not need to set up a JDK for Eclipse. It is only necessary to set up a Java JRE to run applications you write.

Every Eclipse workspace needs its own settings. Projects in this workspace inherit these workspace settings. But you can set up every project on its own.

Table of content


Add JRE api documentation and source code to use it within Eclipse

To have a look at the original JRE library source code and to quick access the Javadoc documentation of the JRE libraries do the following steps.

Under preferences -> Java -> Installed JREs choose your JRE and click on edit.

eclipse-jre-doc-sources-1

Mark the highlighted libraries and click on Javadoc Location.

eclipse-jre-doc-sources-2

Choose the location of the JDK-doc documentation archive file, browse within the archive to the folder called api and click validate to proof if Eclipse can find the entry point of the documentation.

eclipse-jre-doc-sources-3

Select the location of the JDK-doc file. Mine is within my workspace in a project called JavaSE8 resources.

eclipse-jre-doc-sources-4

Now lets attach the source code to this libraries.

eclipse-jre-doc-sources-5

My JRE api source code archive is within my workspace.

eclipse-jre-doc-sources-6


Add JavaFX api documentation and source code to use it within Eclipse

For JavaFX do the same steps as above but now add docs and sources for the highlighted jfxrt.jar library.

eclipse-jre-doc-sources-7

Choose the JavaFX api doc archive and navigate to the folder api. Then validate to make Eclipse can find the entry point for the documentation.

eclipse-jre-doc-sources-8

At last select source attachment to add the JavaFX source archive.

eclipse-jre-doc-sources-9

Choose the path to your JavaFX source archive. Mine is within my workspace in a special project called JavaSE8 resources.

eclipse-jre-doc-sources-10


Eclipse and the JavaFX 8 library access restriction bug

Your situation: You want to create a JavaFX GUI with JRE 1.8. And in Eclipse a JavaFX import is not possible.

Problem explanation: JavaFX is not part of the base Java SE 8 API. Because of Eclipse strictness, you can’t access it. The bug is: JavaFX is already in Java SE 8 standard library. You can’t import it a second time.

Hint: Eclipse has its own Java compiler. A dedicated JDK is not necessary when working with Eclipse. The Java JRE you have installed on your computer causes the problem. The JRE runs after you have compiled your app with the build in Eclipse compiler. Eclipse also uses the JRE to check dependencies and much more.

Your error message could look like this:

Access restriction: The type 'Application' is not API (restriction on required library 'C:\Program Files\Java\jre1.8.0_191\lib\ext\jfxrt.jar')

eclipse-javafx8-access-restriction-bug-1

Some solutions don’t work:

Workaround: The fastest workaround (but no solution) is to set a loosen access rule to your build path:

In your project properties -> Java Build Path -> Libraries: edit the 'Access rules' for your current JRE.

eclipse-javafx8-access-restriction-bug-2

Add: javafx/** as an accessible rule.

eclipse-javafx8-access-restriction-bug-3

After adding the accessible rule.

eclipse-javafx8-access-restriction-bug-4

Alternate Solution: You can also use an “Alternate JRE” as system library, to loosen this restriction.

Resources for further reading