问题
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