0

Is there any way to convert a long filepath into a short (8.3 format) directly through the Java API (i.e. not using the command line)?

For example, it should convert C:\Program Files\Java to C:\Progra~1\Java.

Philip C
  • 1,819
  • 28
  • 50
user987316
  • 894
  • 4
  • 13
  • 35

1 Answers1

0

You can use for example JNA as described in this SO answer

Java using JNA

import com.sun.jna.Native;
import com.sun.jna.platform.win32.Kernel32;

public class LongToShort {

    public static String GetShortPathName(String path) {
        char[] result = new char[256];
        Kernel32.INSTANCE.GetShortPathName(path, result, result.length);
        return Native.toString(result);
    }

    // usage: java LongToShort "C:\Program Files (x86)\Java\jdk1.6.0_45"
    public static void main(String[] args) {
        System.out.println(GetShortPathName(args[0]));
    }
}
Community
  • 1
  • 1
SubOptimal
  • 22,518
  • 3
  • 53
  • 69