Java tutorial
/* * #%L * ********************************************************************** * NAME : Michael Jedich * PROJECT : jre-version-checker * FILENAME : CommandLineInterpreter.java * ********************************************************************** * %% * Copyright (C) 2014 Michael Jedich * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * #L% */ import java.io.File; import java.util.Scanner; import javax.swing.JOptionPane; import org.apache.commons.lang.SystemUtils; /** * User commandline-input will get parsed here. * * @author Michael Jedich * @version 0.7.0 */ public class CommandLineInterpreter { /** * Show simple help screen. * * @param mainError * Error String if a specific error occurs. */ private static void printHelp(final String mainError) { StringBuffer help = new StringBuffer(); if (mainError != null) { System.err.println("ERROR:\n\t" + mainError + "! See help below.\n"); try { Thread.sleep(100); } catch (InterruptedException e) { } } help.append("NAME:\n\tjre-version-checker - A simple JRE version checker.\n\n"); help.append("USAGE:\n\tjre-version-checker.jar jre_version [resources_file] [-gui]\n\n"); help.append("DESCRIPTION:"); help.append("\n\t- TODO.\n"); System.out.println(help.toString()); } /** * * * @param version */ private static boolean checkJREVersion(final float minVersion, final boolean guiAlert) { if (!SystemUtils.isJavaVersionAtLeast(minVersion)) { String message = "You need at least Java " + minVersion + " to run the application.\n" + "\tYour Java version is " + SystemUtils.JAVA_VERSION + "."; alert(message, guiAlert); return false; } return true; } /** * * * @param file * @param guiAlert * @return */ public static boolean checkResources(final File file, final boolean guiAlert) { Scanner input = null; String message = null; try { input = new Scanner(file); while (input.hasNextLine()) { String currentFilePath = input.nextLine().trim(); if (!currentFilePath.isEmpty()) { File currentFile = new File(currentFilePath); if (!currentFile.exists() || !currentFile.canRead() || !currentFile.canWrite()) { message = "Can not read/write resource file:\n\"" + currentFile.getAbsolutePath() + "\""; alert(message, guiAlert); return false; } } } } catch (Exception e) { // TODO: logging e.printStackTrace(); } finally { if (input != null) { input.close(); } } return true; } /** * * * @param message * @param guiAlert */ private static void alert(String message, final boolean guiAlert) { if (message != null) { System.out.println("[ERROR] " + message); if (guiAlert) { JOptionPane.showMessageDialog(null, message); } } } /** * Main method, command line input will get parsed here. * * @param args */ public static void main(String[] args) { // // test-arguments: // args = new String[] { "1.9", "-gui" }; boolean success = false; if (args.length == 1) { String arg = args[0].trim().replaceAll("[-]+", ""); if (arg.equals("help") || arg.equals("h")) printHelp(null); } if (args.length == 0) { printHelp("ONE ARGUMENT NEEDED"); } else { try { boolean guiAlert = false; Float minVersion = null; File resourcesFile = null; // ------------------------------------------------ // // -- // ------------------------------------------------ // final String minJavaVersionArgument = args[0]; if (!minJavaVersionArgument.trim().isEmpty()) { try { minVersion = Float.parseFloat(minJavaVersionArgument); } catch (Exception e) { // do nothing } } if (minVersion == null || minVersion > 2 || minVersion < 1.6) { printHelp("VERSION STRING IS NOT VALID"); } // ------------------------------------------------ // // -- // ------------------------------------------------ // for (int i = 1; i < (args.length <= 3 ? args.length : 3); i++) { final String argument = args[i].trim(); if (argument.equals("-gui")) { guiAlert = true; } else { String resourcesFilePath = argument; if (!resourcesFilePath.isEmpty()) { resourcesFile = new File(resourcesFilePath); if (!resourcesFile.exists() || !resourcesFile.isFile() || !resourcesFile.canRead()) { printHelp("RESOURCES FILE IS NOT VALID\n[" + resourcesFile.getAbsolutePath() + "]"); } } } } // ------------------------------------------------ // // -- // ------------------------------------------------ // success = checkJREVersion(minVersion, guiAlert); if (success && resourcesFile != null) { success = checkResources(resourcesFile, guiAlert); } } catch (Exception e) { success = false; e.printStackTrace(); } } if (!success) { // set error exit code System.exit(1); } } }