module java.base does not “opens java.lang“ to unnamed module

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Python is much like Java and at times even looks simpler. But Python is just as powerful as Java. If Java is the heavy metal of computer programming, then Python is the jazz that opens doors of freedom in software development. Both Java and Python are object-oriented programming languages. Both support Java’s famous features such as encapsulation, inheritance and polymorphism. Both can be used to develop desktop and web-based applications. Both are multi-platform and run on all major platforms such as Linux, MS Windows, and Mac OS. Both support graphical user interface development. Of course, there are also differences between Java and Python. For example, Java programs must be compiled, but in Python you have a choice of compiling your programs into stand-alone applications or running them as interpreted scripts or programs launched by a command from the Command Prompt. There are many other similarities and differences between these two languages, and those similarities make it a lot easier than you might think to learn Python, if you already know Java. While learning Python myself, I realized how fast and easy it was to understand and pick up Python’s syntax when I started converting Java’s programming problems into Python. I had already known Java and worked with it professionally for some time, but I found myself having to learn Python fast to advance in my career. It motivated me to find a way to harness my existing knowing to speed up the process of learning a new language. This book is essentially a systematic presentation of the learning process I documented in learning Python using knowledge of Java. For the engineer who is already proficient in Java, it would be a waste of time to study a Python textbook that begins with the basic concept of object-oriented programming, since the concept of OOP software development is identical in all languages. The differences from one language to another are in their syntax. Syntax is best learned by using examples of the programming language that the engineer already knows. That’s exactly is the learning model of this book. This book is for those who are already comfortable with developing using Java programming language and therefore assumes knowledge of Java. Designed for Java engineers who want to learn Python, this book walks you through the differences between Java 8 and Python 2.7 syntax using examples from both languages. Specifically, the book will demonstrate how to perform the same procedures in Java and Python. For each procedure, the class names, method names, and variable names are kept consistent between Java and Python examples. This way you can see clearly the differences in syntax between the two languages. Using this approach, you will be up to speed with Python in no time. Table of Contents Chapter 1. A First Simple Program Chapter 2. Compiling A Program Chapter 3. Variables Chapter 4. Control Statements And Loops Chapter 5. Operators Chapter 6. Classes Chapter 7. Data Structures Chapter 8. Multithreading And Multiprocessing Chapter 9. I/O Chapter 10. Strings Chapter 11. Sorting And Searching Algorithms Chapter 12. Python’S Modules, Java’S Libraries Chapter 13. Running Shell Commands From Python Chapter 14. Querying Databases Chapter 15. Bulding Stand-Alone Applications With Python Chapter 16. Building Websites With Python
### 回答1: 这个错误是因为Java 9及以上版本引入了模块化系统,而在模块化系统中,Java.lang包默认是对所有模块开放的,但是在这个错误中,Java.base模块没有将Java.lang包开放给未命名模块。这可能是因为您的代码中使用了Java 9及以上版本的特性,但是您的代码没有被编译为模块化的形式。要解决这个问题,您可以将您的代码编译为模块化的形式,或者在启动Java虚拟机时使用--add-opens选项将Java.lang包开放给未命名模块。 ### 回答2: 在Java 9中,引入了一个新的特性“模块化系统”,旨在解决Java应用程序之间依赖冲突的问题。模块是代码和资源的封装单元,可以明确定义导出和开放的包,以此来限制对其他模块的访问。 但是,在使用模块化系统时,我们可能会遇到以下错误信息:“module java.base does not 'opens java.lang' to unnamed module”,即Java基本模块不向未命名模块开放java.lang包。这个错误会发生在某些情况下,例如当我们使用反射机制或某些第三方库时,这些操作需要访问受限的包或类时。 问题的原因是Java基本模块(java.base)默认情况下仅向它使用的其他模块开放java.lang包,但未命名模块却无法访问它。未命名模块是指没有显式声明模块名称的类路径上的类集合。 解决这个问题的方法是显式地将java.lang包开放给我们使用的未命名模块。这可以在模块描述符(module-info.java)中添加以下声明来实现: open module mymodule { // ... opens java.lang; } 这里的mymodule是我们自己的模块名称,我们可以更改它以适应我们的应用程序模块描述符。 添加声明后,我们就可以使用反射机制或其他需要访问java.lang包的操作而不会遇到以上错误信息。 总的来说,在使用Java 9及更高版本的Java时遇到“module java.base does not 'opens java.lang' to unnamed module”问题是常见的,这是由于模块化系统的限制导致的。在解决此问题时,我们应该加强对模块描述符的理解,以便有针对性地进行干预。 ### 回答3: 这个错误信息的意思是java.base模块没有为未命名模块打开java.lang模块。在Java 9中,引入了模块系统,以避免类之间的冲突和不必要的类路径干扰。模块化应用程序将更易于管理和维护,模块之间的依赖性可以更清楚地表示。但是,这也带来了新的挑战。在模块化环境中,所有的Java代码都必须被分配到一个模块中,并且每个模块都必须声明其依赖项。 在这里,错误消息告诉我们java.base模块不会“打开”(也就是允许一个模块访问另一个模块的内部)java.lang模块。在Java 9中,java.lang模块被认为是可观察模块,并且它是自动打开的。但是,如果你在代码中使用反射或Hessian等技术访问java.lang包(例如通过分解字符串名称来实现),那么你需要显式地打开它。 为了解决这个问题,可以使用以下命令来打开java.lang模块: ``` --add-opens java.base/java.lang=ALL-UNNAMED ``` 这个命令不是在代码中使用的,而是在Java命令行中使用的。它告诉Java虚拟机打开java.base模块中的java.lang模块,并允许所有未命名模块访问它。如果你使用的是build tools,如Maven和Gradle,你可以在构建配置文件中添加这个命令。 总之,这个错误消息的原因是因为java.lang模块没有被打开到未命名模块,可以通过使用上述命令来解决。在实践中,了解模块化系统如何工作以及如何处理这些错误消息是非常重要的,以确保你的应用程序在Java 9及以上版本中能够正常运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值