Cannot open file using Runtime.getRuntime().exec when file name contains special character on Window 8.1

462 views Asked by At

I try to use the following code to open a specific file on Window 8.1

public static void main(String[] args) throws Exception
{
    if (args.length > 0)
    {
         File file = new File(args[0]);
         if (file.exists())
         {
             try
             {
                 Runtime.getRuntime().exec(new String[]{"rundll32", "url.dll,FileProtocolHandler", file.getAbsolutePath()});
             }
             catch(Exception e)
             {
                 e.printStackTrace();
             }
         }
         else
         {
             System.out.println("File does not exist");
         }
    }
}

The file cannot be opened if there are special characters in file name.

For example: args[0] = 新正如意 新年发财.txt

How can I fix this problem?

Thanks in advance

0

There are 0 answers