alternatives to batch file since i am unable to include java code in it

和自甴很熟 提交于 2019-12-01 14:25:29

So it is possible to add java code into batch file?

No, you cannot add Java code to batch file.

Alternative Workaround:

To use Java value in batch file, print the value in Java, Use that value in batch file.

In Java,

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

In batch file:

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%" 

Make your Java program pass the filename to the batch file as a command-line option. Within the batch file, you can access the it as the variable %1. You can pass multiple command-line options; the others will be available as %2, %3, and so on.

Here how you can add java code to batch file (though it's not exactly complete solution for your problem):

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

Deprecated annotation is the only one I've found that can be set outside the class.And annotation is needed to avoid toxic output.It also creates two temporary files (.class and .java) which you can delete.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!