问题描述
JEP 330 描述了JDK 11中的一项新功能,用于在Java中启动单文件程序.我尝试过:
JEP 330 describes a new feature in JDK 11 for launching single-file programs in Java. I've tried:
$ ./Example.java
但是它不起作用.正确的用法是什么?
but it doesn't work. What is the correct usage?
推荐答案
尽管您的答案包含正确的信息.只是尝试将其简单化,可以简单地使用JDK11及以后版本中的java
执行文件,例如在MacOS上
Though the answer by you includes correct information. Just trying to put this into simpler terms, a file can simply be executed using java
from JDK11 onwards, for example on MacOS
.../jdk-11.jdk/Contents/Home/bin/java Sample.java
这将寻求并执行标准的 public static void main(String[] args)
方法.可以注意到(甚至是初学者)该方法接受类型为String
的args
,因此在原始命令行中位于源文件的名称之后的参数将传递给已编译的类当它执行时.因此,以下命令
This would seek and execute the standard public static void main(String[] args)
method. As one can notice(even beginners) that this method accepts args
of type String
, hence the arguments placed after the name of the source file in the original command line are passed to the compiled class when it is executed. Therefore the following command
.../jdk-11.jdk/Contents/Home/bin/java <file-name>.java arg1 arg2
在执行阶段将提供字符串参数arg1
,arg2
.
would provide the string arguments arg1
, arg2
during the execution phase.
附带说明 -如果文件包含具有标准主要方法的多个类,则在源文件中找到的第一个顶级类应包含标准的声明执行公共静态void main(String [])方法.
这篇关于如何在Java 11(或更高版本)中启动单文件程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!