[libbluray-devel] [Git][videolan/libbluray][master] Initial support for Java 18.

Petri Hintukainen (@hpi) gitlab at videolan.org
Mon Aug 1 07:42:55 UTC 2022



Petri Hintukainen pushed to branch master at VideoLAN / libbluray


Commits:
3187c308 by Petri Hintukainen at 2022-07-31T23:31:56+03:00
Initial support for Java 18.

Workaround for Java 18 security manager issues:
  Suggested method to enable SecurityManager support in Java 18 is
  "-Djava.security.manager=allowed" command-line argument.
  Of course this fails with older Java versions, and detecting Java
  version is hard before launching the JVM...

- - - - -


2 changed files:

- ChangeLog
- src/libbluray/bdj/java/org/videolan/Libbluray.java


Changes:

=====================================
ChangeLog
=====================================
@@ -1,3 +1,5 @@
+- Initial support for Java 18.
+
 2022-06-28: Version 1.3.2
 - Fix build/run failure after Oracle Java CPU for April 2022
 


=====================================
src/libbluray/bdj/java/org/videolan/Libbluray.java
=====================================
@@ -119,6 +119,47 @@ public class Libbluray {
      *
      */
 
+    public static int getJavaMajor() {
+        try {
+            String ver = System.getProperty("java.version");
+            if (ver.startsWith("1."))
+                ver = ver.substring(2, 3);
+            if (ver.indexOf(".") != -1)
+                ver = ver.substring(0, ver.indexOf("."));
+            if (ver.indexOf("-") != -1)
+                ver = ver.substring(0, ver.indexOf("-"));
+            return Integer.parseInt(ver);
+        } catch (Throwable t) {
+            System.err.println("getJavaMajor(): " + t);
+        }
+        return 0;
+    }
+
+    private static void setSecurityManager(SecurityManager sm) throws Exception {
+        try {
+            System.setSecurityManager(sm);
+            return;
+        } catch (Exception ex) {
+            if (getJavaMajor() < 18)
+                throw ex;
+
+            /* Workaround for Java 18 security manager issues.
+             *   Suggested method (setting security manager to "allow" in command line args)
+             *   fails with older Java versions.
+             *   And, Java version is hard to figure out before launching JVM.
+             */
+            System.err.println("Detected Java >= 18, trying setSecurityManager() workaround");
+            try {
+                java.lang.reflect.Method method = System.class.getDeclaredMethod("implSetSecurityManager", SecurityManager.class);
+                method.setAccessible(true);
+                method.invoke(null, sm);
+                return;
+            } catch (Exception ex2) {
+                throw ex;
+            }
+        }
+    }
+
     private static boolean initOnce = false;
     private static void initOnce() {
         if (initOnce) {
@@ -349,7 +390,7 @@ public class Libbluray {
         System.setProperty("bluray.network.connected", "YES");
 
         try {
-            System.setSecurityManager(new BDJSecurityManager(discRoot, persistentRoot, budaRoot));
+            setSecurityManager(new BDJSecurityManager(discRoot, persistentRoot, budaRoot));
         } catch (Exception ex) {
             System.err.println("System.setSecurityManager() failed: " + ex);
             throw new SecurityException("Failed initializing SecurityManager");
@@ -380,7 +421,7 @@ public class Libbluray {
 
             /* all Xlet contexts (and threads) should be terminated now */
             try {
-                System.setSecurityManager(null);
+                setSecurityManager(null);
             } catch (Exception ex) {
                 System.err.println("System.setSecurityManager(null) failed: " + ex);
             }



View it on GitLab: https://code.videolan.org/videolan/libbluray/-/commit/3187c3080096e107f0a27eed1843232b58342577

-- 
View it on GitLab: https://code.videolan.org/videolan/libbluray/-/commit/3187c3080096e107f0a27eed1843232b58342577
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the libbluray-devel mailing list