Netbeans使用什么javac? [英] What javac is Netbeans using?

查看:117
本文介绍了Netbeans使用什么javac?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作项目需要编译并在JDK1.5下运行,并且我在Mac上.我按照此处的说明进行操作1.5回到Snow Leopard上,并且从IntelliJ IDEA进行构建时工作正常,或者如果我与build.xml位于同一目录中,并尝试使用"ant CleanRebuild",则当我在NetBeans中运行目标"时,它们都已编译版本错误导致

java.lang.UnsupportedClassVersionError: .class文件中的版本号错误 (无法加载课程...

tomcat尝试启动时.

所以我尝试过的事情

  1. 在项目属性/库下将"Java平台"设置为1.5.
  2. 在项目属性/源下,将源/二进制格式"设置为JDK 5.
  3. 将蚂蚁家"指向我在偏好设置"/"ant"下使用的蚂蚁
  4. 重命名我可以找到的每个Javac可执行文件,希望它们不会被NetBeans编译,并且我可以弄清楚它使用的是哪个(不走运)
  5. 将1.5设置为默认值,导致需要将$ netbeans_jdkhome指向1.6 jdk,以使NetBeans甚至启动.

全部失败....

同样,如果我使用build.xml进入netbeans项目的目录并手动运行命令,那么一切都很好.怎么了?

解决方案

修订后的答案

假设:NetBeans 6.9.1版(虽然可能适用于大多数或所有6.x版本),但未使用替代构建系统(例如Maven)...默认值(Ant)使用.

默认情况下,NetBeans使用 Ant 作为其构建系统来执行诸如编译项目,构建数据库等操作.项目,清除项目中的已构建文件等.Ant具有两个适用于此处的概念:目标和任务.在Ant的词汇表中,目标仅仅是一个命令"或一系列需要为特定工作完成的工作.在NetBeans中,常见的目标是编译",构建",清理和构建"等.目标完成的工作"是(除其他事项外)Ant 任务.在NetBeans中,一项任务(与回答该问题特别相关)是 Javac任务.这是Ant用来将.java文件编译为.class文件的任务.

基于Ant的项目(即NetBeans项目)使用文件build.xml控制构建过程,并告诉Ant如何完成目标.在NetBeans项目中,默认情况下在项目的根目录中找到build.xml.但是,NetBeans使用用户可扩展的build.xml文件. NetBeans定义的核心目标和任务实际上位于nbprojects/build-impl.xml中,并在文件的前几行中导入到build.xml中.从理论上讲,用户可以在build.xml中添加或覆盖内容,而build-impl.xml文件中的核心NetBeans定义的配置保持不变.

如果您查看NetBeans Java项目的默认nbproject/build-impl.xml文件,则会发现两次引用的Javac任务. (搜索"<javac".)两者都在宏定义中,因此深入到NetBean默认构建配置的复杂性内.如果我们参考 Javac任务文档,我们会发现这些任务在以下位置使用了编译器:由全局build.compiler属性,由<javac... />任务指定的compiler属性指定的位置,或默认位置(运行时使用的Java编译器,因此也是运行时使用的Java编译器) NetBeans(因为正是它触发了Ant进程).由于我们在任何地方(在默认的build-impl.xml中)都看不到build.compilercompiler属性,因此我们只能得出结论,认为正在使用默认值.

因此,这里有(或多或少正确的)第一个答案. NetBeans使用默认情况下用于执行NetBeans的JDK进行编译.看起来,它实际上比简单的答案要复杂一些,但从本质上讲是正确的.如果您查看Javac Task的文档,它暗指"实现CompilerAdapter接口的类",这表明Ant(以及NetBeans)不是直接调用javac可执行文件,而是使用编译器类(极有可能javac可执行文件也使用该类).请参阅下面的 原始答案 ,以确定用于运行NetBeans的JDK.

那么,如果您不想使用用于运行NetBeans的默认JDK怎么办?这是" Java平台"的出现位置.转到工具"菜单,然后单击"Java平台".您可能只在这里定义了一个平台. (顺便说一句,这实际上是默认情况下使用的JDK的最正确答案... Java平台管理器中此处定义的一个.)如果要针对另一个Java版本进行编译(例如,默认的JDK为1.6) ,但您想针对1.5进行编译,则可以在系统上的某个位置安装备用JDK,然后在NetBeans的Java Platform Manager中配置平台. (我将把它作为练习,让您找到有关如何添加Java平台的文档.对Wiki的肤浅搜索没有发现任何明显的现象.无论如何,这是不言自明的.)

一旦在管理器中创建了一个新平台,您将在项目"选项卡中右键单击您的项目,单击属性",然后单击库".在顶部,您将为项目选择适当的Java平台.更改此值并单击确定"后,NetBeans会对build-impl.xml文件进行几处调整,将其指向要针对其进行编译的新JDK. (这对我们当中真正的极客是有启发性的,在进行此更改之前先复制nbproject目录,并在进行更改后将diff复制到nbproject目录的新内容.)更改指示Javac Ant任务以使用(等于)指定平台的javac可执行文件.因此,这里我们有最正确的答案: NetBeans使用项目的 Java平台中指定的javac可执行文件(由Ant javac任务调用)的等价 >位于项目属性的 Libraries 节点下.

原始答案

netbeans.conf文件中可以找到NetBeans使用的JDK的路径.查找netbeans_jdkhome条目.

您还可以在运行时指定jdkhome(* NIX示例已给出):

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

根据所使用的操作系统,在不同位置可以找到netbeans.conf文件.请参阅NetBeans Wiki上的 NetBeans.conf常见问题解答,以获取查找文件的帮助.

一些其他评论...

...您可以在项目属性中指定-target选项.在NetBeans 6.9中,右键单击项目,然后选择属性".单击编译节点.将您的-target添加到其他编译器选项.

...我在一些地方读到,指定目标并不能保证代码将在版本低于构建它的JDK的JRE上运行.换句话说,建议似乎是如果要1.5二进制文件,则使用1.5 JDK进行编译.

My work 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 fine when building from IntelliJ IDEA, or if I'm just in the same directory as the build.xml and try "ant CleanRebuild" When I "Run Target" in NetBeans they're all compiled with the wrong version resulting in

java.lang.UnsupportedClassVersionError: Bad version number in .class file (unable to load class...

when tomcat is trying to start up.

So things I've tried

  1. Set the "Java Platform" to 1.5 under the project properties/libraries.
  2. Set the Source/Binary Format to JDK 5 under project properties/sources.
  3. Pointed the ant home to the ant I'm using under preferences/ant
  4. Renaming every javac executable I could find in the hopes NetBeans would fail to compile and I could figure out which one it was using (no luck)
  5. Setting 1.5 as the default, resulting in the need to point $netbeans_jdkhome to the 1.6 jdk in order for NetBeans to even start.

All unsuccessful....

Again, if I cd into the directory of the netbeans project with the build.xml and run the command manually all is well....so NetBeans. What's the deal?

解决方案

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.

这篇关于Netbeans使用什么javac?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆