英文:
org.json cannot be resolved to a module?
问题
我正在学习Java。为了在我的应用程序中读取JSON,我下载了[这个JSON库](https://github.com/stleary/JSON-java),这是一个自动模块。
我在我的模块描述符中包含了该库,如下所示:
```java
module art
{
exports art.anixt;
exports art.coartl;
exports art.runeape;
requires org.json; // org.json无法解析为模块Java(8389908)
}
我在VSCode中的settings.json配置如下:
{
"files.exclude": {
"**/.classpath": true,
"**/.project": true,
"**/.settings": true,
"**/.factorypath": true
},
"java.format.settings.url": "Format.xml",
"java.format.settings.profile": "style",
"java.project.referencedLibraries": [
"lib/**/*.jar" // 在引用库中显示的jar文件(见截图)
]
}
如何将这个jar文件包含到我的模块中,并在我的Java文件中导入它?
截图:
<details>
<summary>英文:</summary>
I'm learning Java. To read JSON in my application, I downloaded [this JSON library](https://github.com/stleary/JSON-java); which is an automatic module.
I included that library in my module descriptor like:
```java
module art
{
exports art.anixt;
exports art.coartl;
exports art.runeape;
requires org.json; // org.json cannot be resolved to a moduleJava(8389908)
}
My settings.json in vscode:
{
"files.exclude": {
"**/.classpath": true,
"**/.project": true,
"**/.settings": true,
"**/.factorypath": true
},
"java.format.settings.url": "Format.xml",
"java.format.settings.profile": "style",
"java.project.referencedLibraries": [
"lib/**/*.jar" // jar file showing in Referenced library(see screenshot)
]
}
How do I include the jar file in my module and import it into my Java file?
Screenshot:
答案1
得分: 1
TL;DR — 如'在具有module-info.java的项目中无法解决'错误 这个未解决的问题报告所述,vscode 在处理JPMS和module-info.java方面存在问题。
冗长的版本
根据我自己的经验,我可以亲自证明上面链接的vscode问题报告的记者所说...
>> „...我尝试过Gradle和Maven...“
>>
>> ...
>>
>> „...我发现Gradle和Maven会自动刷新classpath文件并且删除我的修改,这会重新带回错误...“
>>
>> ...
>>
>> „...在classpath文件中需要设置模块路径信息,以便Eclipse感到满意,但是从Gradle或Maven做这件事没有好的方式...“
证明这是一个vscode的问题是,完全相同的项目 — 除了删除您的评论外没有更改 — 在IntelliJ中编译得非常好...
由于您的项目既不使用Maven也不使用Gradle — 而是选择在lib
文件夹中使用基于文件的依赖管理 — 所以您的情况更糟,因为您已经取消了应用任何能够解决此问题的JPMS启用插件的选项。
例如,通过将下面的pom.xml添加到我对您的项目的实验版本中,并为*maven-compiler-plugin
*适当配置...
…
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20200518</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
<compilerArgs>
<arg>-Xlint:unchecked</arg>
<arg>--add-modules</arg>
<arg>org.json</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
…
...Maven会奇迹般地处理module-info.java并成功编译...
我已经成功地通过帮助他们应用在vscode错误报告中提到的mrJar插件 来解决了其他程序员的JPMS问题。因此,如果您愿意使用Gradle而不是Maven,我同样可以指导您如何配置该插件。
英文:
TL;DR — As this unresolved 'Cannot be resolved' errors in projects with module-info.java issue reports, vscode is brain dead when it comes to JPMS and module-info.java.
The long-winded version
From my own experience, I can personally vouch for what the reporter of the above-linked vscode issue reports…
>> „…I've tried both Gradle and Maven…“
>>
>> …
>>
>> „…I find that Gradle and Maven will automatically refresh the classpath file and remove my modifications to it, which will bring back the errors…“
>>
>> …
>>
>> „…there needs to be module path information set in the classpath file in order for Eclipse to be happy, but there is no good way to do with that from Gradle or Maven…“
Proof that it's a vscode issue is that the exact same project — unchanged except for the removal of your comment — compiles perfectly fine in IntelliJ…
Since your project uses neither Maven nor Gradle — opting instead to use file-based dependency mgt with the jar in the lib
folder — you're in even worse shape because you've eliminated the option of applying any JPMS-enabling plugins that could resolve the issue.
For example, by adding the following pom.xml with the appropriate configuration for the maven-compiler-plugin
to my experimental version of your project…
…
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20200518</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
<compilerArgs>
<arg>-Xlint:unchecked</arg>
<arg>--add-modules</arg>
<arg>org.json</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
…
…Maven does its magic and processes the module-info.java successfully…
I've successfully resolved other Stackers' JPMS woes by helping them apply that mrJar plugin mentioned in that vscode bug report. So if you're open to using Gradle instead of Maven, I could likewise advise you on how to configure that plugin too.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论