java 单文件 怎么运行,如何在Java 11(或更高版本)中启动单文件程序?

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?

解决方案

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

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 .java arg1 arg2

would provide the string arguments arg1, arg2 during the execution phase.

Side note - If the file includes multiple classes with standard main methods, the first top-level class found in the source file which shall contain the declaration of the standard public static void main(String[]) method is executed.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值