Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Moving to Module: Issues & Solutions

Project Jigsaw
Moving to Module: Issues & Solutions

  • Be the first to comment

Moving to Module: Issues & Solutions

  1. 1. Moving to Module Issues & Solutions Project Jigsaw Java in the Box Yuichi Sakuraba
  2. 2. Module Issues of Non-Modular Software Issues of Modular Software
  3. 3. Motivation
  4. 4. -classpath rt.jar
  5. 5. -classpath Issues public is TOO Public TOO Many Libraries Which Libs Use which Libs? Want to Hide “public” Class
  6. 6. rt.jar Issue rt.jar (Java SE 8) Standard Library is TOO Huge
  7. 7. -classpath Issues rt.jar Issue Introduce Module { }Dependency Disclosure Scope Divide into Modules According to Features
  8. 8. Module JAR module-info.java Dependency Disclosure Scope
  9. 9. Build javac --module-path [directory] [source file] -p or jar --create --file [JAR filename] -C [class file directory] .
  10. 10. Build javac --module-path [directory] [source file] -p or jar --create --file [JAR filename] -C [class file directory] . Execution java -p [directory] --module [module]/[main class] -m or
  11. 11. java -p mods -m net.javainthebox.jigsawsample/net.javainthebox.sample.Main jigsawsample
  12. 12. java -p mods -m net.javainthebox.jigsawsample/net.javainthebox.sample.Main jigsawsample javafx.media javafx.controls
  13. 13. java -p mods -m net.javainthebox.jigsawsample/net.javainthebox.sample.Main jigsawsample javafx.media javafx.controls javafx.graphics javafx.base
  14. 14. java -p mods -m net.javainthebox.jigsawsample/net.javainthebox.sample.Main jigsawsample javafx.media javafx.controls javafx.graphics javafx.base java.desktop java.xml jdk.jsobject ...... ...... ......
  15. 15. Tool Support IDE IntelliJ IDEA Eclipse NetBeans Only Nightly Build
  16. 16. Tool Support IDE IntelliJ IDEA Eclipse NetBeans Only Nightly Build Build Tool Maven Gradle Need to Write Options in Build File
  17. 17. jdeps Tool to Check Dependency
  18. 18. jdeps Tool to Check Dependency C:samplemods>jdeps -s jigsawsample.jar jigsawsample.jar -> java.base jigsawsample.jar -> java.logging jigsawsample.jar -> java.xml jigsawsample.jar -> javafx.base jigsawsample.jar -> javafx.controls jigsawsample.jar -> javafx.graphics jigsawsample.jar -> javafx.media
  19. 19. jdeps Tool to Check Dependency C:samplemods>jdeps --generate-module-info . jigsawsample.jar writing to .jigsawsamplemodule-info.java module jigsawsample { requires java.logging; requires javafx.base; requires javafx.controls; requires javafx.media; requires transitive java.xml; requires transitive javafx.graphics; exports net.javainthebox.sample; exports net.javainthebox.sample.internal; }
  20. 20. jdeps Tool to Check Dependency C:samplemods>jdeps --generate-module-info . jigsawsample.jar writing to .jigsawsamplemodule-info.java module jigsawsample { requires java.logging; requires javafx.base; requires javafx.controls; requires javafx.media; requires transitive java.xml; requires transitive javafx.graphics; exports net.javainthebox.sample; exports net.javainthebox.sample.internal; } Edit as Necessary
  21. 21. Issues of Non-Modular Software Non-Disclosure API Non-Standard Module
  22. 22. import com.sun.javafx.tk.FontMetrics; import com.sun.javafx.tk.Toolkit; import javafx.scene.text.Font; public class FontSample { public static void main(String... args) { Font font = Font.font(24); Toolkit toolkit = Toolkit.getToolkit(); FontMetrics metrics = toolkit.getFontLoader().getFontMetrics(font); System.out.println(metrics.getLineHeight()); } }
  23. 23. C:fxsample>javac FontSample.java FontSample.java:1: error: package com.sun.javafx.tk is not visible import com.sun.javafx.tk.FontMetrics; ^ (package com.sun.javafx.tk is declared in module javafx. graphics, which does not export it to the unnamed module) FontSample.java:2: error: package com.sun.javafx.tk is not visible import com.sun.javafx.tk.Toolkit; ^ (package com.sun.javafx.tk is declared in module javafx. graphics, which does not export it to the unnamed module) 2 errors
  24. 24. import com.sun.javafx.tk.FontMetrics; import com.sun.javafx.tk.Toolkit; import javafx.scene.text.Font; public class FontSample { public static void main(String... args) { Font font = Font.font(24); Toolkit toolkit = Toolkit.getToolkit(); FontMetrics metrics = toolkit.getFontLoader().getFontMetrics(font); System.out.println(metrics.getLineHeight()); } }
  25. 25. Non-Disclosure API Non Exported Packages com.sun... sun... java.awt.peer
  26. 26. Non-Disclosure API Non Exported Packages com.sun... sun... java.awt.peer Exceptional API sun.misc.Unsafe sun.reflect.Reflection (Remove in Future)
  27. 27. Solution add export by --add-exports option --add-exports module/package=target javac java
  28. 28. Solution add export by --add-exports option --add-exports module/package=target javac java when non-modular system target is ALL-UNNAMED ex. javac --add-exports javafx.graphics/com.sun.javafx.tk=ALL-UNNAMED FontSample.java
  29. 29. public class FontSample { public static void main(String... args) throws Exception { Font font = Font.font(24); FontMetrics metrics = Toolkit.getToolkit().getFontLoader().getFontMetrics(font); Class<FontMetrics> metricsClass = FontMetrics.class; Field lineHeight = metricsClass.getDeclaredField("lineHeight"); lineHeight.setAccessible(true); System.out.println(lineHeight.getFloat(metrics)); } }
  30. 30. C:fxsample>javac --add-exports javafx.graphics/com.sun.javafx.tk=ALL-UNNAMED FontSample.java C:fxsample>java --add-exports javafx.graphics/com.sun.javafx.tk=ALL-UNNAMED FontSample WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by FontSample (file:/C:/fxsa mple/) to field com.sun.javafx.tk.FontMetrics.lineHeight WARNING: Please consider reporting this to the maintainers of FontSample WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a futu re release 31.921875
  31. 31. public class FontSample { public static void main(String... args) throws Exception { Font font = Font.font(24); FontMetrics metrics = Toolkit.getToolkit().getFontLoader().getFontMetrics(font); Class<FontMetrics> metricsClass = FontMetrics.class; Field lineHeight = metricsClass.getDeclaredField("lineHeight"); lineHeight.setAccessible(true); System.out.println(lineHeight.getFloat(metrics)); } }
  32. 32. Non-Disclosure API Reflection Reflective access to public --add-exports
  33. 33. Non-Disclosure API Reflection Reflective access to public --add-exports Reflective access to non-public --add-opens in execution using setAccessible(true) ex. java --add-opens javafx.graphics/com.sun.javafx.tk=ALL-UNNAMED FontSample.java
  34. 34. import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import net.javainthebox.xml.Name; public class JAXBSample { public static void main(String... args) throws JAXBException { JAXBContext context = JAXBContext.newInstance("net.javainthebox.xml"); Unmarshaller unmarshaller = context.createUnmarshaller(); File file = new File("sakuraba.xml"); Name sakuraba = (Name)unmarshaller.unmarshal(file); System.out.println(sakuraba.getFirst() + " " + sakuraba.getLast()); } }
  35. 35. C:jaxbsample>javac JAXBSample.java netjavaintheboxxmlName.java:11: error: package javax.xml. bind.annotation is not visible import javax.xml.bind.annotation.XmlAccessType; ^ (package javax.xml.bind.annotation is declared in module java.xml.bind, which is not in the module graph) <<skip the rest>>
  36. 36. import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import net.javainthebox.xml.Name; public class JAXBSample { public static void main(String... args) throws JAXBException { JAXBContext context = JAXBContext.newInstance("net.javainthebox.xml"); Unmarshaller unmarshaller = context.createUnmarshaller(); File file = new File("sakuraba.xml"); Name sakuraba = (Name)unmarshaller.unmarshal(file); System.out.println(sakuraba.getFirst() + " " + sakuraba.getLast()); } }
  37. 37. Non-Standard Module Modules not included in standard modulepath
  38. 38. Non-Standard Module Modules not included in standard modulepath JAXB JAX-WS CORBA java.xml.bind java.xml.ws java.corba These modules will be removed in future You should use the modules supplied by Java EE
  39. 39. Non-Standard Module Modules not included in standard modulepath JAXB JAX-WS CORBA java.xml.bind java.xml.ws java.corba These modules will be removed in future You should use the modules supplied by Java EE Incubator jdk.incubator.httpclient
  40. 40. Solution Add Modules by --add-modules option --add-modules module javac java ex) java --add-modules java.xml.bind JAXBSample
  41. 41. Issues of Modular Software modulepath classpath
  42. 42. In Java SE 8, Only classpath is Used
  43. 43. In Java SE 8, Only classpath is Used Module and Non-Module are Used In Java SE 9, modulepath classpath
  44. 44. In Java SE 8, Only classpath is Used Module and Non-Module are Used In Java SE 9, modulepath classpath specified in module-info.java Module CAN Access Only Modules
  45. 45. foo.jar bar.jarmyapp.jar Module Non-Module
  46. 46. foo.jar bar.jarmyapp.jar Module Non-Module Access Directly: Automatic Module Access Indirectly: Unnamed Module
  47. 47. foo.jar bar.jarmyapp.jar Module Non-Module Access Directly: Automatic Module Access Indirectly: Unnamed Module Automatic Unnamed
  48. 48. foo.jar bar.jarmyapp.jar Module Non-Module Automatic Unnamed modulepath classpath
  49. 49. foo.jar bar.jarmyapp.jar Module Non-Module Automatic Unnamed modulepath classpath Automatic Module: Named Module Unnamed Module: No Name Modu MANIFEST.MF Automatic-Module-Name or JAR filename Automatic Module Name is Defined by
  50. 50. foo.jar bar.jarmyapp.jar Module Non-Module Automatic Unnamed modulepath classpath module myapp { requires foo; } > java -cp bar.jar -p mod -m myapp/myapp.Main
  51. 51. foo.jar bar.jarmyapp.jar java.xml.bind Module Non-Module Automatic Module Can’ Describe Dependency t
  52. 52. foo.jar bar.jarmyapp.jar java.xml.bind Module Non-Module Automatic Module Can’ Describe Dependency t > java -cp bar.jar -p mods --add-modules java.xml.bind -m myapp/myapp.Main
  53. 53. foo.jar bar.jarmyapp.jar java.xml.bind Module Non-Module Automatic Module Can’ Describe Dependency t > java -cp bar.jar -p mods --add-modules java.xml.bind -m myapp/myapp.Main > java -cp bar.jar -p mods --add-modules ALL-MODULE-PATH -m myapp/myapp.Main If You Don’ t Know Dependencies
  54. 54. Conclusion Non-Disclosure API Issues of Non-Modular Software Non-Standard Module May Remove These APIs/Modules in Future modulepath & classpath Issues of Modular Software May Use Only modulepath in Future??
  55. 55. Moving to Module Issues & Solutions Project Jigsaw Java in the Box Yuichi Sakuraba

×