#!/bin/bash # javafx: compile and run javafx applications # usage: javafx [package] [mainclass] - folders and .java are added # Must set JAVAFX_LIB environment variable to lib directory for JavaFX # If there's a src directory, it's assumed all code is under that # If there's a src subdirectory (such as for Intellij), cd's into that first # if [ "$JAVAFX_LIB" == "" ]; then export JAVAFX_LIB=$PATH_TO_FX; fi export build="." if [ -d src ]; then export build="src" fi build_subdirs=(`find "$build" -mindepth 1 -maxdepth 1 -type d -not -name '.*' -exec basename {} \;`) if [ "$1" == "" ] && [ ${#build_subdirs[@]} -eq 1 ]; then export package="${build_subdirs[0]}" export main=Main elif [ "$1" == "" ]; then export package="" export main=Main elif [ "$2" != "" ]; then export package="$1" export main="$2" # remaining cases: $2 is empty, so is $1 a package or class name? elif [ -d "$build"/"$1" ]; then export package="$1" export main="Main" else export package="" export main="$1" fi #echo "BUILD SUBDIRS: ${build_subdirs[@]}" #echo "PACKAGE: $package" #echo "MAIN: $main" if [ "$package" == "" ]; then (cd $build; set -x; javac --module-path "$JAVAFX_LIB" --add-modules=javafx.controls,javafx.fxml *.java) if [ $? -eq 0 ]; then (set -x; java -cp "$build" --module-path "$JAVAFX_LIB" --add-modules=javafx.controls,javafx.fxml "$main") fi else (cd $build; set -x; javac --module-path "$JAVAFX_LIB" --add-modules=javafx.controls,javafx.fxml "$package"/*.java) if [ $? -eq 0 ]; then (set -x; java -cp "$build" --module-path "$JAVAFX_LIB" --add-modules=javafx.controls,javafx.fxml "$package"."$main") fi fi # alternative ways to look for files in src directory (that doesn't use find): #build_dir_entries=($(shopt -s nullglob; echo "$build"/*)) #if [ "$1" == "" ] && [ ${#build_dir_entries[@]} -eq 1 ] && [ -d ${build_dir_entries[0]} ]; then # export package="${build_dir_entries[0]}" # export main=Main