Better error handling when running JARs in a directory with '!'.

Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
Jeffrey Han 2015-05-07 13:28:40 -04:00
parent 885158d3b7
commit 50d55d8d99
2 changed files with 43 additions and 7 deletions

View File

@ -135,7 +135,11 @@ public class Opsu extends StateBasedGame {
ResourceLoader.addResourceLocation(new FileSystemLocation(new File("./res/")));
// initialize databases
DBController.init();
try {
DBController.init();
} catch (UnsatisfiedLinkError e) {
errorAndExit(e, "The databases could not be initialized.");
}
// check if just updated
if (args.length >= 2)
@ -176,12 +180,7 @@ public class Opsu extends StateBasedGame {
}
}
} catch (SlickException e) {
// JARs will not run properly inside directories containing '!'
// http://bugs.java.com/view_bug.do?bug_id=4523159
if (new File("").getAbsolutePath().indexOf('!') != -1)
ErrorHandler.error("Cannot run JAR from path containing '!'.", null, false);
else
ErrorHandler.error("Error while creating game container.", e, true);
errorAndExit(e, "An error occurred while creating the game container.");
}
}
@ -242,4 +241,20 @@ public class Opsu extends StateBasedGame {
}
}
}
/**
* Throws an error and exits the application with the given message.
* @param e the exception that caused the crash
* @param message the message to display
*/
private static void errorAndExit(Throwable e, String message) {
// JARs will not run properly inside directories containing '!'
// http://bugs.java.com/view_bug.do?bug_id=4523159
if (Utils.isJarRunning() && Utils.getRunningDirectory() != null &&
Utils.getRunningDirectory().getAbsolutePath().indexOf('!') != -1)
ErrorHandler.error("JARs cannot be run from some paths containing '!'. Please move or rename the file and try again.", null, false);
else
ErrorHandler.error(message, e, true);
System.exit(1);
}
}

View File

@ -35,6 +35,7 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
@ -633,4 +634,24 @@ public class Utils {
return easeOut(t, a, b, d);
return easeOut(d - t, a, b, d);
}
/**
* Returns whether or not the application is running within a JAR.
* @return true if JAR, false if file
*/
public static boolean isJarRunning() {
return Opsu.class.getResource(String.format("%s.class", Opsu.class.getSimpleName())).toString().startsWith("jar:");
}
/**
* Returns the directory where the application is being run.
*/
public static File getRunningDirectory() {
try {
return new File(Opsu.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
} catch (URISyntaxException e) {
Log.error("Could not get the running directory.", e);
return null;
}
}
}