Process makemenuconfig = Runtime.getRuntime().exec("cmd.exe /k start c:\\text1.bat "+"参数名");
这里调用系统命令行打开一个控制台窗口即cmd.exe,其中/k参数是让结果执行完毕之后不关闭cmd命令行窗口,改为/c则自动关闭,在cmd命令行中使用start命令打开一个批处理文件,批处理文件后面跟的即是参数
批处理应该像cmd控制台一样,直接在批处理里面写参数就行了,如 java Myclass 10 就行了,main方法本来就有String[]args ,接收参数的,处理一下这个args数组就行了
参数直接代入批处理可行性似乎不大,我试着用重写批处理文件的方式试了一下是可行的,代码如下:
import java.io.*;
public class Test {
public void writeFile(String path,String str){
try{
FileWriter theFile = new FileWriter(path,true);
PrintWriter out = new PrintWriter(theFile);
out.println("cd \\");
out.println("echo "+str+" >test.txt");
out.close();
theFile.close();
}catch(IOException e){}
}
public void execute(String cmdLine){
try{
String line="";
Process pro = Runtime.getRuntime().exec(cmdLine);
BufferedReader buf = new BufferedReader(new InputStreamReader(pro.getInputStream()));
line = buf.readLine();
while(line != null){
System.out.println(line);
line = buf.readLine();
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String args[]) throws Exception{
Test exe = new Test();
String file = "d:\\test.bat";
exe.writeFile(file, "Hello,world!");
exe.execute("cmd /c "+file);
}
}