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

发布于 2024-12-06 23:06:13 字数 79 浏览 1 评论 0原文

我正在查看一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

梅窗月明清似水 2024-12-13 23:06:14

main 方法仅在 Java 虚拟机执行代码时使用。如果没有 main 方法,代码将无法执行,但仍然可以编译。

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

javac MyClass1.java MyClass2.java

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 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

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

夏花。依旧 2024-12-13 23:06:14

运行和编译是有区别的。 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.

就此别过 2024-12-13 23:06:14

// 仅适用于 java 1.6 或更低版本

public class Test{   
    // this is static block

    static{
        System.out.println("This is static block");  
    }
}

在 Java 中(运行时):

  1. 识别所有静态成员。
  2. 所有变量和方法都被初始化
  3. 静态块被执行

// works only on java 1.6 or less versions

public class Test{   
    // this is static block

    static{
        System.out.println("This is static block");  
    }
}

In Java (while running):

  1. all static members are identified.
  2. all variables and methods are initialized
  3. static block is executed
泛滥成性 2024-12-13 23:06:14

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

我假设您的意思是运行(而不是编译),因为您不需要 main() 来编译。在这种情况下,显式声明的 main() 方法只是运行程序的方法之一。
您可以使用一些框架来执行代码。他们有 main() (仅讨论控制台应用程序)并且只要求您声明一个入口点。例如,这就是运行单元测试的方式。

how does Java compile run your source without knowing where to
start?

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.

撩起发的微风 2024-12-13 23:06:14

您编写的每个 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.

¢蛋碎的人ぎ生 2024-12-13 23:06:14

是的,我们可以在没有 main 方法的情况下运行 java 程序,为此我们将使用静态函数

以下是代码:

class Vishal
{
    static
    {
        System.out.println("Hi look program is running without main() method");
    }
}

这将输出“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:

class Vishal
{
    static
    {
        System.out.println("Hi look program is running without main() method");
    }
}

This will output "Hi look program is running without main() method"

以可爱出名 2024-12-13 23:06:14
public class Test{   
    // this is static block

    static{
        System.out.println("This is static block");  
        System.exit(0);
    }
}

这在 JDK 1.6 或更早版本中可以正常运行。在 1.7 及更高版本中,需要包含 main() 函数。

public class Test{   
    // this is static block

    static{
        System.out.println("This is static block");  
        System.exit(0);
    }
}

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.

守望孤独 2024-12-13 23:06:14

我们可以编译一个没有 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.

想你只要分分秒秒 2024-12-13 23:06:14
package com.test;

public class Test {
    static {
        System.out.println("HOLAAAA");
        System.exit(1);
    }
}

//by coco
//Command line:
//java -Djava.security.manager=com.test.Test
package com.test;

public class Test {
    static {
        System.out.println("HOLAAAA");
        System.exit(1);
    }
}

//by coco
//Command line:
//java -Djava.security.manager=com.test.Test
静赏你的温柔 2024-12-13 23:06:14

其中一种方法是静态块,但在以前版本的 JDK 中而不是在 JDK 1.7 中。

class A3{  
  static{  
  System.out.println("static block is invoked");  
  System.exit(0);  
  }  
}  

One of the way is static block but in previous version of JDK not in JDK 1.7.

class A3{  
  static{  
  System.out.println("static block is invoked");  
  System.exit(0);  
  }  
}  
如痴如狂 2024-12-13 23:06:14

这将执行没有任何错误并且没有 main() 方法

abstract class hello extends javafx.application.Application
{
    static 
    {
    System.out.println("without main method");
    System.exit(0);
    }
}

This will execute without any error and without main() method

abstract class hello extends javafx.application.Application
{
    static 
    {
    System.out.println("without main method");
    System.exit(0);
    }
}
小糖芽 2024-12-13 23:06:14

如果您也不想使用静态块,可以按照以下方式完成

public class NoMain {

    private static final int STATUS = getStatus();

    private static int getStatus() {
        System.out.println("Hello World!!");
        System.exit(0);
        return 0;
    }

}

,但是请注意,这是针对 Java 6 版本的。这似乎在 Java 7 及更高版本中不起作用

If you don't want to use static block too, it can be done following way

public class NoMain {

    private static final int STATUS = getStatus();

    private static int getStatus() {
        System.out.println("Hello World!!");
        System.exit(0);
        return 0;
    }

}

However, please note that this is for Java 6 version. This doesn't seem to work in Java 7 and above

苏辞 2024-12-13 23:06:14

是的,我们可以编译并执行一个没有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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文