是否可以使用JDK 1.7或更高版本在没有main()的情况下用Java编写程序? [英] Is it possible to write a program in Java without main() using JDK 1.7 or higher?

查看:131
本文介绍了是否可以使用JDK 1.7或更高版本在没有main()的情况下用Java编写程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用 JDK 1.6 或更低版本的 Hello world > JDK 。

The following program will print Hello world when compiled with JDK 1.6 or lower versions of JDK.


    public class A
    {
      static
      {
        System.out.println("Hello world");
        System.exit(0);
      }
    }

JDK 1.7 或更高在编译上述程序时,版本将生成运行时错误,如下所示。

But JDK 1.7 or higher versions will generate a runtime error as follows when the above program is compiled.


    Error: Main method not found in class A, please define the main method as:
       public static void main(String[] args)
    or a JavaFX application class must extend javafx.application.Application

我想知道是否有一些方法可以使用 JDK在Java中成功编译并运行没有 main()的程序1.7 或更高。

I would like to know if there is some way to compile and run a program without main() successfully in Java using JDK 1.7 or higher.

提前致谢。

推荐答案

没有。 public static void main(String [] args)是所有Java应用程序的主条目。有让你相信的框架不需要这个方法,比如JUnit执行的单元测试,但事实是框架有一个 main 在其中的某个地方定义的方法,为你做必要的调用并结束调用你的代码。

No. public static void main(String[] args) is the main entry for all Java applications. There are frameworks that makes you believe there's no need of this method, like a unit test executed by JUnit, but the fact is that the framework has a main method defined somewhere inside it, does the necessary calls for you and ends calling your code.

这篇关于是否可以使用JDK 1.7或更高版本在没有main()的情况下用Java编写程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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