简体   繁体   English

DEBUG和RELEASE用Java(Eclipse)构建?

[英]DEBUG and RELEASE builds in Java (Eclipse)?

New to Java and Eclipse (but experienced with Visual Studio and Delphi). Java和Eclipse的新手(但在Visual Studio和Delphi方面经验丰富)。 Using Eclipse Mars (4.5) and cannot find how to set the build configuration (DEBUG or RELEASE). 使用Eclipse Mars(4.5)并且无法找到如何设置构建配置(DEBUG或RELEASE)。 Couple of related questions: 几个相关问题:

  • Are DEBUG/RELEASE supported in Java? Java中是否支持DEBUG / RELEASE?
  • How do you switch between the two configurations? 如何在两种配置之间切换?
  • Can you detect the configurations at build time and run conditional code like this (using Delphi as an example): {$IFDEF DBG} CallDebugFunction(); {$ELSE} CallReleaseFunction(); {$ENDIF}; 你能在构建时检测配置并运行这样的条件代码(以Delphi为例): {$IFDEF DBG} CallDebugFunction(); {$ELSE} CallReleaseFunction(); {$ENDIF}; {$IFDEF DBG} CallDebugFunction(); {$ELSE} CallReleaseFunction(); {$ENDIF};

DEBUG/RELEASE are not exactly supported in java. java中并不完全支持DEBUG / RELEASE。 But there are a few facts that are useful to remember, and a few other ways of accomplishing parts of the same thing. 但是有一些事实是有用的记忆,还有一些其他方法来完成同一件事的一部分。

The creators of java decided that there is great merit in having every single compilation unit producing the exact same bytecode regardless of external factors, so Java does not have a pre-processor. java的创建者认为,无论外部因素如何,每个编译单元产生完全相同的字节码都有很大的优点,因此Java没有预处理器。

The closest thing that java has to the kind of DEBUG/RELEASE that you are familiar with is the assert keyword. java最接近您熟悉的DEBUG / RELEASE类型是assert关键字。 You can control whether assertions will be evaluated by supplying an -assertionsenabled ( -ea for short) parameter to the VM. 您可以通过向VM提供-assertionsenabled (简称-ea )参数来控制是否评估断言。 Read up on it, and also read up on how to pass parameters to the VM. 阅读它,并阅读有关如何将参数传递给VM的信息。

Note that VM parameters are runtime parameters, they have nothing to do with the compiler, which means that assertions will always be emitted by the compiler into the bytecode, and if you do not supply -ea , then the runtime will refrain from evaluating them. 请注意,VM参数是运行时参数,它们与编译器无关,这意味着编译器将始终将断言发送到字节码中,如果不提供-ea ,则运行时将避免对它们进行评估。 So, at the very least there will still always be a hidden if( assertionsEnabled ) { ... } statement to execute for every assertion. 因此,至少仍然会有一个隐藏的if( assertionsEnabled ) { ... }语句来执行每个断言。

Another thing worth remembering is that public static final variables are treated as compile-time constants, and the compiler may avoid emitting any bytecode for source code controlled by an if( false ) clause. 另一件事值得记住的是, public static final变量被当作编译时间常数, 并且编译器可避免发射用于由受控源代码中的任何字节码if( false )子句。 However, the source code will always be compiled, so it has to be correct, despite the fact that no bytecode will be generated. 但是,源代码将始终被编译,因此它必须是正确的,尽管事实上不会生成字节码。

So, you can have a global variable defined as public static final boolean DEBUG = true to control all your debugging code, but you will have to manually change it in the source code and rebuild your project in order to produce a release build; 因此,您可以将全局变量定义为public static final boolean DEBUG = true来控制所有调试代码,但您必须在源代码中手动更改它并重建项目以生成发布版本; java specifically refrains from providing any other way of accomplishing this. java特别避免提供任何其他方法来实现这一点。

Also, note that if( false ) (and by extension if( DEBUG ) ) will produce a warning about the condition being always true or always false, so I don't like using it. 另外,请注意if( false ) (以及扩展if( DEBUG ) )将产生关于条件始终为true或始终为false的警告,因此我不喜欢使用它。

The philosophy of java is that we generally do not care about performance to such a paranoid degree as to want to have total control over tiny little performance differences between DEBUG and RELEASE. java的哲学是我们通常不关心这种偏执程度的性能,以至于想要完全控制DEBUG和RELEASE之间微小的性能差异。 Generally, the assert keyword is all that's needed, and actually (to my dismay) most java people do not even use assert due to various (lame, IMHO) reasons. 一般来说, assert关键字就是所需要的,实际上(令我沮丧的是)大多数java人甚至不会因为各种(跛脚,恕我直言)原因而使用断言。

As for emitting debugging information, the vast majority of debugging info is generated anyway, because it has to be available at runtime through reflection. 至于发出调试信息,绝大多数调试信息都是生成的,因为它必须在运行时通过反射可用。 There is one tiny thing that I am aware of which you can control: the -parameters compiler option, but it is really insignificant, and it is possible that future versions of the compiler will deprecate the option and include the functionality that it controls as standard behavior. 我知道你可以控制的一件小事: -parameters编译器选项,但它实际上是无关紧要的,编译器的未来版本可能会弃用该选项并包含它作为标准控制的功能行为。

This also means that java code can be reverse-engineered much more easily than C++ code, and for this reason there exist java obfuscators which pass the code through an identifier-mangling phase before sending it to the java compiler, so as to reduce the amount of useful information in the bytecode files. 这也意味着java代码可以比C ++代码更容易进行逆向工程,因此存在java混淆器,它们在将代码发送到java编译器之前通过标识符修改阶段传递代码,从而减少数量字节码文件中的有用信息。

You might be glad to know that this is all not too bad due to JITting: the bytecode gets compiled into machine code by the VM, and many optimizations are carried out at that stage, so you benefit from them always, not just on RELEASE. 您可能很高兴知道由于JITting这一切都不是太糟糕:VM将字节码编译成机器代码,并且在该阶段进行了许多优化,因此您始终可以从中受益,而不仅仅是在RELEASE上。

As for detecting whether assertions are enabled, you can use the following code: 至于检测断言是否已启用,您可以使用以下代码:

static boolean areAssertionsEnabled()
{
    //noinspection UnusedAssignment
    boolean b = false;
    //noinspection ConstantConditions,AssertWithSideEffects
    assert b = true;
    //noinspection ConstantConditions
    return b;
}

The noinspection thingies are for suppressing warnings in IntelliJ IDEA, a java IDE which is far superior to Eclipse. noinspection检测的东西用于抑制IntelliJ IDEA中的警告,这是一个远远优于Eclipse的Java IDE。 If you insist on using Eclipse you will have to find the equivalent warning suppression mechanisms for it. 如果您坚持使用Eclipse,则必须为其找到等效的警告抑制机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM