在命令提示符下运行时执行期间无法找到文件

Unable to find file during runtime execution at command prompt

各位专家大家好,

当我 运行 在命令提示符下执行以下命令时,我收到 FileNotFoundException:

c:\TA\java -jar LoginCaptchaChrome.jar LoginCaptchaChrome https://2015.qa.int.www.mol.com/ C:\TA\TD\Login\Login.xlsx C:\TA\TR\LoginCaptchaChrome_22082016_1838.xlsx

报错信息如下:

`Exception in thread "main" java.io.FileNotFoundException: C:\TA\TR\LoginCaptchaChrome_22082016_1838.xlsx" (The filename, directory name, or volume label syntax is incorrect)
        at java.io.FileOutputStream.open0(Native Method)
        at java.io.FileOutputStream.open(Unknown Source)
        at java.io.FileOutputStream.<init>(Unknown Source)
        at java.io.FileOutputStream.<init>(Unknown Source)
        at LoginCaptchaChrome.main(LoginCaptchaChrome.java:58)

我实际上是在命令提示符下传递参数,文件

LoginCaptchaChrome_22082016_1838.xlsx` is not being passed to the code line :

FileOutputStream fos = new FileOutputStream("\"" + args[3] + "\"");

在以下代码中:

public class LoginCaptchaChrome {

public static void main(String[] args) throws IOException, InterruptedException{
        String tc = args[0];
        String address = args[1];
        String test_data = args[2];
        String test_result = args[3];`

        System.setProperty("webdriver.chrome.driver", "C:\Users\Lam Chio Meng\Desktop\work\chromedriver_win32\chromedriver.exe");     

        FileOutputStream fos = new FileOutputStream("\"" + args[3] + "\"");
        XSSFWorkbook workbook = new XSSFWorkbook();                             

希望在此得到高手的指点。提前谢谢你。

问题来自对如何在命令行传递参数的误解。

以shell为例。假设这个命令在提示符下:

someCommand "arg with spaces"

进程的实际参数是什么:

  • someCommand,以及
  • arg with spaces。是的,这是一个单一的论点。

这意味着您的问题是这一行:

new FileOutputStream("\"" + args[3] + "\"");

您根本不需要前导引号和尾随引号。

此外,由于这是 2016 年,请不要使用 FileOutputStream。使用 JSR 203:

final Path path = Paths.get(args[3]);
final OutputStream out = Files.newOutputStream(path);

查看 Java 程序如何实际查看参数的简单方法是这样的程序:

public final class CmdLineArgs
{
    public static void main(final String... args)
    {
        final int len = args.length;

        System.out.println("---- Begin arguments ----");
        IntStream.range(0, len)
            .map(index -> String.format("Arg %d: %d", index + 1, args[index])
            .forEach(System.out::println);
        System.out.println("---- End arguments   ----");

        System.exit(0);
    }
}

在提示符下尝试 运行 这个命令,比如:

java MyClass foo bar

和:

java MyClass "foo bar"

看看区别。