简体   繁体   English

从 JAR 文件导入 VSCode 无法解析 class

[英]VSCode Import from JAR file cannot resolve class

We have a java app that needs to use a public class form a JAR file.我们有一个 java 应用程序需要使用公共 class 形成 JAR 文件。 After much frustration with the main application, we have created a simple repo here to try to figure out what is going on.在对主应用程序感到非常沮丧之后,我们在这里创建了一个简单的 repo 来尝试弄清楚发生了什么。
The overly simple file that ends up in the JAR file is as follows:最终在 JAR 文件中的过于简单的文件如下:

package com.mystuff.helpers;

public class printStuff {
    public void showMsg(String msg) {
        System.out.println(msg);
    }
}

We create the JAR file with this command:我们使用以下命令创建 JAR 文件:

jar cvf MyJavaHelpers.jar com

The folder structure is as follows (the printStuff.java file is in the helpers folder):文件夹结构如下(printStuff.java文件在helpers文件夹中):

文件夹结构

A listing of the JAR contents is as follows: JAR 内容列表如下:

jar tf MyJavaHelpers.jar

META-INF/
META-INF/MANIFEST.MF
com/
com/mystuff/
com/mystuff/helpers/
com/mystuff/helpers/printStuff.java
com/mystuff/helpers/README.md 

Finally, the program that we have to use this simple class is as follows:最后,我们要使用这个简单的 class 的程序如下:

package com.mystuff.testapp;

import com.mystuff.helpers.*;

// To build the JAR file
// jar cvf MyJavaHelpers.jar com
// To display the contents of the JAR file
// jar tf MyJavaHelpers.jar



public class testDriver {
    public static void main(String[] args) {
        System.out.println("Starting testDriver");    
         com.mystuff.helpers.printStuff ps = new com.mystuff.helpers.printStuff();
        // testPrintStuff(ps);
        // testPrintStuffAgain(ps);
    }
   /*
    private static void testPrintStuffAgain(printStuff ps) {
        ps.showMsg("This is a fine kettle of clams");
    }

    private static void testPrintStuff(printStuff ps) {
        ps.showMsg("This is a fine kettle of fish");
    }
    */
}

In VS Code (v 1.55.0) We have a Java Project to contain our TestDriver that looks like this:在 VS Code (v 1.55.0) 中,我们有一个 Java 项目来包含我们的 TestDriver,如下所示: 在此处输入图像描述

Finally , the issue is that when we try to run the test driver, we get the message:最后,问题是当我们尝试运行测试驱动程序时,我们收到消息:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
        com.mystuff.helpers.printStuff cannot be resolved to a type
        com.mystuff.helpers.printStuff cannot be resolved to a type

        at com.mystuff.testapp.testDriver.main(testDriver.java:15)

We have tried the command Clean Java Language Server Workspace which seems to indicate that it works, but we cannot get past this error.我们已经尝试了命令Clean Java Language Server Workspace ,这似乎表明它可以工作,但我们无法克服这个错误。 Based on what we have looked at, the JAR file appears to be in the correct place (It is in the lib folder of the main app).根据我们所看到的,JAR 文件似乎位于正确的位置(它位于主应用程序的lib文件夹中)。 The import com.mystuff,helpers.;导入com.mystuff,helpers.; line does not show as an error, so it seems to us that it is found, however, the actual import of the printStuff class fails.该行未显示为错误,因此在我们看来,它已找到,但是printStuff class 的实际导入失败。 We have tried the fully qualified class name as well as relying on the import and only using the short name.我们已经尝试了完全限定的 class 名称以及依赖于导入并且仅使用短名称。 They both fail.他们都失败了。
We have seem some guidance about setting the classpath , but have not been able to find how to do that explicitly in VS Code.我们似乎有一些关于设置classpath的指导,但还没有找到如何在 VS Code 中明确地做到这一点。 Of course, if we do not have this little helper in a JAR file, but just as a side-by-side in the same project, it works just fine.当然,如果我们在 JAR 文件中没有这个小助手,但就像在同一个项目中并排一样,它也可以正常工作。 The issue that started us down this journey is trying to use a public class from a pre-packaged JAR file.让我们开始这一旅程的问题是尝试使用预打包的 JAR 文件中的公共class。

Any and all help is greatly appreciated.非常感谢任何和所有帮助。 Thanks in advance.提前致谢。

Before adding the jar to library, you may run the command java -jar printStuff.jar to test if it could be executed successfully.在将 jar 添加到库之前,您可以运行命令java -jar printStuff.jar测试是否可以成功执行。

The error occurs because the class must be called with its fully qualified name.因为 class 必须使用其完全限定名称来调用,所以会发生错误。 To be clear, the name of this class is not printStuff , It's com.mystuff.helpers.printStuff , so the right command should be:需要明确的是,这个 class 的名称不是printStuff ,而是com.mystuff.helpers.printStuff ,所以正确的命令应该是:

  1. Turn to the folder;转到文件夹;

  2. Compile.java file: javac com\mystuff\helpers\printStuff.java编译.java 文件: javac com\mystuff\helpers\printStuff.java

  3. Generate.jar: jar cvfe printStuff.jar com.mystuff.helpers.printStuff.\生成.jar: jar cvfe printStuff.jar com.mystuff.helpers.printStuff.\

在此处输入图像描述

Then readd it to referenced libraries and see if the error goes away.然后将其读取到引用的库中,看看错误是否消失。

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

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