Skip to content

Instantly share code, notes, and snippets.

@mcgivrer
Last active March 7, 2025 19:32
Show Gist options
  • Save mcgivrer/c4a65916809f07915c5b7cfcb93422ed to your computer and use it in GitHub Desktop.
Save mcgivrer/c4a65916809f07915c5b7cfcb93422ed to your computer and use it in GitHub Desktop.
Java Application creation template
package $APP_PACKAGE_NAME;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.ResourceBundle;
/**
* Main class for Project $PROJECT_NAME
*
* @author ${AUTHOR_NAME} ${AUTHOR_EMAIL}
* @since ${APP_VERSION}
*/
public class ${APP_NAME}{
private ResourceBundle messages = ResourceBundle.getBundle("i18n/messages");
private Properties config = new Properties();
private static boolean exit = false;
private static int debug = 0;
private static String debugFilter = "";
private static String loggerFilter = "ERR,WARN,INFO";
public ${APP_NAME}(){
info(${APP_NAME}.class, "Initialization application %s (%s) %n- running on JDK %s %n- at %s %n- with classpath = %s%n",
messages.getString("app.title"),
messages.getString("app.version"),
System.getProperty("java.version"),
System.getProperty("java.home"),
System.getProperty("java.class.path"));
}
public void run(String[] args){
init(args);
loop();
dispose();
}
private void init(String[] args){
List<String> lArgs = Arrays.asList(args);
try {
config.load(this.getClass().getResourceAsStream("/config.properties"));
config.forEach((k, v) -> {
info($APP_NAME.class, "Configuration|%s=%s", k, v);
});
exit = Boolean.parseBoolean(config.getProperty("app.exit"));
} catch (IOException e) {
error(${APP_NAME}.class, "Configuration|Unable to read configuration file: %s", e.getMessage());
}
lArgs.forEach(s -> {
info(${APP_NAME}.class, "Configuration;Argument: %s", s);
});
}
private void loop(){
while(!exit){
// will loop until exit=true or CTRL+C
}
}
private void dispose(){
info(${APP_NAME}.class, "End of application %s (%s)",
messages.getString("app.name"),
messages.getString("app.version"));
}
public static void main(String[] argc){
${APP_NAME} app = new ${APP_NAME}();
app.run(argc);
}
public static void log(Class<?> className, String level, String message, Object... args) {
if (loggerFilter.contains(level)) {
String dateFormatted = DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(LocalDateTime.now());
System.out.printf(dateFormatted + "|" + className.getName() + "|" + level + "|" + message + "%n", args);
}
}
public static void debug(Class<?> className, String message, Object... args) {
log(className, "DEBUG", message, args);
}
public static void debug(Class<?> className, int level, String message, Object... args) {
if (level >= debug) {
log(className, "DEBUG", message, args);
}
}
public static void info(Class<?> className, String message, Object... args) {
log(className, "INFO", message, args);
}
public static void warn(Class<?> className, String message, Object... args) {
log(className, "WARN", message, args);
}
public static void error(Class<?> className, String message, Object... args) {
log(className, "ERR", message, args);
}
}
# build v5.1
# Copyright 2025 Frederic Delorme (McGivrer) fredericDOTdelormeATgmailDOTcom
project.name=$APP_NAME
project.title=$PROJECT_NAME
project.version=$APP_VERSION
project.main.class=$APP_PACKAGE_NAME.$APP_NAME
project.javadoc.classpath=$APP_PACKAGE_NAME
project.javadoc.packages=--group "$APP_NAME" $APP_PACKAGE_NAME
project.author.name=$AUTHOR_NAME
project.author.email=$AUTHOR_EMAIL
project.build.jdk.version=$JAVA_VERSION
project.build.jars=
app.name=$APP_NAME
app.vesion=$APP_VERSION
app.title=$PROJECT_NAME-($APP_VERSION)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment