如果你真的在寻找简单的 Windows 批处理解决方案,这里有一个源自 PostgreSQL 的批处理。
SET path=c:\
SET /P path="Path of code [%path%]: "
SET folder=\
SET /P folder="Folder name [%folder%]: "
REM and do what you want with those values ...
echo %path%
echo %folder%
此方法甚至允许提出默认值。
编辑
我刚刚看到你的 java 代码是从控制台而不是从标准输入读取的,所以没有重定向解决方案会有所帮助。也许你应该看看 SO How to simulate keyboard presses in java? 的以下帖子
EDIT2
所以问题不是我最初想到的批量询问参数,而是自动化一个在控制台上询问两个参数的java程序。机器人是自动化此类事情的好方法。我写了一段 java 来模拟它在键盘上输入的参数,然后输入。我自己的技巧是使用 Alt xyz 为任何字符发送正确的 KeyEvent。
你应该只做java -jar ...\RoboSend.jar "ext.bat" "real_path" "real_folder"或java -jar ...\RoboSend.jar "ext.bat" %path% %folder%
package org.sba.robotsend;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
/**
* This program simulates the typing of its arguments on Console keyboard.
* Each argument is send via Robot to the console, followed by an Enter key
* Ex : java -j RobotSend.jar "echo foo" "echo bar" gives :
* c:\> echo foo
* foo
* c:\> echo bar
* bar
*
* It is intented to automate programs reading their input on the Console
*
* @author serge.ballesta
*/
public class RobotSend {
private Robot robot;
private Charset cp850;
private static final int[] keys = { KeyEvent.VK_NUMPAD0, KeyEvent.VK_NUMPAD1,
KeyEvent.VK_NUMPAD2, KeyEvent.VK_NUMPAD3, KeyEvent.VK_NUMPAD4,
KeyEvent.VK_NUMPAD5, KeyEvent.VK_NUMPAD6, KeyEvent.VK_NUMPAD7,
KeyEvent.VK_NUMPAD8, KeyEvent.VK_NUMPAD9
};
/**
* This program simulates the typing of its arguments on Console keyboard.
* Each argument is send via Robot to the console, followed by an Enter key
* Ex : java -j RobotSend.jar "echo foo" "echo bar" gives :
* c:\> echo foo
* foo
* c:\> echo bar
* bar
*
* It is intented to automate programs reading their input on the Console
* @param args the command line arguments
*/
public static void main(String[] args) {
RobotSend robot = new RobotSend();
try {
robot.run(args);
}
catch (Exception e) {
e.printStackTrace();
}
}
public void run(String[] args) throws AWTException {
robot = new Robot();
cp850 = Charset.forName("IBM850");
for (String str: args) {
sendString(str);
}
}
/**
* Send a byte using the Alt xyz sequence.
* the byte must be in CP850 code page, indipendently of the actual code
* page of the console (at least for System natively in CP850 ...)
* @param c the byte (char) to be inputted via keyboard
*/
public void sendByte(byte c) {
int i = (int) c;
if (i < 0) { i = 256 + i; }
if (i < 0 || i > 255) { i = 'X'; }
int i1 = i / 100;
int i2 = (i % 100) / 10;
int i3 = i % 10;
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(keys[i1]);
robot.keyRelease(keys[i1]);
robot.keyPress(keys[i2]);
robot.keyRelease(keys[i2]);
robot.keyPress(keys[i3]);
robot.keyRelease(keys[i3]);
robot.keyRelease(KeyEvent.VK_ALT);
}
/**
* Simulate a Enter
*/
public void sendEnter() {
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}
/**
* Send a String via the Console keyboard.
* The string is first encoded in CP850, sent one char at a time via sendByte
* and followed by an Enter key
* @param str
*/
public void sendString(String str) {
ByteBuffer buf = cp850.encode(str);
for (byte b: buf.array()) {
sendByte(b);
}
sendEnter();
}
}