Java程序在没有定义main方法的情况下如何运行?
我正在查看一些 Java 源代码,注意到 main
方法没有定义。
Java如何编译源码却无从下手?
I was looking through some Java source and noticed that the main
method wasn't defined.
How does Java compile source code without knowing where to start?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
main
方法仅在 Java 虚拟机执行代码时使用。如果没有main
方法,代码将无法执行,但仍然可以编译。编译代码时,您通常在命令行上指定一组文件,例如
Java 编译器 (
javac
) 检查您传递给它的每个类并将其编译为 .class 文件。Java 源代码可能缺少
main
方法的原因之一是它被设计为用作库,而不是被执行。您可能会发现有趣的事情:虽然 Java 编译器编译的源代码不需要
main
方法,但是 Java 编译器本身的源代码 确实有一个main
方法。The
main
method is only used when the Java Virtual Machine is executing your code. Code cannot be executed without amain
method but it can still be compiled.When compiling code, you usually specify a set of files on the command line e.g.
The Java compiler (
javac
) examines each class you passed to it and compiles it into a .class file.One reason Java source code may be missing a
main
method is because it is designed to be used as a library, instead of being executed.Something you may find interesting: although the source code compiled by the Java compiler does not need a
main
method, the source code for the Java compiler itself does have amain
method.运行和编译是有区别的。 Java代码可以增量编译。您只需要在某个地方有一个
main
来运行代码。 Java“知道从哪里开始”,因为编译器足够聪明,可以在编译时安排所有依赖项。事实上,如果您在某种标准容器中构建 Web 应用程序,您的代码可能不会有
main
方法。容器可以,但您只需编写插入的组件即可。There is a difference between running and compiling. Java code can be compiled incrementally. You only need a
main
somewhere to run the code. Java "knows where to start" because the compiler is smart enough to arrange all the dependencies when you compile.Indeed, if you are building a web application in some sort of standard container, your code probably won't have a
main
method. The container does, but you just write components that plug in.// 仅适用于 java 1.6 或更低版本
在 Java 中(运行时):
// works only on java 1.6 or less versions
In Java (while running):
我假设您的意思是运行(而不是编译),因为您不需要 main() 来编译。在这种情况下,显式声明的 main() 方法只是运行程序的方法之一。
您可以使用一些框架来执行代码。他们有 main() (仅讨论控制台应用程序)并且只要求您声明一个入口点。例如,这就是运行单元测试的方式。
I assume you meant run (instead of compile), since you don't need a main() to compile. In which case, an explicitly declared main() method is only one of the ways to run a program.
You can use some frameworks to execute your code. They have got the main() (talking about console applications only) and require you to declare an entry point only. This is how you run unit tests, for example.
您编写的每个 Java 类并不是要作为运行的入口点,这就是原因。我想说这是规则而不是例外。
Every single Java class you write isn't intended to be an entry point for running, that's why. I'd say that's the rule rather than the exception.
是的,我们可以在没有 main 方法的情况下运行 java 程序,为此我们将使用静态函数
以下是代码:
这将输出“Hi Look program is running without main() method”
Yes, we can run a java program without main method, for this we will use static function
Following is the code:
This will output "Hi look program is running without main() method"
这在 JDK 1.6 或更早版本中可以正常运行。在 1.7 及更高版本中,需要包含
main()
函数。This will run fine in JDK version 1.6 or earlier. In version 1.7 and later it is necessary to include a
main()
function.我们可以编译一个没有 main 方法的程序。实际运行程序与编译程序不同。大多数库不包含 main 方法。所以无论程序是否包含main方法,编译都没有问题。
We can compile a program without main method. Actually running a program differs from compiling it.Most of the libraries do not contain a main method. so for compiling there is no problem whether the program contains main method or not.
其中一种方法是静态块,但在以前版本的 JDK 中而不是在 JDK 1.7 中。
One of the way is static block but in previous version of JDK not in JDK 1.7.
这将执行没有任何错误并且没有 main() 方法
This will execute without any error and without main() method
如果您也不想使用静态块,可以按照以下方式完成
,但是请注意,这是针对 Java 6 版本的。这似乎在 Java 7 及更高版本中不起作用
If you don't want to use static block too, it can be done following way
However, please note that this is for Java 6 version. This doesn't seem to work in Java 7 and above
是的,我们可以编译并执行一个没有Main函数的java程序。相反,我们可以使用静态块来编译和执行程序。仅早期版本的 java 支持。不过现在不支持了...
Yes, We can Compile and Execute a java program without Main function. Instead of that we can use static block to compile and Execute the program. It is supported only for previous versions of java. But now it is not supported...