Which javac is used by Netbeans?

My working project needs to be compiled and run under JDK1.5, and I'm on a Mac. I followed the instructions here to get 1.5 back on Snow Leopard, and it works great when creating from IntelliJ IDEA, or if I'm just in the same directory as build.xml, and try "ant CleanRebuild" When I Run the target "in NetBeans, they are all compiled with the wrong version, which leads to

java.lang.UnsupportedClassVersionError: Invalid version number in .class file (class cannot be loaded ...

when tomcat tries to start.

So what I tried

  • Install "Java Platform" in 1.5 in the project properties / libraries.
  • Set the source / binary format in JDK 5 in the project properties / sources.
  • Points ant to the ant main page, which I use in the / ant settings
  • Renaming each javac executable that I can find in the hope that NetBeans won't compile and I could figure out which one it used (no luck)
  • Installing 1.5 by default, so you need to point $ netbeans_jdkhome to 1.6 jdk for NetBeans to even start.

All unsuccessful ....

Again, if I cd into the netbeans project directory with build.xml and run the command manually, everything is fine ... so NetBeans. What a deal?

+4
source share
1 answer

Revised Answer

Assumptions: NetBeans version 6.9.1 (although it probably applies to most or all 6.x versions), alternative build systems (like Maven) are not used ... the default value (Ant) is used.

By default, NetBeans uses Ant as its build system to perform tasks such as compiling a project, creating a project, cleaning embedded files from a project, etc. Ant has two concepts that apply here: goals and objectives. A goal in the Ant dictionary is simply a “team” or a series of tasks that must be completed for a specific job. In NetBeans, common goals are "compilation", "build", "cleanup and build", etc. The "tasks" that complete the goal are (among other things) Ant tasks. In NetBeans, one task (which is especially important when answering this question) is the Javac Task . This is the task Ant uses to compile .java files to .class files.

The Ant-based project, and therefore the NetBeans project, uses the build.xml file to control the build process and tells Ant how to go about achieving the goals. In a NetBeans project, build.xml is located by default in the project root directory. However, NetBeans uses a user-expandable build.xml file. The main goals and objectives defined by NetBeans are actually located in nbprojects/build-impl.xml and are imported into build.xml within the first few lines of the file. The theory is that users can add or redefine things in build.xml , while the basic configuration defined by NetBeans remains untouched in the build-impl.xml .

If you look in the nbproject/build-impl.xml file for a Java NetBeans project, you will find the Javac task mentioned twice. (Search for " <javac ".) Both are in macro definitions and, therefore, deep in the complexity of the NetBean default build configuration. If we refer to the Javac Task documentation , we find that tasks use the compiler at the location indicated by the global build.compiler property, through the compiler specified in the <javac... /> task, or by default, which is the Java compiler used at startup, and therefore the one used to start NetBeans (since this is what works when Ant is processed), since we don’t see the build.compiler or compiler attribute somewhere (by default, build-impl.xml ), then we can only conclude that the default value is used th.

So, we have a (more or less correct) first answer. NetBeans compiles using the JDK, which was used to run NetBeans by default. It seems like this is actually a little more complicated than the simple answer, but essentially it is correct. If you look at the documentation for the Javac Task, it refers to a “class that implements the CompilerAdapter interface,” which assumes that instead of directly invoking the executable, javac Ant (and therefore NetBeans) is compiled using the compiler class (which, in all likelihood, javac executable also uses). Refer to the Original Answer below to determine which JDK is used to run NetBeans.

So what if you don't want to use the default JDK that was used to run NetBeans? Here you will find Java Platforms. Go to the Tools menu and click on Java Platforms. You probably have only one platform. (Aside, this is actually the most correct answer to the fact that the JDK is used by default ... the one defined here in the Java Platform Manager.) If you want to compile a different version of Java (let's say your default JDK is 1, 6, but you want to compile with 1.5), you must install an alternative JDK somewhere on your system, and then configure the platform here in NetBeans Java Platform Manager. (I will leave this as an exercise for you to find documentation on how to add the Java platform. The surface wiki search has not become something obvious. In any case, it is clear enough.)

As soon as a new platform is created in the manager, you should right-click your project in the Projects tab, click Properties, and then Libraries. At the top, you select the appropriate Java platform for the project. Once you change this value and click OK, NetBeans will make a few changes to your build-impl.xml , which points to the new JDK with which it is being compiled. (It is instructive that it is truly geeky among us to make a copy of the nbproject directory before making this change, and in diff , which is against the new contents of the nbproject directory after making the changes.) Instructions for changing the Javac Ant The task is to use the (equivalent) javac executable of the specified platform. So, we have the most correct answer: NetBeans uses the equivalent of the javac executable (as called by the Ant javac task), which is specified in the Java platform of the project located in the node's library of project properties.

Original answer

The JDK path used by NetBeans can be found in the netbeans.conf file. Find the netbeans_jdkhome entry.

You can also tell jdkhome at runtime (NIX example provided):

 netbeans --jdkhome /usr/bin/jdk1.6.0_22 

The netbeans.conf file is located in different places depending on which OS you are using. See the NetBeans.conf FAQ on the NetBeans wiki for a file.

A few additional comments ...

... You can specify the -target parameter in the project properties. In NetBeans 6.9, right-click the project and select Properties. Click Compile node. Add your -target to the advanced compiler options.

... I read in several places that specifying a goal 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 to use 1.5 binaries, then compile them using the 1.5 JDK.

+4
source

Source: https://habr.com/ru/post/899892/


All Articles