`org.json`无法解析为模块?

huangapple go评论72阅读模式
英文:

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文件中导入它?

截图:

`org.json`无法解析为模块?


<details>
<summary>英文:</summary>

I&#39;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:

{
    &quot;files.exclude&quot;: {
        &quot;**/.classpath&quot;: true,
        &quot;**/.project&quot;: true,
        &quot;**/.settings&quot;: true,
        &quot;**/.factorypath&quot;: true
    },
    &quot;java.format.settings.url&quot;: &quot;Format.xml&quot;,
    &quot;java.format.settings.profile&quot;: &quot;style&quot;,
    &quot;java.project.referencedLibraries&quot;: [
        &quot;lib/**/*.jar&quot; // 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:

`org.json`无法解析为模块?

答案1

得分: 1

TL;DR — 如'在具有module-info.java的项目中无法解决'错误 这个未解决的问题报告所述,vscode 在处理JPMS和module-info.java方面存在问题。


冗长的版本

根据我自己的经验,我可以亲自证明上面链接的vscode问题报告的记者所说...

>> „...我尝试过Gradle和Maven...
>>
>> ...
>>
>> „...我发现Gradle和Maven会自动刷新classpath文件并且删除我的修改,这会重新带回错误...
>>
>> ...
>>
>> „...在classpath文件中需要设置模块路径信息,以便Eclipse感到满意,但是从Gradle或Maven做这件事没有好的方式...

证明这是一个vscode的问题是,完全相同的项目 — 除了删除您的评论外没有更改 — 在IntelliJ中编译得非常好...

`org.json`无法解析为模块?

由于您的项目既不使用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并成功编译...

`org.json`无法解析为模块?

我已经成功地通过帮助他们应用在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…

`org.json`无法解析为模块?

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…

&lt;dependencies&gt;
	&lt;dependency&gt;
	   &lt;groupId&gt;org.json&lt;/groupId&gt;
		&lt;artifactId&gt;json&lt;/artifactId&gt;
		&lt;version&gt;20200518&lt;/version&gt;
	&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;build&gt;
	&lt;plugins&gt;
		&lt;plugin&gt;
			&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
			&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
			&lt;version&gt;3.8.1&lt;/version&gt;
			&lt;configuration&gt;
				&lt;source&gt;11&lt;/source&gt;
				&lt;target&gt;11&lt;/target&gt;
				&lt;compilerArgs&gt;
					&lt;arg&gt;-Xlint:unchecked&lt;/arg&gt;
					&lt;arg&gt;--add-modules&lt;/arg&gt;
					&lt;arg&gt;org.json&lt;/arg&gt;
				&lt;/compilerArgs&gt;
			&lt;/configuration&gt;
		&lt;/plugin&gt;
	&lt;/plugins&gt;
&lt;/build&gt;

…Maven does its magic and processes the module-info.java successfully…

`org.json`无法解析为模块?

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.

huangapple
  • 本文由 发表于 2020年10月11日 20:07:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64303821.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定