简体   繁体   English

为什么 VSCode 一直打开 *.class 文件而不是 .java 文件?

[英]Why does VSCode keep opening *.class files as opposed to .java files?

(I couldn't figure out how to upload my screen capture to stackoverflow. So this is a streamable link: https://streamable.com/0im8tx ) (我不知道如何将我的屏幕截图上传到 stackoverflow。所以这是一个流媒体链接: https://streamable.com/0im8tx

In this video, VSCode opens QueriesController.class as opposed to QueriesController.java when I cmd click into QueriesController .在此视频中,当我 cmd 单击进入 QueriesController 时,VSCode 打开 QueriesController.class 而不是QueriesController

I have compiled provided the definitions of the jar file in my workspace:我在我的工作区中编译提供了 jar 文件的定义:

    "settings": {
        "java.project.referencedLibraries": {
            "include": [
                "<path-to-jar-that-contains-QueriesController.jar>",
.... 
            "sources": {
                "<path-to-jar-that-contains-QueriesController.jar>": "/my/local/java/definition/src/folder",

Does anyone know why VSCode is choosing to open the definition as a.class file rather than a.java file?有谁知道为什么 VSCode 选择将定义打开为 a.class 文件而不是 a.java 文件?

I use commands to generate a simple jar package and use it in another project.我使用命令生成了一个简单的jar package 并在另一个项目中使用。 It's true that when we click the class name, .class file is opened instead of .java file:确实,当我们单击 class 名称时,打开的是.class文件而不是.java文件:

在此处输入图像描述

About how to generate a executable jar package, you can have a look at this reply :关于如何生成可执行文件jar package,你可以看看这个回复

  1. Compile.java file and generate.class;编译.java文件,生成.class;

  2. Generate manifest and pack them into jar生成manifest打包成jar

In general, a JAR (Java ARchive) is a package file format typically used to aggregate many Java class files and associated metadata and resources (text, images, etc.) into one file for distribution.一般来说,JAR (Java ARchive) 是一种 package 文件格式,通常用于将许多 Java class files和关联的metadataresources (文本、图像等)聚合到一个文件中以供分发。

.java file isn't included in jar packages, and that's why you get.class file opened instead of.java file. .java 文件不包含在 jar 包中,这就是为什么您打开 .class 文件而不是 .java 文件的原因。

I am not familiar with VSCode but your problem is common across most IDEs.我不熟悉 VSCode,但你的问题在大多数 IDE 中都很常见。

Usually when a jar is made, it consists of compiled class files rather than original source codes.通常在制作 jar 时,它由编译后的 class 文件组成,而不是原始源代码。 The reason for this is to run code as efficient and fast as possible and usually people don't want source code in jar because when running they also have to be recompiled again which is a waste of time.这样做的原因是尽可能高效和快速地运行代码,通常人们不想要 jar 中的源代码,因为在运行时它们还必须再次重新编译,这是浪费时间。

在此处输入图像描述

Take a look at this picture.看看这张照片。 I have just downloaded a jar file from mavenrepository and it downloads the compiled version of jar. The extension is .class我刚刚从 mavenrepository 下载了一个 jar 文件,它下载了 jar 的编译版本。扩展名是.class

What the IDE does is it tries to decompile the code with a decompiler (In this case as you can see FernFlower decompiler). IDE 所做的是尝试使用反编译器反编译代码(在这种情况下,您可以看到 FernFlower 反编译器)。

However it lacks formatting and in-code documentation the source code (.java) has.但是,它缺少源代码 (.java) 所具有的格式和代码内文档。 Which is why most IDEs offer to download sources.这就是为什么大多数 IDE 提供下载源代码的原因。 Intellij shows this right on top. Intellij 在顶部显示了这一点。 Other IDEs may have this setting buried in deep.其他 IDE 可能会将此设置隐藏得很深。 (You may have to check for yourself) (你可能需要自己检查)

When you download sources, IDE try to contact the server and download original source code.下载源码时,IDE尝试联系服务器下载原始源码。 Probably that would look something like this:可能看起来像这样:

在此处输入图像描述

If you look closely you can see name has changed to .java which represents the source code.如果仔细观察,您会发现名称已更改为代表源代码的.java

VS Code has option under Java Settings, Java Download sources and Maven download sources . VS Code 在 Java Settings、 Java Download sources 和 Maven download sources下有选项。

It is not enabled by default.默认情况下不启用。 Upon enabling it, VS Code shows the proper source file, although the name appears to be .Class files.(Upon Ctrl + Clicking the symbol, with method implementations, comments, etc.,JavaDoc Comments)启用它后,VS Code 会显示正确的源文件,尽管名称看起来是.Class文件。(按 Ctrl + 单击符号,方法实现、注释等,JavaDoc 注释)

If proper sources are not found in m2 repository, it shows the decompiled class file with stubbed methods.如果在 m2 存储库中找不到合适的源,它会显示带有存根方法的反编译 class 文件。 A comment similar to this is shown at the beginning of the file.与此类似的注释显示在文件的开头。

// Failed to get sources. Instead, stub sources have been generated by the disassembler.
 // Implementation of methods is unavailable.

In Either of the cases, VS Code shows the maven library files as .Class files in read-only mode.在任何一种情况下,VS Code 都以只读模式将 maven 库文件显示为.Class文件。 Also, source files are not displayed on the Java Project Explorer.(Although even if it exists in the local.m2 repos).此外,源文件不会显示在 Java Project Explorer 中。(尽管它存在于 local.m2 存储库中)。

Hope that helps!希望有帮助! Happy Coding!快乐编码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM