虽然有一些技术可以让您创建 perfect (而不是 perfect )批处理文件与一些“本地”Windows 脚本语言混合。
“完美”的混合体应该是什么样子:
- 嵌入的代码必须是可用的,你应该有能力 将其复制粘贴到您想要的任何其他编辑器/IDE 中。应该有 没有对临时文件和奇怪的转义序列的无休止的回声。(而是 对于支持多行注释的每种语言都是可能的)。
- 没有错误消息的“有毒”打印(例如 /* 将打印错误 在命令提示符下尽管批处理将继续 在下一行)
- 没有临时文件。编译后的二进制文件除外 对于没有翻译的语言来说,这是不可避免的。
是否有可能创建“完美”的 java/batch 混合体?
最佳答案
答案差不多。
主要障碍是我知道的所有编译器都对文件扩展名非常严格,并且会拒绝编译任何没有 .java
扩展名的文件。
由于批处理文件中以@
开头的任何命令都不会显示,我们可以使用java注释来消除来自java注释的错误消息(应该用.bat保存
扩展名):
@Deprecated /* >nul 2>&1
:: self compiled java/.bat hybrid
::
:: deprecated is the only one annotation that can be used outside the class definition
:: and is needed for 'mute' start of multi-line java comment
:: that will be not printed by the batch file.
:: though it still creates two files - the .class and the .java
:: it still allows you to embed both batch and java code into one file
@echo off
setlocal
java -version >nul 2>&1 || (
echo java not found
exit /b 1
)
::find class name
::can be different than the script name
for /f "usebackq tokens=3 delims=} " %%c in (`type %~f0 ^|find /i "public class"^|findstr /v "for /f"`) do (
set "javaFile=%%c"
goto :skip
)
:skip
copy "%~f0" "%javaFile%.java" >nul 2>&1
javac "%javaFile%.java"
java "%javaFile%"
::optional
::del %javaFile%.* >nul 2>&1
end local
exit /b 0
*******/
public class TestClass
{
public static void main(String args[])
{
System.out.println("selfcompiled .bat/.java hybrid");
}
}
上面的示例涵盖了第 1 点和第 2 点。但是当然仍然需要创建 .java 文件。仍然复制比逐行回显 java 内容更快。
更进一步(或半步)- 使.java
扩展的行为类似于.bat
(或几乎)
假设您不喜欢代码中查找 java 类名和自复制代码的部分。
您可以使 .java 扩展名像 .bat 文件一样工作。或者几乎 - %0
将丢失,文件名将存储在 %1
中。
为此,您需要使用管理员权限调用此批处理文件:
@echo off
:: requires Admin permissions
:: allows a files with .JAVA (in this case ) extension to act like .bat/.cmd files.
:: Will create a 'caller.bat' asociated with the extension
:: which will create a temp .bat file on each call (you can consider this as cheating)
:: and will call it.
:: Have on mind that the %0 argument will be lost.
rem :: "installing" a caller.
if not exist "c:\javaCaller.bat" (
echo @echo off
echo copy "%%~nx1" "%%temp%%\%%~nx1.bat" /Y ^>nul
echo "%%temp%%\%%~nx1.bat" %%*
) > c:\javaCaller.bat
rem :: associating file extension
assoc .java=javafile
ftype javafile=c:\javaCaller "%%1" %%*
然后自编译的.java文件是这样的(应该以.java
扩展名保存):
@Deprecated /* >nul 2>&1
:: requires ran javaExtInstaller.bat
:: https://github.com/npocmaka/batch.scripts/blob/master/Java/javaExtInstaller.bat
::
:: self compiled java/.bat hybrid
::
:: deprecated is the only one annotation that can be used outside the class definition
:: and is needed for 'mute' start of multi-line java comment
:: that will be not printed by the batch file.
:: allows you to embed both batch and java code into one file
@echo off
setlocal
java -version >nul 2>&1 || (
echo java not found
exit /b 1
)
if not exist "%~n1.class" javac "%~nx1"
:: to compile the class every time use:
:: javac "%~nx1"
java "%~n1"
endlocal
exit /b 0
*******/
public class TestClass
{
public static void main(String args[])
{
System.out.println("selfcompiled .bat/.java hybrid");
}
}
javaCaller.bat
仍然会创建临时 .bat
文件,但它不会“可见”并且批处理部分要短得多。
示例中没有包,因为只使用了一个 java 文件。包只会让示例更难理解。
关于java - 如何将 java 代码嵌入到批处理脚本中?是否可以创建 .java/.bat 混合体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28190126/