Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Path with spaces

Tags:

java

path

spaces

I am not a programmer and I need to fix some code to solve a problem. The problem is that the app does not read file paths with spaces.

Code:

private void jMenuHELPActionPerformed(java.awt.event.ActionEvent evt) { //GEN FIRST:event_jMenuHELPActionPerformed
    try {
        Runtime.getRuntime().exec("cmd /c start "+" C:\Users\rafi\Documents\Name with spaces\file.txt");
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    // ...
}

When I try to open a file from within the app, it opens a window with the following error:

Windows cannot `find C:\Users\rafi\Documents\Name`. Make sure that the name is correct.

It reads the path only to the first space.

How can I solve this problem?

like image 657
jagal Avatar asked Nov 23 '25 06:11

jagal


1 Answers

Use the exec method that takes an array of arguments instead. Don't forget you also need to escape your backslashes.

Runtime.getRuntime().exec(new String[] {"cmd", "/c", "start", "C:\\Users\\rafi\\Documents\\Name with spaces\\file.txt"});
like image 74
wyskoj Avatar answered Nov 25 '25 18:11

wyskoj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!