如何在不定义main方法的情况下运行Java程序

如何在不定义main方法的情况下运行Java程序

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

问题描述

我正在查看一些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 方法,确实有一个 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.

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

07-29 17:01