Debug / Standard Build a Java Application

Greetings

I use embedded java, I am writing an application for a device with a low resource. One of the problems I get is when the code interrupts the device. I can get some information from him with some login details that I added. (Simple print instructions)

This log cannot remain in the application due to memory limitations.

I was wondering if anyone knows if there is a flag that I can pass to the JVM to indicate if it needs debugging or standard compilation.

I would like print statements to be included if their debugging assembly, if it is a standard assembly to remove print statements. I ask because I have to constantly cut and paste statements, etc. (It is a pain)

thanks

+3
source share
3 answers

You can use the fact that it is if (constant)optimized by the compiler.

Make a global variable somewhere called DEBUG:

public static final boolean DEBUG = true;

and then register as follows:

if (DEBUG) {
    System.out.println("Debug");
}

To disable debugging, simply change DEBUGto falseand all logging instructions will be optimized by the compiler. You can verify this by looking at the generated bytecode with javap -c.

For example:

class DebugTest {
    public static final boolean DEBUG = true;
    public static void main(String[] args) {
        if (DEBUG) {
            int a = 10;
            System.out.println("a = " + a);
        }
    }
}

compiled as:

Compiled from "DebugTest.java"
class DebugTest extends java.lang.Object {
public static final boolean DEBUG;

DebugTest ();
  Code:
   0: aload_0
   1: invokespecial # 1; // Method java / lang / Object. "" :() V
   4: return

public static void main(java.lang.String[]);
  Code:
   0:   bipush  10
   2:   istore_1
   3:   getstatic   #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   6:   new #3; //class java/lang/StringBuilder
   9:   dup
   10:  invokespecial   #4; //Method java/lang/StringBuilder."":()V
   13:  ldc #5; //String a = 
   15:  invokevirtual   #6; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   18:  iload_1
   19:  invokevirtual   #7; //Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
   22:  invokevirtual   #8; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
   25:  invokevirtual   #9; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   28:  return

}

DEBUG false :

Compiled from "DebugTest.java"
class DebugTest extends java.lang.Object{
public static final boolean DEBUG;

DebugTest();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."":()V
   4:   return

public static void main(java.lang.String[]);
  Code:
   0:   return

}
+8

, , .

. , cpp.

, javac , ,

private static final DEBUG = false;

if(DEBUG) System.err.println("Entered");

, , - .

+1

, , ?

, if-check, .

No matter what you do, do not continue to copy / paste your code into the project and from it automate this manual work. Save yourself.

+1
source

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


All Articles