Question

I have been trying for 4 hours now to get this thing to run and I managed to do it without understanding why :/

I have created a very simple java program with a GUI containing 2 textboxes where people can type the path to an exe-file. 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:

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:

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. 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.

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?

Was it helpful?

Solution 3

boy do i feel like an idiot... The java errors threw me off, but the problem was a missing slash before "run this.exe".

so many shames...

OTHER TIPS

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.

//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();
...

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");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top