简体   繁体   中英

How do I run a java class with a shebang line on Windows?

我编写了一些 Java 实用程序,可与 Java 11 支持的 shebang 线一起使用。它们在我的 Mac 上运行良好,但我现在需要在 Windows 上使用它们,而 Windows 不支持 shebang 线,有没有办法做这个?

While Windows doesn't support the shebang line, the Java runtime will still run a java source file that conforms to the limitations for shebang-line java classes. (This basically means it must be contained in a single source file and not call any outside libraries. Standard Java libraries still work.)

Here's how I did it for a class I wrote called UuidGen.java :

First, I commented out the Shebang line, since it doesn't get used.

Second, I created a batch file called uuid.bat:

@echo off
java %~db0\UuidGen.java %*

Third, I put this file in the same directory as the UuidGen.java file.

Fourth, I put this directory on the system path.

Now, to run it, all I need to do is type uuid.

The %~db0 in the path gets replaced by the path to the .bat file being executed. The %* at the end passes all the parameters of the .bat command to the java command.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM