Question

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);

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

  }

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

For example say I had a currentDir with value [Desktop\A B.lnk]

Parsed it would be [Desktop\"A B.ink"] 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 'B.ink', Make sure you typed the name in correctly and try again

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top