Revised answer
Assumptions: NetBeans version 6.9.1 (although likely applicable to most or all 6.x versions), alternative build systems (e.g., Maven) are not used...the default (Ant) is used.
NetBeans, by default, uses Ant as its build system for doing things like compiling a project, building a project, cleaning built files from a project, etc. Ant has two concepts that are applicable here: targets and tasks. A target, in Ant's vocabulary, is simply a "command" or a series of jobs that need to be completed for a particular job. In NetBeans, common targets are "Compile", "Build", "Clean and Build", etc. The "jobs" that a target completes are (among other things) Ant tasks. In NetBeans one task (which is particularly relevant in answering this question) is the Javac Task. This is the task that Ant uses to compile .java
files into .class
files.
An Ant-based project, and therefore a NetBeans project, uses the file build.xml
to control the build process and tell Ant how to go about accomplishing the targets. In a NetBeans project, the build.xml
is found in the root directory of the project, by default. NetBeans, however, uses a user-extensible build.xml
file. The core targets and tasks defined by NetBeans are actually located in nbprojects/build-impl.xml
and imported into build.xml
within the first few lines of the file. The theory is that users can add or override things in build.xml
while the core NetBeans-defined configuration remains untouched in the build-impl.xml
file.
If you look in the default nbproject/build-impl.xml
file for a NetBeans Java project, you will find the Javac task referred to twice. (Search for "<javac
".) Both are in macro definitions, and therefore deep within the complexities of NetBean's default build configuration. If we refer to the Javac Task documentation we find that the tasks uses the compiler in the location specified either by the global build.compiler
property, by the compiler
attribute specified with the <javac... />
task, or the default which is the Java compiler that is used when running and, and thus the one that is used when running NetBeans (because it is what fires off the Ant process). Since we don't see build.compiler
or the compiler
attribute anywhere (in the default build-impl.xml
), then we can only conclude that the default is being used.
So here we have the (more-or-less correct) first answer. NetBeans compiles using the JDK that was used to execute NetBeans by default. It looks like it is actually a bit more complicated than that simple answer, but it is essentially correct. If you look at the documentation for the Javac Task it alludes to "a class that implements the CompilerAdapter interface", which suggests that rather than calling the javac executable directly, Ant (and therefore NetBeans) compiles using the compiler class (that, in all likelihood, the javac executable also uses). Refer to the Original answer below to determine which JDK what used to run NetBeans.
So, what if you don't want to use the default JDK that was used to run NetBeans? This is where "Java Platforms" comes in. Go to the Tools menu, and click on "Java Platforms". You likely only have one platform defined here. (As an aside, this is actually the most correct answer to what JDK is used by default... the one defined here in the Java Platform Manager.) If you would like to compile against another Java version (say your default JDK is 1.6, but you want to compile against 1.5) then you would install the alternate JDK somewhere on your system, and then configure a platform here in NetBeans' Java Platform Manager. (I'll leave it as an exercise for you to find the documentation on how to add a Java Platform. A superficial search of the wiki didn't turn up anything obvious. In any case, it's fairly self-explanatory.)
Once a new platform is created in the manager, you would right-click on your project in the Projects tab, click on "Properties", and then on "Libraries". At the top, you would select the appropriate Java platform for the project. As soon as you change this value and click on "OK", NetBeans makes several adjustments to your build-impl.xml
file that point it to the new JDK against which to compile. (It is instructive for the truly geeky amongst us to make a copy of the nbproject
directory before making this change and to diff
that against the new contents of the nbproject
directory after the change is made.) The changes instruct the Javac Ant Task to use the (equivalent of the) javac executable of the specified platform. So here we have the most correct answer: NetBeans uses the equivalent of the javac executable (as invoked by the Ant javac task) that is specified in the project's Java Platform located under the Libraries node of the project's properties.
Original answer
The path to the JDK used by NetBeans can be found in the netbeans.conf
file. Look for the netbeans_jdkhome
entry.
You can also specify the jdkhome at runtime (*NIX example given):
netbeans --jdkhome /usr/bin/jdk1.6.0_22
The netbeans.conf
file is found in different places depending on what OS you are using. See the NetBeans.conf FAQ on the NetBeans wiki for help finding the file.
A few additional comments...
...You can specify the -target option in the project properties. In NetBeans 6.9 right-click on the project, and choose Properties. Click on the Compiling node. Add your -target to Additional Compiler Options.
...I have read in a few places that specifying a target is not a guarantee that the code will run on a JRE whose version is lower than the JDK that built it. In other words, the recommendation seems to be that if you want 1.5 binaries, then compile with the 1.5 JDK.
javac
at all! It seems to do it from some API calls, I guess. – Brawley-version
in Project/Properties/Build-Compiling/"Additional Compiler Options" and see which version is printed out during clean+build. – Brawleyjavac
is used, but I don't understand it: hg.openjdk.java.net/mlvm/mlvm/file/4436cde7337c/netbeans/… – Brawley/Users/joe/.netbeans/6.9/var/log
(where 6.9 is replaced with your version number). See Where do I find the NetBeans log file to attach to the error report? on the NetBeans Wiki. – Lorenza