This question already has an answer here:
这个问题已经有了答案:
- How do Java programs run without defining the main method? 12 answers
- Java程序如何在不定义主方法的情况下运行?12个答案
- Printing message on Console without using main() method 10 answers
- 不使用main()方法10在控制台打印消息
According to my knowledge we cannot execute without a main method because when your running the java program. java Virtual machine look for the main method .if JVM could not find the main method it will show you run time error Exception in thread main could not find the main class.
据我所知,没有主方法我们无法执行,因为当您运行java程序时。java虚拟机查找主方法,如果JVM找不到主方法,它会显示在thread main中运行时间错误异常找不到主类。
But I searched for many blogs and some of them are saying, yes it can be done through static blogs if there is a static block in a java program,the class loader loads all static block before java gives call to main() method!.
但是我搜索了很多博客,其中一些人说,是的,如果java程序中有一个静态块,那么可以通过静态博客完成,类加载器在java调用main()方法之前加载所有静态块!
Is it really possible and what will be the usability of these.
这真的有可能吗?这些东西的可用性是什么?
4 个解决方案
#1
21
Now - no
现在,没有
Prior to Java 7:
Java 7之前:
Yes, sequence is as follows:
是的,序列如下:
- jvm loads class
- jvm加载类
- executes static blocks
- 执行静态块
- looks for main method and invokes it
- 查找主方法并调用它
So, if there's code in a static block, it will be executed. But there's no point in doing that.
因此,如果静态块中有代码,它将被执行。但这样做没有意义。
How to test that:
如何测试:
public final class Test {
static {
System.out.println("FOO");
}
}
Then if you try to run the class (either form command line with java Test
or with an IDE), the result is:
然后,如果您尝试运行类(使用java测试或IDE生成命令行),结果是:
FOO
java.lang.NoSuchMethodError: mainFOO . lang。NoSuchMethodError:主要
#2
2
since you tagged Java-ee as well - then YES it is possible.
既然你也标记了java ee,那么这是可能的。
and in core java as well it is possible using static blocks
在core java中也可以使用静态块
and check this How can you run a Java program without main method?
检查一下,没有主方法怎么运行Java程序?
Edit:
as already pointed out in other answers - it is not support in Java 7
编辑:正如在其他答案中已经指出的,它不支持Java 7
#3
2
Yes You can compile and execute without main method By using static block. But after static block executed (printed) you will get an error saying no main method found.
是的,通过使用静态块,您可以不使用主方法编译和执行。但是在执行静态块(打印)之后,您将得到一个错误,说没有找到主方法。
And Latest INFO --> YOU cant Do this with JAVA 7 version. IT will not execute.
还有最新的信息——>你不能用JAVA 7版本做这个。它不会执行。
{
static
{
System.out.println("Hello World!");
System.exit(0); // prevents “main method not found” error
}
}
But this will not execute with JAVA 7 version.
但是这不会在JAVA 7版本中执行。
#4
1
You should also be able to accomplish a similar thing using the premain method of a Java agent.
您还应该能够使用Java代理的premain方法完成类似的事情。
The manifest of the agent JAR file must contain the attribute Premain-Class. The value of this attribute is the name of the agent class. The agent class must implement a public static premain method similar in principle to the main application entry point. After the Java Virtual Machine (JVM) has initialized, each premain method will be called in the order the agents were specified, then the real application main method will be called. Each premain method must return in order for the startup sequence to proceed.
代理JAR文件的清单必须包含属性premainclass。这个属性的值是代理类的名称。代理类必须实现与主应用程序入口点原则上类似的公共静态前主方法。在Java虚拟机(JVM)初始化之后,将按指定代理的顺序调用每个premain方法,然后调用真正的应用程序主方法。每个premain方法必须返回,以便启动序列继续。
#1
21
Now - no
现在,没有
Prior to Java 7:
Java 7之前:
Yes, sequence is as follows:
是的,序列如下:
- jvm loads class
- jvm加载类
- executes static blocks
- 执行静态块
- looks for main method and invokes it
- 查找主方法并调用它
So, if there's code in a static block, it will be executed. But there's no point in doing that.
因此,如果静态块中有代码,它将被执行。但这样做没有意义。
How to test that:
如何测试:
public final class Test {
static {
System.out.println("FOO");
}
}
Then if you try to run the class (either form command line with java Test
or with an IDE), the result is:
然后,如果您尝试运行类(使用java测试或IDE生成命令行),结果是:
FOO
java.lang.NoSuchMethodError: mainFOO . lang。NoSuchMethodError:主要
#2
2
since you tagged Java-ee as well - then YES it is possible.
既然你也标记了java ee,那么这是可能的。
and in core java as well it is possible using static blocks
在core java中也可以使用静态块
and check this How can you run a Java program without main method?
检查一下,没有主方法怎么运行Java程序?
Edit:
as already pointed out in other answers - it is not support in Java 7
编辑:正如在其他答案中已经指出的,它不支持Java 7
#3
2
Yes You can compile and execute without main method By using static block. But after static block executed (printed) you will get an error saying no main method found.
是的,通过使用静态块,您可以不使用主方法编译和执行。但是在执行静态块(打印)之后,您将得到一个错误,说没有找到主方法。
And Latest INFO --> YOU cant Do this with JAVA 7 version. IT will not execute.
还有最新的信息——>你不能用JAVA 7版本做这个。它不会执行。
{
static
{
System.out.println("Hello World!");
System.exit(0); // prevents “main method not found” error
}
}
But this will not execute with JAVA 7 version.
但是这不会在JAVA 7版本中执行。
#4
1
You should also be able to accomplish a similar thing using the premain method of a Java agent.
您还应该能够使用Java代理的premain方法完成类似的事情。
The manifest of the agent JAR file must contain the attribute Premain-Class. The value of this attribute is the name of the agent class. The agent class must implement a public static premain method similar in principle to the main application entry point. After the Java Virtual Machine (JVM) has initialized, each premain method will be called in the order the agents were specified, then the real application main method will be called. Each premain method must return in order for the startup sequence to proceed.
代理JAR文件的清单必须包含属性premainclass。这个属性的值是代理类的名称。代理类必须实现与主应用程序入口点原则上类似的公共静态前主方法。在Java虚拟机(JVM)初始化之后,将按指定代理的顺序调用每个premain方法,然后调用真正的应用程序主方法。每个premain方法必须返回,以便启动序列继续。