3

Actually I have next code:

String f = LauncherFrame.class.getProtectionDomain().getCodeSource().getLocation().getPath(); // path to launcher
java.lang.System.out.println(f);

String launcherHash = "";

try{
  MessageDigest md5  = MessageDigest.getInstance("MD5");
  launcherHash = calculateHash(md5, f);
}catch (Exception e) {
  java.lang.System.out.println(e){
  return;
}

calculateHash function:

public static String calculateHash(MessageDigest algorithm,String fileName) throws Exception{
      FileInputStream    fis = new FileInputStream(fileName);
      BufferedInputStream bis = new BufferedInputStream(fis);
      DigestInputStream  dis = new DigestInputStream(bis, algorithm);

      while (dis.read() != -1);
            byte[] hash = algorithm.digest();

      return byteArray2Hex(hash);
}

It's work good on unix/windows when my .jar file haven't cyrillic characters in path. But when it have, I getting next exception:

java.io.FileNotFoundException: 
C:\Users\%d0%90%d1%80%d1%82%d1%83%d1%80\Documents\NetBeansProjects\artcraft-client\build\classes (Can't find file)

How I can fix it?

Charles
  • 50,943
  • 13
  • 104
  • 142
Arthur Halma
  • 3,943
  • 3
  • 23
  • 44

1 Answers1

2

This is from memory so the syntax may be a bit off, but the best is probably to use the built in URL support for opening streams directly from an URL;

URL url = LauncherFrame.class.getProtectionDomain().getCodeSource().getLocation();

then pass the URL to calculateHash instead of the filename and use URL's openStream method to get a stream with the content;

InputStream is = url.openStream();
BufferedInputStream bis = new BufferedInputStream(is);
...
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294