有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

批处理文件的替代方案,因为我无法在其中包含java代码

我听说java代码不能在my previous question:上的注释中添加到批处理文件中

但是有没有其他的选择呢
是否可以将java代码添加到批处理文件中

我在批处理文件中尝试了以下操作,但无效:

vol231.exe -f m.walkin(new File(d.detectDrive())) imageinfo > Volatility.txt

这是cmdenv。我创建用于在java命令提示符下运行的bat:

E:
echo off
cls

... //This part is a long part for reducing footprint whereby the command prompt is switched to Helix

vol231.exe -f E:\KOHMOHOJOJO-PC-20140714-152414.raw imageinfo > Volatility.txt
vol231.exe -f E:\KOHMOHOJOJO-PC-20140714-152414.raw --profile=Win7SP0x86 pslist >> Volatility.txt
pause
exit

这允许我的volatility命令在一个封闭的命令提示符(Helix)中运行,从而减少了占用空间

但是,volatility命令都是硬编码的。E:\KOHMOHOJOJO-PC-20140714-152414.raw--profile=Win7SP0x86各不相同。因为批处理文件允许java编码,所以有没有其他方法可以这样做

我不熟悉java和批处理文件

编辑1: enter image description here enter image description here

我尝试更改java_路径,但仍然有错误

编辑2: 我尝试了不同的途径,如:

  1. C:\Program Files\Java\jdk1.8.0_05\binC:\Program Files\Java\jre8\bin 我得到Error: Could not find or load main class TestRun1

  2. C:\Users\User\workspace\Volatility\binC:\Users\User\workspace\Volatility\src(这是保存类的工作区)我得到C:\Users\User\workspace\Volatility\(bin or src)\java未被识别为内部或外部命令、可操作程序或批处理文件。所以我添加了java。exe文件。然后错误返回到Error: Could not find or load main class TestRun1

此批处理文件似乎无法读取java。。我尝试在TestRun1前面添加一个.,但仍然有相同的错误。我还发现我在Control Panel\System and Security\System\Advance System Settings\Environment Variables(Path)下有一个环境变量。我尝试删除C:\Program Files\Java\jdk1.8.0_05\bin,然后运行它,但仍然有相同的错误


共 (3) 个答案

  1. # 1 楼答案

    让Java程序将文件名作为命令行选项传递给批处理文件。在批处理文件中,您可以将其作为变量%1访问。您可以传递多个命令行选项;其他的将以%2%3等形式提供

  2. # 2 楼答案

    下面介绍如何将java代码添加到批处理文件(尽管这并不是解决问题的完整解决方案):

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

    不推荐使用的注释是我发现的唯一一个可以在类之外设置的注释。需要注释以避免有毒输出。它还创建了两个临时文件(.class和.java),您可以删除它们

  3. # 3 楼答案

    所以有可能将java代码添加到批处理文件中吗

    ,无法将Java代码添加到批处理文件中

    替代解决方案:

    要在批处理文件中使用Java值,请在Java中打印该值,然后在批处理文件中使用该值

    在爪哇

    public class TestRun1
    {
        public static void main(String args[])
        {
           System.out.println(m.walkin(new File(d.detectDrive())));
        }
    }
    

    在批处理文件中:

    set tmp_file=%TEMP%\%~n0.tmp
    
    rem   set as per your java directory   -
    set java_path=C:\Program Files\Java\jdk1.6.0_43\bin
    
    "%java_path%\java" TestRun1> "%tmp_file%"
    set /P p=<"%tmp_file%"
    echo %p%
    
    rem   -now p has the value, use it in the command    
    
    vol231.exe -f %p% imageinfo > Volatility.txt
    vol231.exe -f %p%  profile=Win7SP0x86 pslist >> Volatility.txt
    vol231.exe -f %p%  profile=Win7SP0x86 dlllist > Volatility.txt
    vol231.exe -f %p%  profile=Win7SP0x86 iehistory >> Volatility.txt
    vol231.exe -f %p%  profile=Win7SP0x86 svcscan >> Volatility.txt
    vol231.exe -f %p%  profile=Win7SP0x86 hivelist >> Volatility.txt
    vol231.exe -f %p%  profile=Win7SP0x86 filescan >> Volatility.txt
    vol231.exe -f %p%  profile=Win7SP0x86 netscan >> Volatility.txt
    pause
    del /F "%tmp_file%"