0

I heard that java code cannot be to add into batch file in a comment on my previous question:

But is there any alternatives to it?
Is it possible to add java code into batch file?

I tried the following in the batch file but it does not work:

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

This is cmdenv.bat that i created to run in command prompt from java:

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

This allows my volatility commands to run in a enclosed command prompt (Helix) which reduces footprint.

However, the volatility commands are all hardcoded. The E:\KOHMOHOJOJO-PC-20140714-152414.raw and --profile=Win7SP0x86 varies. Is there any alternative to do it since batch file does allow java coding?

I am new to java and batch file.

Edit1: enter image description here enter image description here

I tried changing the java_path but it still have errors.

Edit2: I tried different paths like:

  1. C:\Program Files\Java\jdk1.8.0_05\bin and C:\Program Files\Java\jre8\bin I get Error: Could not find or load main class TestRun1.

  2. C:\Users\User\workspace\Volatility\bin and C:\Users\User\workspace\Volatility\src (this is the workspace where the class is saved) I get C:\Users\User\workspace\Volatility\(bin or src)\java is not recognised as an internal or external command, operable program or batch file. So I added java.exe into the file. And the error gets back to Error: Could not find or load main class TestRun1.

This batch file doesn't seem to be able to read the java.. I tried adding a . in front of the TestRun1 but still it has the same error. I also found out that I have the environment variable under Control Panel\System and Security\System\Advance System Settings\Environment Variables (Path). I tried deleting C:\Program Files\Java\jdk1.8.0_05\bin and then running it but it still has the same error.

Community
  • 1
  • 1
Linify
  • 227
  • 4
  • 13
  • possible duplicate of [How to Pass Command Line Parameters in Batch File](http://stackoverflow.com/questions/26551/how-to-pass-command-line-parameters-in-batch-file) – Wyzard Jul 15 '14 at 07:15
  • Tip: All the information should be present in current question, I edited and pasted all the info from the linked question here. Please remember this in your future questions on Stack Overflow, a question must have **complete information** – Infinite Recursion Jul 15 '14 at 07:39

3 Answers3

0

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.

Wyzard
  • 33,849
  • 3
  • 67
  • 87
  • hi, do you mind explaining on how to make java program pass the filename to batch file as a command-line option??? – Linify Jul 16 '14 at 09:06
0

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%" 
Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
  • i tried but it says java is not recognised as an internal or external command, operable program or batch file – Linify Jul 15 '14 at 07:50
  • I'm really sorry to trouble you. But I wonder if it is the problem with me running those applications from my thumbdrive (E:\). Now it says **C:\Program is not recognised as an internal or external command, operable program or batch file**. And then I tried testing out different parts. And I realised that `System.out.println(m.walkin(new File(d.detectDrive())));` has an error. It has a red cross on the line pane area. But `m.walkin(new File(d.detectDrive()));` can work. And then next, the `%p%` is unable to print `E:\KOHMOHOJOJO-PC-20140714-152414.raw` when running the command. Sorry!!! – Linify Jul 15 '14 at 08:09
  • Add the System.out.println in the java file where you already have the method `walkin`. Don't create new java file. In that java file, adding this line will print it. Note: there should not be any other println in that java file. Else, batch file can't use it. – Infinite Recursion Jul 15 '14 at 08:17
  • I have uploaded the images. Still trying my best to debug. – Linify Jul 16 '14 at 00:02
0

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.

npocmaka
  • 55,367
  • 18
  • 148
  • 187