1 /* 2 * Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.jpackage.main; 27 28 import jdk.jpackage.internal.Arguments; 29 import jdk.jpackage.internal.Log; 30 import jdk.jpackage.internal.CLIHelp; 31 import java.io.PrintWriter; 32 import java.util.ResourceBundle; 33 34 public class Main { 35 36 private static final ResourceBundle bundle = ResourceBundle.getBundle( 37 "jdk.jpackage.internal.resources.MainResources"); 38 39 private static final String version = bundle.getString("MSG_Version") 40 + " " + System.getProperty("java.version"); 41 42 /** 43 * main(String... args) 44 * This is the entry point for the jpackage tool. 45 * 46 * @param args command line arguments 47 */ 48 public static void main(String... args) throws Exception { 49 // Create logger with default system.out and system.err 50 Log.Logger logger = new Log.Logger(false); 51 Log.setLogger(logger); 52 53 int status = run(args); 54 System.exit(status); 55 } 56 57 /** 58 * run() - this is the entry point for the ToolProvider API. 59 * 60 * @param out output stream 61 * @param err error output stream 62 * @param args command line arguments 63 * @return an exit code. 0 means success, non-zero means an error occurred. 64 */ 65 public static int run(PrintWriter out, PrintWriter err, String... args) 66 throws Exception { 67 // Create logger with provided streams 68 Log.Logger logger = new Log.Logger(false); 69 logger.setPrintWriter(out, err); 70 Log.setLogger(logger); 71 72 int status = run(args); 73 Log.flush(); 74 return status; 75 } 76 77 private static int run(String... args) throws Exception { 78 String[] newArgs = CommandLine.parse(args); 79 if (newArgs.length == 0) { 80 CLIHelp.showHelp(true); 81 } else if (hasHelp(newArgs)){ 82 if (hasVersion(newArgs)) { 83 Log.info(version + "\n"); 84 } 85 CLIHelp.showHelp(false); 86 } else if (hasVersion(newArgs)) { 87 Log.info(version); 88 } else { 89 try { 90 Arguments arguments = new Arguments(newArgs); 91 if (!arguments.processArguments()) { 92 // processArguments() should log error message if failed. 93 return -1; 94 } 95 } catch (Exception e) { 96 if (Log.isVerbose()) { 97 Log.verbose(e); 98 } else { 99 Log.error(e.getMessage()); 100 if (e.getCause() != null && e.getCause() != e) { 101 Log.error(e.getCause().getMessage()); 102 } 103 } 104 return -1; 105 } 106 } 107 108 return 0; 109 } 110 111 private static boolean hasHelp(String[] args) { 112 for (String a : args) { 113 if ("--help".equals(a) || "-h".equals(a)) { 114 return true; 115 } 116 } 117 return false; 118 } 119 120 private static boolean hasVersion(String[] args) { 121 for (String a : args) { 122 if ("--version".equals(a)) { 123 return true; 124 } 125 } 126 return false; 127 } 128 129 }