简体   繁体   English

如何在 C 和 JAVA 中的 main() 之前执行特定函数?

[英]How to execute a particular function before main() in C and JAVA ?

I want to execute one function before main function in C and JAVA language.我想在 C 和 JAVA 语言中的 main 函数之前执行一个函数。 I know one way that is, by using #pragma directive in C language.我知道一种方法,即在 C 语言中使用#pragma指令。 Is there any other way to do that in both languages?有没有其他方法可以用两种语言做到这一点?

I can think of two simple(-ish) ways to do it in Java: 我可以想到在Java中使用两种简单的(-ish)方法:

Method #1 - static initializers 方法#1 - 静态初始化器

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. 方法#2 - 代理主。

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. 基本上,您必须创建自己的JVM启动程序,该启动程序使用不同的方案来启动应用程序。 Have this do the extra stuff before loading the entry point class and calling its main method. 在加载入口点类并调用其main方法之前,请执行此操作。 (Or do something different entirely.) (或者做一些完全不同的事情。)

You could even replace the default classloader; 你甚至可以替换默认的类加载器; eg How to change default class loader in Java? 例如, 如何在Java中更改默认类加载器?

in java you can use static block 在java中你可以使用静态块

public class JavaApplication2 {

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

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

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

As an extension to the C standard gcc provides the function attribute constructor which allows functions to be called before main() . 作为C标准的扩展,gcc提供了函数属性constructor函数,它允许在main()之前调用函数。

For details please see here (scroll down). 有关详细信息,请参阅此处 (向下滚动)。 Also this SO question and its answers help on this. 此SO问题及其答案也有助于此。

It could be the first thing you call from your main function. 这可能是您从主要功能调用的第一件事。 That way it will run before your "real" main function. 这样它就会在你的“真正的”主要功能之前运行。

Use that method(what you want to run before main method) as your main method. 使用该方法(您希望在main方法之前运行的方法)作为主要方法。

Then it is so simple 然后就这么简单了

Or use static block before main() 或者在main()之前使用static

In java, execution of any other method is possible before main() method execution.在 java 中,可以在main()方法执行之前执行任何其他方法。 We need a static initializer block for that.为此,我们需要一个静态初始化块。 It starts with a static keyword.它以 static 关键字开头。 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.当我们开始执行这段代码时,静态初始化块将首先开始执行,在静态初始化块中,我们调用add()方法并将值存储在静态变量中,并打印它。 Next, we are calling the main() method, which will execute.接下来,我们将调用将执行的main()方法。 when the static initializer block execution will complete JVM will call the main() method again.当静态初始化块执行完成时,JVM 将再次调用main()方法。

Output Output of the above code输出上述代码的输出

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

相关问题 Websphere崩溃之前,如何在Java中执行特定方法? - How to execute a particular method in java before websphere goes down? 我们如何才能在 java 的 main 方法中只执行一次特定的代码行? - How can we execute particular line of code only once in main method in java? 当看到正在执行的Java项目中的某个功能时,如何使单元测试每次执行特定的测试用例? - How to make the unit test execute a particular test case everytime when it sees a certain function in the executing java project? Java&MySql-如何在执行查询前对其进行转义 - Java & MySql - How to escape query before execute it 部署前如何执行Java程序 - how to execute java program before deployment java的this()构造函数重载,如何在this()之前执行代码 - java Constructor overload with this(), how to execute code before this() 如何调用java主函数? - How to call a java main function? 有没有办法在Java中的类的所有方法之前和之后始终执行一个函数? - Is there a way to always execute a function before and after all methods of a class in Java? 在Java(Android)中在运行前设置变量/执行函数? - Set variable/execute function before runtime in java(android)? 在 java 中执行相同 class 的“每个之前”方法 function - Execute a function "before each" method of the same class in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM