java variable linked to batch file

不问归期 提交于 2019-12-25 05:09:58

问题


I am currently learning java and I encountered this problem. I am not sure if it can be done like this as I am still in the learning stage. So in my java main coding:

import java.io.File;
import java.io.IOException;

public class TestRun1
{
    public static void main(String args[])
    {

        //Detect usb drive letter
        drive d = new drive();
        System.out.println(d.detectDrive());

        //Check for .raw files in the drive (e.g. E:\)
        MainEntry m = new MainEntry();
        m.walkin(new File(d.detectDrive()));

        try
        {
            Runtime rt = Runtime.getRuntime();
            Process p = rt.exec("cmd /c start d.detectDrive()\\MyBatchFile.bat");
        } 
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

The "cmd /c start d.detectDrive()\MyBatchFile.bat" does not work. I do not know how to replace the variable.

And i created a batch file (MyBatchFile.bat):

@echo off
set Path1 = d.detectDrive()
Path1
pause
set Path2 = m.walkin(new File(d.detectDrive()))
vol231.exe -f Path2 imageinfo > Volatility.txt
pause
exit

It does not work. Please do not laugh.

I really isn't good in programming since I just started on java and batch file. Can anyone help me with it? I don't want to hard code it to become a E: or something like that. I want to make it flexible. But I have no idea how to do it. I sincerely ask for any help.


回答1:


Procedure:

You should append the return value of the method which detects the drive, to the filename and compose the proper Batch command string.

Steps:

Get the return value of the method

  • String drive = d.detectDrive();
  • so, drive contains the value E:

append the value of drive to the filename

  • drive+"\MyBatchFile.bat"
  • so, we have E:\MyBatchFile.bat

append the result the batch command

  • cmd /c start "+drive+"\MyBatchFile.bat
  • result is cmd /c start E:\MyBatchFile.bat

So to invoke the batch command, the final code should be as follows:

    try {
        System.out.println(d.detectDrive()); 
        Runtime rt = Runtime.getRuntime();
        String drive = d.detectDrive();
        // <<---append the return value to the compose Batch command string--->>
        Process p = rt.exec("cmd /c start "+drive+"\\MyBatchFile.bat");
    } 
    catch (IOException e) {
        e.printStackTrace();
    }


来源:https://stackoverflow.com/questions/24750149/java-variable-linked-to-batch-file

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