How to launch a Shortcut with Spaces in Java using ProcessBuilder

This is the code I would use normally to execute a Shortcut file (.lnk)

  //Get Directory
  String currentDir = new File(game.getGamePath()).getCanonicalPath();

  //Parse Directory to put apostrophes around shortcut name    
  currentDir = currentDir.substring(0, currentDir.lastIndexOf("\") + 1) + '"' +                      
  currentDir.substring(currentDir.lastIndexOf("\") + 1, currentDir.length()) + '"';

  //prep the launcher
  ProcessBuilder processBuild = new ProcessBuilder();
  processBuild.command("cmd", "/c", "start" ,"/wait", "", currentDir);

  Process = processBuild.start();
  try {
  } catch (InterruptedException ex) {


Problem is when the Shortcut file name has spaces I get an Error from windows saying it could not load []

For example say I had a currentDir with value [DesktopA B.lnk]

Parsed it would be [Desktop”A”] and this works perfectly on the command prompt.

problem is I would get this Error if I were to use the above code:

Windows cannot Find ‘’, Make sure you typed the name in correctly and try again


Use ” to get double quotes within strings Also, quote the whole link from the start instead of just the link name as you might have other spaces before it. That will save you from trouble.

Leave a Reply

Your email address will not be published. Required fields are marked *