简体   繁体   English

在Java中使用空格运行外部exe

[英]running external exe with spaces in java

I have been trying for 4 hours now to get this thing to run and I managed to do it without understanding why :/ 我已经尝试了4个小时才能运行此程序,但我却不理解为什么这样做://

I have created a very simple java program with a GUI containing 2 textboxes where people can type the path to an exe-file. 我用GUI创建了一个非常简单的Java程序,其中包含2个文本框,人们可以在其中键入exe文件的路径。 When a button is clicked it reads the text in this box and runs the corresponding software. 单击按钮后,它将读取此框中的文本并运行相应的软件。

This seems to work when people type ""C:\\Program Files (x86)\\thatsoftware\\" in the directory box and "C:\\Program Files (x86)\\thatsoftware\\run this.exe -arg" in the file to run box: 当人们在目录框中键入“ C:\\ Program Files(x86)\\ thatsoftware \\”,并在要运行的文件中键入“ C:\\ Program Files(x86)\\ thatsoftware \\ run this.exe -arg”时,这似乎起作用框:

Runtime.getRuntime().exec(txtFile.getText().toString(), null, new File(txtPath.getText().toString()));

However, when I set only 1 directory box and append (hardcoded) the file and argument to it, it will not work: 但是,当我仅设置1个目录框并附加(硬编码)文件和参数时,它将不起作用:

String fileToRun=txtPath.getText().toString()+"run this.exe -arg";
Runtime.getRuntime().exec(fileToRun, null, new File(txtPath.getText().toString()));

I have tried passing the file as an array as well: 我也尝试将文件作为数组传递:

String fileToRun[]={txtPath.getText().toString(),"run this.exe"," -arg"};
Runtime.getRuntime().exec(fileToRun, null, new File(txtPath.getText().toString()));

to no avail. 无济于事。 The same kind of problems pop up when I try to run it as a processbuilder. 当我尝试将其作为processbuilder运行时,会弹出同样的问题。 I will get an error message like "file C:\\Program Files (x86)\\thatsoftware\\ -arg" does not exist." Very weird, since the argument is passed, but not the file name apparently. 我将收到类似“文件C:\\ Program Files(x86)\\ thatsoftware \\ -arg”不存在的错误消息。很奇怪,因为传递了参数,但显然没有文件名。

I can manage to run it when the whole string is typed in the text box by the user, but not if I add the argument and or filename to it in the code. 当用户在文本框中键入整个字符串时,我可以设法运行它,但是如果我在代码中添加了参数和或文件名,则无法运行它。 Could anyone be so kind to explain this to me and tell how it can be done with only one text box? 谁能这么仁慈地向我解释一下,并告诉我如何仅用一个文本框就能做到?

You need to wrap the executable in escaped quotes \\" like this: 您需要将可执行文件包装在转义引号\\“中,如下所示:

Runtime runtime = Runtime.getRuntime();
Process ps = runtime.exec("\"run this.exe\"");

Or using a path and the argument as you need: 或根据需要使用路径和参数:

Process ps = runtime.exec("\"C:\\Program Files (x86)\\Thatsoftware\\my exe.bat\" -arg");

As I know you must put each element as separate field: 据我所知,您必须将每个元素都放在单独的字段中:

String fileToRun[]={
                   txtPath.getText().toString(),
                   "run",
                   "this.exe",
                   " -arg"
                    };

I don't know if you use Swing or not but Swing has javax.swing.JFileChooser . 我不知道您是否使用Swing ,但是Swing具有javax.swing.JFileChooser

//config fileChooser
    JFileChooser fc = new JFileChooser(lastOpenDir);

    fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
    fc.setDialogTitle("Load Beacon List");
    fc.removeChoosableFileFilter(fc.getFileFilter());  //remove the default file filter
    FileFilter filter = new FileNameExtensionFilter("EXE file", "exe");
    fc.addChoosableFileFilter(filter); //add XML file filter

    //show dialog
    int returnVal = fc.showOpenDialog(this);
    if(returnVal == JFileChooser.APPROVE_OPTION){

        File selectedDir = fc.getSelectedFile();
...

boy do i feel like an idiot... The java errors threw me off, but the problem was a missing slash before "run this.exe". 男孩,我感觉像个白痴吗... Java错误使我失望,但问题是在“运行this.exe”之前缺少斜杠。

so many shames... 太可惜了...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM