changeset 10758:2ec52c28f1d3

8193353: javapackager is totally broken in "-createbss" mode Reviewed-by: kcr
author vdrozdov
date Wed, 20 Dec 2017 14:58:38 -0800
parents 6e6bf169ee92
children c56facad487c
files modules/jdk.packager/src/main/java/com/oracle/tools/packager/IOUtils.java modules/jdk.packager/src/main/java/com/sun/javafx/tools/packager/Main.java modules/jdk.packager/src/main/java/com/sun/javafx/tools/packager/PackagerLib.java modules/jdk.packager/src/main/java/com/sun/javafx/tools/packager/bundlers/BundleParams.java
diffstat 4 files changed, 149 insertions(+), 126 deletions(-) [+]
line wrap: on
line diff
--- a/modules/jdk.packager/src/main/java/com/oracle/tools/packager/IOUtils.java	Tue Dec 19 20:24:18 2017 -0800
+++ b/modules/jdk.packager/src/main/java/com/oracle/tools/packager/IOUtils.java	Wed Dec 20 14:58:38 2017 -0800
@@ -34,6 +34,8 @@
 import java.nio.file.Path;
 import java.nio.file.SimpleFileVisitor;
 import java.nio.file.attribute.BasicFileAttributes;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * @deprecated use {@link ToolProvider} to locate the {@code "javapackager"} tool instead.
@@ -198,6 +200,46 @@
         }
     }
 
+    @SuppressWarnings("unchecked")
+    public static int execute(Object ... args) throws IOException, InterruptedException {
+        final ArrayList<String> argsList = new ArrayList<>();
+        for (Object a : args) {
+            if (a instanceof List) {
+                argsList.addAll((List)a);
+            } else if (a instanceof String) {
+                argsList.add((String)a);
+            }
+        }
+        final Process p = Runtime.getRuntime().exec(argsList.toArray(new String[argsList.size()]));
+        final BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
+        Thread t = new Thread(() -> {
+            try {
+                String line;
+                while ((line = in.readLine()) != null) {
+                    Log.info(line);
+                }
+            } catch (IOException ioe) {
+                com.oracle.tools.packager.Log.verbose(ioe);
+            }
+        });
+        t.setDaemon(true);
+        t.start();
+        final BufferedReader err = new BufferedReader(new InputStreamReader(p.getErrorStream()));
+        t = new Thread(() -> {
+            try {
+                String line;
+                while ((line = err.readLine()) != null) {
+                    Log.error(line);
+                }
+            } catch (IOException ioe) {
+                Log.verbose(ioe);
+            }
+        });
+        t.setDaemon(true);
+        t.start();
+        return p.waitFor();
+    }
+
     //no good test if we are running pre-JRE7
     //use heuristic approach
     // "false positive" is better than wrong answer
--- a/modules/jdk.packager/src/main/java/com/sun/javafx/tools/packager/Main.java	Tue Dec 19 20:24:18 2017 -0800
+++ b/modules/jdk.packager/src/main/java/com/sun/javafx/tools/packager/Main.java	Wed Dec 20 14:58:38 2017 -0800
@@ -74,6 +74,11 @@
     private static boolean signJar = false;
     private static boolean makeAll = false;
 
+    private static final String CREATE_BSS_INTERNAL = "-createbss_internal";
+    private static final String CREATE_BSS_EXTERNAL = "-createbss";
+    private static final String CREATE_JAR_INTERNAL = "-createjar_internal";
+    private static final String CREATE_JAR_EXTERNAL = "-createjar";
+
     private static void addResources(CommonParams commonParams,
                                      String srcdir, String srcfiles) {
         if (srcdir == null || "".equals(srcdir)) {
@@ -206,8 +211,53 @@
         return status;
     }
 
+    private static int relaunchJavapackager(String... args) throws Exception {
+
+        final String exe = (Platform.getPlatform() == Platform.WINDOWS) ? ".exe" : "";
+        String javaHome = System.getProperty("java.home");
+        if (javaHome == null) {
+            throw new PackagerException("ERR_MissingJavaHome");
+        }
+
+        final File java = new File(new File(javaHome), "bin/java" + exe);
+
+        // compose new arguments
+        List<String> newArgs = new ArrayList<>();
+        newArgs.add(java.getAbsolutePath());
+        newArgs.add("--add-modules");
+        newArgs.add("javafx.graphics");
+        newArgs.add("-m");
+        newArgs.add("jdk.packager/com.sun.javafx.tools.packager.Main");
+
+        for (String arg : args) {
+            if (arg.equalsIgnoreCase(CREATE_BSS_EXTERNAL)) {
+                newArgs.add(CREATE_BSS_INTERNAL);
+            } else if (arg.equalsIgnoreCase(CREATE_JAR_EXTERNAL)) {
+                newArgs.add(CREATE_JAR_INTERNAL);
+            } else {
+                newArgs.add(arg);
+            }
+        }
+
+        int ret = IOUtils.execute(newArgs);
+        if (ret != 0) {
+            throw new PackagerException(
+                    "Error: Conversion of CSS files to binary form failed");
+        }
+
+        return ret;
+    }
+
     @SuppressWarnings("deprecation")
     public static int run(String... args) throws Exception {
+
+       for (String arg : args) {
+            if (arg.equalsIgnoreCase(CREATE_BSS_EXTERNAL) ||
+                arg.equalsIgnoreCase(CREATE_JAR_EXTERNAL)) {
+                return relaunchJavapackager(args);
+            }
+        }
+
         BundleType bundleType = BundleType.NONE;
 
         if (args.length == 0 || args.length == 1 && args[0].equals("-help")) {
@@ -225,7 +275,7 @@
             String srcfiles = null;
 
             try {
-                if (args[0].equalsIgnoreCase("-createjar")) {
+                if (args[0].equalsIgnoreCase(CREATE_JAR_INTERNAL)) {
                     Log.info("Warning: -createjar has been deprecated and will be removed in a future release.");
                     for (int i = 1; i < args.length; i++) {
                         String arg = args[i];
@@ -415,7 +465,7 @@
                     }
 
                     addResources(deployParams, srcdir, srcfiles);
-                } else if (args[0].equalsIgnoreCase("-createbss")) {
+                } else if (args[0].equalsIgnoreCase(CREATE_BSS_INTERNAL)) {
                     Log.info("Warning: -createbss has been deprecated and will be removed in a future release.");
                     for (int i = 1; i < args.length; i++) {
                         String arg = args[i];
--- a/modules/jdk.packager/src/main/java/com/sun/javafx/tools/packager/PackagerLib.java	Tue Dec 19 20:24:18 2017 -0800
+++ b/modules/jdk.packager/src/main/java/com/sun/javafx/tools/packager/PackagerLib.java	Wed Dec 20 14:58:38 2017 -0800
@@ -30,6 +30,7 @@
 import com.oracle.tools.packager.Log;
 import com.oracle.tools.packager.Platform;
 import com.oracle.tools.packager.UnsupportedPlatformException;
+import com.oracle.tools.packager.IOUtils;
 import com.sun.javafx.tools.packager.JarSignature.InputStreamSource;
 import com.sun.javafx.tools.packager.bundlers.BundleParams;
 import com.sun.javafx.tools.packager.bundlers.Bundler.BundleType;
@@ -47,9 +48,6 @@
 import java.io.OutputStream;
 import java.io.Writer;
 import java.lang.reflect.Method;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.net.URLClassLoader;
 import java.nio.file.Files;
 import java.nio.file.StandardCopyOption;
 import java.security.InvalidKeyException;
@@ -98,20 +96,6 @@
 
     private enum Filter {ALL, CLASSES_ONLY, RESOURCES}
 
-    private ClassLoader classLoader;
-
-    private ClassLoader getClassLoader() throws PackagerException {
-        if (classLoader == null) {
-            try {
-                URL[] urls = {new URL(getJfxrtPath())};
-                classLoader = URLClassLoader.newInstance(urls);
-            } catch (MalformedURLException ex) {
-                throw new PackagerException(ex, "ERR_CantFindRuntime");
-            }
-        }
-        return classLoader;
-    }
-
     //  if set of input resources consist of SINGLE element and
     //   this element is jar file then we expect this to be request to
     //   "update" jar file
@@ -517,14 +501,6 @@
 
         final File javac = new File(new File(jHome), "bin/javac" + exe);
 
-        String jfxHome = System.getenv("JAVAFX_HOME");
-        if (jfxHome == null) {
-            jfxHome = System.getProperty("javafx.home");
-        }
-        if (jfxHome == null) {
-            throw new PackagerException("ERR_MissingJavaFxHome");
-        }
-
         final String srcDirName = "src";
         final String compiledDirName = "compiled";
         final String distDirName = "dist";
@@ -542,23 +518,31 @@
             try (FileWriter sources = new FileWriter(tmpFile)) {
                 scanAndCopy(new PackagerResource(new File(srcDirName), "."), sources, compiledDir);
             }
-            String classpath = jfxHome + "/../lib/jfxrt.jar";
-            if (makeAllParams.classpath != null) {
-                classpath += File.pathSeparator + makeAllParams.classpath;
-            }
+
             if (makeAllParams.verbose) {
                 Log.info("Executing javac:");
                 Log.infof("%s %s %s %s %s %s%n",
                         javac.getAbsolutePath(),
                         "-d", compiledDirName,
-                        "-cp", classpath,
+                        "-cp", makeAllParams.classpath != null ?
+                                makeAllParams.classpath : "<null>",
                         "@" + tmpFile.getAbsolutePath());
             }
-            int ret = execute(
-                    javac.getAbsolutePath(),
-                    "-d", compiledDirName,
-                    "-cp", classpath,
-                    "@" + tmpFile.getAbsolutePath());
+
+            int ret = -1;
+            if (makeAllParams.classpath != null) {
+                ret = IOUtils.execute(
+                        javac.getAbsolutePath(),
+                        "-d", compiledDirName,
+                        "-cp", makeAllParams.classpath,
+                        "@" + tmpFile.getAbsolutePath());
+            } else {
+                ret = IOUtils.execute(
+                        javac.getAbsolutePath(),
+                        "-d", compiledDirName,
+                        "@" + tmpFile.getAbsolutePath());
+            }
+
             if (ret != 0) {
                 throw new PackagerException("ERR_JavacFailed", Integer.toString(ret));
             }
@@ -597,46 +581,6 @@
         deleteDirectory(compiledDir);
     }
 
-    @SuppressWarnings("unchecked")
-    private static int execute(Object ... args) throws IOException, InterruptedException {
-        final ArrayList<String> argsList = new ArrayList<>();
-        for (Object a : args) {
-            if (a instanceof List) {
-                argsList.addAll((List)a);
-            } else if (a instanceof String) {
-                argsList.add((String)a);
-            }
-        }
-        final Process p = Runtime.getRuntime().exec(argsList.toArray(new String[argsList.size()]));
-        final BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
-        Thread t = new Thread(() -> {
-            try {
-                String line;
-                while ((line = in.readLine()) != null) {
-                    Log.info(line);
-                }
-            } catch (IOException ioe) {
-                com.oracle.tools.packager.Log.verbose(ioe);
-            }
-        });
-        t.setDaemon(true);
-        t.start();
-        final BufferedReader err = new BufferedReader(new InputStreamReader(p.getErrorStream()));
-        t = new Thread(() -> {
-            try {
-                String line;
-                while ((line = err.readLine()) != null) {
-                    Log.error(line);
-                }
-            } catch (IOException ioe) {
-                Log.verbose(ioe);
-            }
-        });
-        t.setDaemon(true);
-        t.start();
-        return p.waitFor();
-    }
-
     private static void scanAndCopy(PackagerResource dir, Writer out, File outdir) throws PackagerException {
         if (!dir.getFile().exists()) {
             throw new PackagerException("ERR_MissingDirectory", dir.getFile().getName());
@@ -833,34 +777,6 @@
         }
     }
 
-    // Returns path to jfxrt.jar relatively to jar containing PackagerLib.class
-    private String getJfxrtPath() throws PackagerException {
-        String theClassFile = "PackagerLib.class";
-        Class theClass = PackagerLib.class;
-        String classUrl = theClass.getResource(theClassFile).toString();
-
-        if (!classUrl.startsWith("jar:file:") || !classUrl.contains("!")){
-            throw new PackagerException("ERR_CantFindRuntime");
-        }
-
-        // Strip everything after and including the "!"
-        classUrl = classUrl.substring(0, classUrl.lastIndexOf("!"));
-        // Strip everything after the last "/" or "\" to get rid of the jar filename
-        int lastIndexOfSlash = Math.max(classUrl.lastIndexOf("/"), classUrl.lastIndexOf("\\"));
-
-        return classUrl.substring(0, lastIndexOfSlash)
-                + "/../lib/jfxrt.jar!/";
-    }
-
-    private Class loadClassFromRuntime(String className) throws PackagerException {
-        try {
-            ClassLoader cl = getClassLoader();
-            return cl.loadClass(className);
-        } catch (ClassNotFoundException ex) {
-            throw new PackagerException(ex, "ERR_CantFindRuntime");
-        }
-    }
-
     private void createBinaryCss(String cssFile, String binCssFile) throws PackagerException {
         String ofname = (binCssFile != null)
                 ? binCssFile
@@ -874,14 +790,15 @@
         }
 
         // Using reflection because CSS parser is part of runtime
-        // and we want to avoid dependency on jfxrt during build
+        // and we want to avoid dependency on javafx.graphics
         Class<?> clazz;
         try {
             clazz = Class.forName("com.sun.javafx.css.parser.Css2Bin");
         } catch (ClassNotFoundException e) {
-            // class was not found with default class loader, trying to
-            // locate it by loading from jfxrt.jar
-            clazz = loadClassFromRuntime("com.sun.javafx.css.parser.Css2Bin");
+            Throwable causeEx = e.getCause();
+            String cause = (causeEx != null) ? causeEx.getMessage()
+                    : bundle.getString("ERR_UnknownReason");
+            throw new PackagerException(e, "ERR_BSSConversionFailed", cssFile, cause);
         }
 
         try {
--- a/modules/jdk.packager/src/main/java/com/sun/javafx/tools/packager/bundlers/BundleParams.java	Tue Dec 19 20:24:18 2017 -0800
+++ b/modules/jdk.packager/src/main/java/com/sun/javafx/tools/packager/bundlers/BundleParams.java	Wed Dec 20 14:58:38 2017 -0800
@@ -356,34 +356,48 @@
         return ARGUMENTS.fetchFrom(params);
     }
 
-    //Validation approach:
+    // Validation approach:
+    //  - javac and
+    //
+    //  - /jmods dir
+    // or
     //  - JRE marker (rt.jar)
     //  - FX marker (jfxrt.jar)
     //  - JDK marker (tools.jar)
     private static boolean checkJDKRoot(File jdkRoot) {
-        File rtJar = new File(jdkRoot, "jre/lib/rt.jar");
-        if (!rtJar.exists()) {
-            Log.verbose("rt.jar is not found at " + rtJar.getAbsolutePath());
+        String exe = (Platform.getPlatform() == Platform.WINDOWS) ? ".exe" : "";
+        File javac = new File(jdkRoot, "bin/javac" + exe);
+        if (!javac.exists()) {
+            Log.verbose("javac is not found at " + javac.getAbsolutePath());
             return false;
         }
 
-        File jfxJar = new File(jdkRoot, "jre/lib/ext/jfxrt.jar");
-        if (!jfxJar.exists()) {
-            //Try again with new location
-            jfxJar = new File(jdkRoot, "jre/lib/jfxrt.jar");
+        File jmods = new File(jdkRoot, "jmods");
+        if (!jmods.exists()) {
+            // old non-modular JDKs
+            File rtJar = new File(jdkRoot, "jre/lib/rt.jar");
+            if (!rtJar.exists()) {
+                Log.verbose("rt.jar is not found at " + rtJar.getAbsolutePath());
+                return false;
+            }
+
+            File jfxJar = new File(jdkRoot, "jre/lib/ext/jfxrt.jar");
             if (!jfxJar.exists()) {
-                Log.verbose("jfxrt.jar is not found at " + jfxJar.getAbsolutePath());
+                //Try again with new location
+                jfxJar = new File(jdkRoot, "jre/lib/jfxrt.jar");
+                if (!jfxJar.exists()) {
+                    Log.verbose("jfxrt.jar is not found at " + jfxJar.getAbsolutePath());
+                    return false;
+                }
+            }
+
+
+            File toolsJar = new File(jdkRoot, "lib/tools.jar");
+            if (!toolsJar.exists()) {
+                Log.verbose("tools.jar is not found at " + toolsJar.getAbsolutePath());
                 return false;
             }
         }
-
-
-        File toolsJar = new File(jdkRoot, "lib/tools.jar");
-        if (!toolsJar.exists()) {
-            Log.verbose("tools.jar is not found at " + toolsJar.getAbsolutePath());
-            return false;
-        }
-
         return true;
     }