Skip to content

Commit

Permalink
Fix problems with resource loading from executable Jar
Browse files Browse the repository at this point in the history
When running from a Jar, relative resource paths are not supported for some reason, which led to excpetions at startup.
http://stackoverflow.com/questions/24841062/javafx-resource-cannot-be-loaded-when-running-from-jar

Resolves:
#14
  • Loading branch information
jbion committed Feb 8, 2016
1 parent 19913e5 commit f3b75ac
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ sourceCompatibility = '1.8'

description = 'A JavaFX program to view log files in a beautiful way.'

def mainClassName = "org.hildan.fxlog.Main"
def mainClassName = "org.hildan.fxlog.FXLog"

repositories { mavenCentral() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,25 @@
import org.hildan.fxlog.controllers.MainController;
import org.hildan.fxlog.errors.ErrorDialog;

public class Main extends Application {
public class FXLog extends Application {

private static final String BASE_PACKAGE = '/' + FXLog.class.getPackage().getName().replace('.', '/');

private static final String VIEWS_PATH = BASE_PACKAGE + "/view";

private static final String CSS_PATH = BASE_PACKAGE;

@Override
public void start(Stage stage) {
// fail gracefully on any thread with a dialog
Thread.setDefaultUncaughtExceptionHandler((t, e) -> Platform.runLater(() -> ErrorDialog.uncaughtException(e)));
Thread.currentThread().setUncaughtExceptionHandler((t, e) -> ErrorDialog.uncaughtException(e));
try {
URL url = getClass().getResource("view/main.fxml");
URL url = getClass().getResource(BASE_PACKAGE + "/view/main.fxml");
FXMLLoader loader = new FXMLLoader(url);
Parent root = loader.load();
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("dark_theme.css").toExternalForm());
scene.getStylesheets().add(getClass().getResource(BASE_PACKAGE + "/dark_theme.css").toExternalForm());
stage.setTitle("FX Log");
stage.setScene(scene);
stage.show();
Expand All @@ -39,6 +45,36 @@ public void start(Stage stage) {
}
}

/**
* Loads the given view.
*
* @param viewFilename
* the name of the view. It can be a path relative to the views package.
* @return the Parent returned by the FXMLLoader
* @throws IOException
* if the resource couldn't be loaded for some reason
*/
public static Parent loadView(String viewFilename) throws IOException {
URL url = FXLog.class.getResource(VIEWS_PATH + '/' + viewFilename);
return FXMLLoader.load(url);
}

/**
* Loads the given CSS file and returns it as a style string.
*
* @param cssFilename
* the name of the CSS file. It can be a path relative to the CSS package.
* @return the CSS as a style string
*/
public static String getCss(String cssFilename) {
String path = CSS_PATH + '/' + cssFilename;
URL url = FXLog.class.getResource(path);
if (url == null) {
throw new RuntimeException(String.format("Cannot find CSS stylesheet '%s'", path));
}
return url.toExternalForm();
}

private void autoOpenFile(MainController controller) {
List<String> params = getParameters().getRaw();
if (!params.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import javafx.stage.Stage;

import org.apache.commons.io.input.Tailer;
import org.hildan.fxlog.FXLog;
import org.hildan.fxlog.coloring.ColorizedRowFactory;
import org.hildan.fxlog.coloring.Colorizer;
import org.hildan.fxlog.columns.Columnizer;
Expand Down Expand Up @@ -243,14 +244,14 @@ private void configureRecentFilesMenu() {
private void configureColorizersStage() {
colorizersStage = new Stage();
try {
Parent root = FXMLLoader.load(getClass().getResource("../view/colorizers.fxml"));
Parent root = FXLog.loadView("colorizers.fxml");
Scene scene = new Scene(root);
colorizersStage.setTitle("Customize Colorizers");
colorizersStage.setScene(scene);
editColorizersBtn.disableProperty().bind(colorizersStage.showingProperty());
List<String> styles = scene.getStylesheets();
styles.clear();
styles.add(getClass().getResource("../light_theme.css").toExternalForm());
styles.add(FXLog.getCss("light_theme.css"));
} catch (IOException e) {
ErrorDialog.uncaughtException(e);
}
Expand Down Expand Up @@ -421,7 +422,7 @@ public void selectDarkTheme() {
Arrays.asList(mainPane.getScene().getStylesheets(), colorizersStage.getScene().getStylesheets());
for (List<String> style : styles) {
style.clear();
style.add(getClass().getResource("../dark_theme.css").toExternalForm());
style.add(FXLog.getCss("dark_theme.css"));
}
}

Expand All @@ -433,7 +434,7 @@ public void selectBrightTheme() {
Arrays.asList(mainPane.getScene().getStylesheets(), colorizersStage.getScene().getStylesheets());
for (List<String> style : styles) {
style.clear();
style.add(getClass().getResource("../light_theme.css").toExternalForm());
style.add(FXLog.getCss("light_theme.css"));
}
}

Expand Down

0 comments on commit f3b75ac

Please sign in to comment.