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?
-
2http://stackoverflow.com/questions/3379190/avoiding-the-main-entry-point-in-a-c-program – Ram Jun 27 '13 at 08:02
-
C subset https://stackoverflow.com/questions/8713470/executing-code-before-main – Ciro Santilli OurBigBook.com Oct 03 '17 at 08:12
7 Answers
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?
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 ");
}
}

- 9,531
- 1
- 27
- 69

- 2,082
- 12
- 20
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

- 9,531
- 1
- 27
- 69

- 47,782
- 11
- 87
- 106
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.
It could be the first thing you call from your main function. That way it will run before your "real" main function.

- 1,133
- 2
- 14
- 31
-
1but in this case program is running through main(). i don't want that. – New_User Jun 27 '13 at 08:04
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()

- 34,993
- 17
- 75
- 115
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

- 1
- 1
- 2