4

I want to execute one function before main function in C and JAVA language. I know one way that is, by using #pragma directive in C language. Is there any other way to do that in both languages?

0decimal0
  • 3,884
  • 2
  • 24
  • 39
New_User
  • 395
  • 1
  • 4
  • 7

7 Answers7

7

I can think of two simple(-ish) ways to do it in Java:

Method #1 - static initializers

For example:

public class Test {
    static {
        System.err.println("Static initializer first");
    }

    public static void main(String[] args) {
        System.err.println("In main");
    }
}

Method #2 - A proxy main.

public class ProxyMain {
    public static void main(String[] args) {
        String classname = args[0];
        // Do some other stuff.
        // Load the class
        // Reflectively find the 'public static void main(String[])' method
        // Reflectively invoke it with the rest of the args.
    }
}

You then launch this as:

java <jvm-options> ... ProxyMain <real-Main> arg ...

There is also a 3rd method, but it requires some "extreme measures". Basically you have to create your own JVM launcher that uses a different scheme for starting the application. Have this do the extra stuff before loading the entry point class and calling its main method. (Or do something different entirely.)

You could even replace the default classloader; e.g. How to change default class loader in Java?

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
5

in java you can use static block

public class JavaApplication2 {

    static {
        System.out.println("in static ");
    }

    public static void main(String[] args) {
        System.out.println("in main ");
    }
}
johnchen902
  • 9,531
  • 1
  • 27
  • 69
shreyansh jogi
  • 2,082
  • 12
  • 20
1

Try combining a static block and a static method containing what you want executed before your main method.

package test;

public class Main {
    static {
        beforeMain();
    }
    public static void main(String[] args) {
        System.out.println("after");
    }
    private static void beforeMain() {
        System.out.println("before");
    }
}

Output:

before
after
johnchen902
  • 9,531
  • 1
  • 27
  • 69
Mena
  • 47,782
  • 11
  • 87
  • 106
1

As an extension to the C standard gcc provides the function attribute constructor which allows functions to be called before main().

For details please see here (scroll down). Also this SO question and its answers help on this.

Community
  • 1
  • 1
alk
  • 69,737
  • 10
  • 105
  • 255
0

It could be the first thing you call from your main function. That way it will run before your "real" main function.

Goatcat
  • 1,133
  • 2
  • 14
  • 31
0

Use that method(what you want to run before main method) as your main method.

Then it is so simple

Or use static block before main()

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

In java, execution of any other method is possible before main() method execution. We need a static initializer block for that. It starts with a static keyword. Like:

static {
    // statements
}

Now try to understand with actual code implementation.

public class BefourMain {
    
    static int  sum;
    
    static {
        sum = add(4,5,6);
        System.out.println("Calling add() method: " + sum);
        System.out.println("\ncalling main");
        main(null);
        System.out.println("Main end\n");
        System.out.println("Now JVM calling main()\n");
    }
    

    
    public static void main(String[] args) {

        int sum = add(1,2,3);
        System.out.println(sum);

    }
    
    static int add(int a, int b, int c) {
        return a + b + c;
    }
}

Now try to understand with actual code implementation.

When we start executing this code the static initializer block will start execution first, in the static initializer block we are calling the add() method and storing the value in the static variable, and printing it. Next, we are calling the main() method, which will execute. when the static initializer block execution will complete JVM will call the main() method again.

Output Output of the above code