【发布时间】:2021-07-02 16:41:13
【问题描述】:
我们有一个需要使用 JAR 文件的公共类的 java 应用程序。在对主应用程序感到非常沮丧之后,我们在这里创建了一个简单的 repo 来尝试弄清楚发生了什么。
以 JAR 文件结尾的过于简单的文件如下:
package com.mystuff.helpers;
public class printStuff {
public void showMsg(String msg) {
System.out.println(msg);
}
}
我们使用以下命令创建 JAR 文件:
jar cvf MyJavaHelpers.jar com
文件夹结构如下(printStuff.java文件在helpers文件夹下):
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
最后,我们要使用这个简单类的程序如下:
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");
}
*/
}
在 VS Code (v 1.55.0) 中,我们有一个 Java 项目来包含我们的 TestDriver,如下所示:
最后,问题是当我们尝试运行测试驱动程序时,我们收到消息:
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)
我们已经尝试了命令Clean Java Language Server Workspace,这似乎表明它可以工作,但我们无法克服这个错误。
根据我们所看到的,JAR 文件似乎位于正确的位置(它位于主应用程序的 lib 文件夹中)。
import com.mystuff,helpers.; 行没有显示为错误,所以在我们看来,它是找到的,但是,printStuff 类的实际导入失败。我们已经尝试了完全限定的类名以及依赖于导入并且仅使用短名称。他们都失败了。
我们似乎有一些关于设置 classpath 的指导,但还没有找到如何在 VS Code 中明确地做到这一点。
当然,如果我们在 JAR 文件中没有这个小助手,但就像在同一个项目中并排一样,它就可以正常工作。让我们开始这一旅程的问题是尝试使用预打包 JAR 文件中的 public 类。
非常感谢任何和所有帮助。提前致谢。
【问题讨论】:
标签: java class visual-studio-code