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?
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"});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With