Path: blob/master/test/jdk/tools/jpackage/apps/installer/Hello.java
41153 views
/*1* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.BufferedWriter;24import java.io.File;25import java.io.FileWriter;26import java.io.PrintWriter;27import java.awt.Desktop;28import java.awt.desktop.OpenFilesEvent;29import java.awt.desktop.OpenFilesHandler;30import java.util.List;3132public class Hello implements OpenFilesHandler {3334private static final String MSG = "jpackage test application";35private static final int EXPECTED_NUM_OF_PARAMS = 3; // Starts at 136private static List<File> files;3738public static void main(String[] args) {39if(Desktop.getDesktop().isSupported(Desktop.Action.APP_OPEN_FILE)) {40Desktop.getDesktop().setOpenFileHandler(new Hello());41try {42Thread.sleep(1000);43} catch (InterruptedException e) {44e.printStackTrace();45}46}47printToStdout(args);48if (args.length == 1 || (files != null && files.size() == 1)) { // Called via file association49printToFile(args);50}51}5253private static void printToStdout(String[] args) {54System.out.println(MSG);5556System.out.println("args.length: " + (files == null ? args.length : args.length + files.size()));5758for (String arg : args) {59System.out.println(arg);60}6162if (files != null) {63for (File file : files) {64System.out.println(file.getAbsolutePath());65}66}6768for (int index = 1; index <= EXPECTED_NUM_OF_PARAMS; index++) {69String value = System.getProperty("param" + index);70if (value != null) {71System.out.println("-Dparam" + index + "=" + value);72}73}74}7576private static void printToFile(String[] args) {77File inputFile = files == null ? new File(args[0]) : files.get(0);78String outputFile = inputFile.getParent() + File.separator + "appOutput.txt";79File file = new File(outputFile);8081try (PrintWriter out82= new PrintWriter(new BufferedWriter(new FileWriter(file)))) {83out.println(MSG);8485out.println("args.length: " + (files == null ? args.length : args.length + files.size()));8687for (String arg : args) {88out.println(arg);89}9091if (files != null) {92for (File f : files) {93out.println(f.getAbsolutePath());94}95}9697for (int index = 1; index <= EXPECTED_NUM_OF_PARAMS; index++) {98String value = System.getProperty("param" + index);99if (value != null) {100out.println("-Dparam" + index + "=" + value);101}102}103} catch (Exception ex) {104System.err.println(ex.getMessage());105}106}107108@Override109public void openFiles(OpenFilesEvent e) {110files = e.getFiles();111}112}113114115