如何将java代码嵌入到批处理脚本中?是否可以创建.java / .bat混合? [英] How can I embed java code into batch script?Is it possible to create .java/.bat hybrid?

查看:249
本文介绍了如何将java代码嵌入到批处理脚本中?是否可以创建.java / .bat混合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然有一些技术可以让你创建完美(并非如此完美)批处理文件混合使用一些本机Windows脚本语言。

While there are some techniques that allows you to create the perfect (and not so perfect) batch file hybrids with some 'native' windows script languages.

完美混合应该是什么样的:

What a 'perfect' hybrid should look like:


  1. 嵌入的代码必须按原样使用,你应该能够
    将它复制粘贴到你想要的任何其他编辑器/ IDE中。对于临时文件和奇怪的转义序列应该有
    无穷无尽的回声。(而对于支持多行注释的每种语言,都可能是
    )。

  2. 没有'有毒'错误信息的打印(例如 / * 将在命令提示符中打印错误
    ,尽管批次的执行将在下一行继续

  3. 没有临时文件。除了编译的二进制文件之外,对于没有翻译的语言,
    是不可避免的。

  1. The embedded code must be usable as it is and you should be capable to copy-pasted it in any other editor/IDE you want. There should be no endless echoes to temp files and weird escape sequences.(rather possible for every language that support multi-line comments).
  2. No 'poisonous' prints of error messages (e.g. /* will print an error in command prompt despite the execution of the batch will continue on the next line)
  3. No temporary files.With the exception of compiled binary files which is unavoidable for the languages that does not have an interpreter.

是否可以创建'完美'的java /批混合?

Is it possible to create the 'perfect' java/batch hybrid?

推荐答案

答案差不多。

主要障碍是我所知道的所有编译器对文件扩展都非常严格,并拒绝编译任何没有 .java的文件 extension。

The main impediment is that the all compilers I know are very strict about the file extensions and will refuse to compile any file that has no .java extension.

因为不会显示以 @ 开头的批处理文件中的任何命令我们可以使用java注释t o沉默来自java注释的错误消息(应该用 .bat 扩展名保存):

As any command in batch files starting with @ will be not displayed we can use the java annotations to silence the error message that comes from the java comment (should be saved with .bat extension):

@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内容要快。

The example above covers points 1. and 2. but of course there's still need of creation of .java file.Still copying is faster than echoing java content line by line.

更进一步(或半步) - 制作一个 .java 扩展名,作为 .bat (或几乎)

One step further (or half step) - making a .java extension to act like .bat (or almost)

让我们说你不喜欢代码中找到java类名和自我复制代码的部分。
您可以将.java扩展名设置为.bat文件。或者几乎 - %0 将丢失,文件名将存储在%1
为此,您需要使用管理员权限调用此批处理文件:

Lets say you don't the like the part in the code that finds the java class name and the self-copy code. You can make the .java extension to act like .bat file.Or almost - the %0 will be lost and the file name will be stored in %1. For that purpose you need to call this batch file with admin permissions:

   @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文件看起来像这样(应该用<$ c $保存) c> .java extension):

Then the self-compiled .java file will look like this (should be saved with .java extension):

@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");
    }
}

仍然会创建temp .bat javaCaller.bat 的文件,但它不是可见的,批次部分要短得多。

there still will have a creation of temp .bat file by the javaCaller.bat but it will be not 'visible' and batch part is much shorter.

在示例中没有包,因为使用了唯一的单个java文件.Packages只会使示例更难理解。

In the examples there are no packages as the only single java file is used .Packages will make only the example harder to understand.

这篇关于如何将java代码嵌入到批处理脚本中?是否可以创建.java / .bat混合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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