有 Java 编程相关的问题?

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

链接到批处理文件的java变量

我目前正在学习java,我遇到了这个问题。我不确定是否可以这样做,因为我仍处于学习阶段。因此,在我的java主代码中:

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();
        }
    }
}

“cmd/c start d.detectDrive()\MyBatchFile.bat”不起作用。我不知道如何替换变量

我创建了一个批处理文件(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

它不起作用。请不要笑

我真的不擅长编程,因为我刚开始使用java和批处理文件。有人能帮我吗?我不想把它硬编码成E:或类似的东西。我想让它更灵活。但是我不知道怎么做。我真诚地请求任何帮助


共 (1) 个答案

  1. # 1 楼答案

    程序:

    您应该将检测驱动器的方法的返回值附加到文件名,并组成正确的批处理命令字符串

    步骤:

    获取方法的返回值

    • 字符串驱动器=d.detectDrive()
    • 因此,drive包含值E:

    将驱动器的值附加到文件名

    • 驱动器+“\MyBatchFile.bat”
    • 因此,我们有E:\MyBatchFile。蝙蝠

    将结果附加到批处理命令

    • cmd/c启动“+drive+”\MyBatchFile。球棒
    • 结果是cmd/c start E:\MyBatchFile。蝙蝠

    因此,要调用batch命令,最后的代码应该如下:

        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();
        }