Java程序如何在不定义main方法的情况下运行? [英] How do Java programs run without defining the main method?

查看:25
本文介绍了Java程序如何在不定义main方法的情况下运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在浏览一些 Java 源代码时注意到 main 方法没有定义.

I was looking through some Java source and noticed that the main method wasn't defined.

Java 如何在不知道从哪里开始的情况下编译源代码?

How does Java compile source code without knowing where to start?

推荐答案

main 方法仅在 Java 虚拟机执行您的代码时使用.没有 main 方法就不能执行代码,但它仍然可以编译.

The main method is only used when the Java Virtual Machine is executing your code. Code cannot be executed without a main method but it can still be compiled.

在编译代码时,通常会在命令行中指定一组文件,例如

When compiling code, you usually specify a set of files on the command line e.g.

javac MyClass1.java MyClass2.java

Java 编译器 (javac) 检查您传递给它的每个类并将其编译为 .class 文件.

The Java compiler (javac) examines each class you passed to it and compiles it into a .class file.

Java 源代码可能缺少 main 方法的一个原因是它被设计为用作库,而不是被执行.

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.

您可能会发现一些有趣的事情:虽然 Java 编译器编译的源代码不需要 main 方法,但是 Java编译器本身的源代码确实有一个main 方法.

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 a main method.

这篇关于Java程序如何在不定义main方法的情况下运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆