Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从JAR文件导入的VSCode无法解析类_Java_Class_Visual Studio Code - Fatal编程技术网

Java 从JAR文件导入的VSCode无法解析类

Java 从JAR文件导入的VSCode无法解析类,java,class,visual-studio-code,Java,Class,Visual Studio Code,我们有一个java应用程序,它需要使用一个公共类来形成一个JAR文件。在对主应用程序感到非常失望之后,我们在这里创建了一个简单的回购协议,试图弄清楚发生了什么。 在JAR文件中结束的过于简单的文件如下所示: package com.mystuff.helpers; public class printStuff { public void showMsg(String msg) { System.out.println(msg); } } jar tf MyJ

我们有一个java应用程序,它需要使用一个公共类来形成一个JAR文件。在对主应用程序感到非常失望之后,我们在这里创建了一个简单的回购协议,试图弄清楚发生了什么。
在JAR文件中结束的过于简单的文件如下所示:

package com.mystuff.helpers;

public class printStuff {
    public void showMsg(String msg) {
        System.out.println(msg);
    }
}
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");
    }
    */
}
我们使用以下命令创建JAR文件:

jar cvf MyJavaHelpers.jar com
文件夹结构如下(printStuff.java文件位于helpers文件夹中):

JAR内容列表如下所示:

package com.mystuff.helpers;

public class printStuff {
    public void showMsg(String msg) {
        System.out.println(msg);
    }
}
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");
    }
    */
}
最后,我们必须使用这个简单类的程序如下:

package com.mystuff.helpers;

public class printStuff {
    public void showMsg(String msg) {
        System.out.println(msg);
    }
}
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代码(V1.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)
我们尝试了命令cleanjava语言服务器工作区,这似乎表明它可以工作,但我们无法克服这个错误。 根据我们所看到的,JAR文件似乎位于正确的位置(它位于主应用程序的lib文件夹中)。 导入com.mystuff,助手。;行并没有显示为错误,所以在我们看来它似乎被找到了,但是,printStuff类的实际导入失败了。我们尝试了完全限定的类名,并且依赖于导入,只使用了短名称。他们都失败了。
我们似乎有一些关于设置类路径的指导,但还没有找到如何在VS代码中显式地实现这一点。 当然,如果我们在JAR文件中没有这个小助手,但就像在同一个项目中并排使用一样,它工作得很好。让我们开始这段旅程的问题是试图使用预打包JAR文件中的公共类


非常感谢您的任何帮助。提前感谢。

在将jar添加到库之前,您可以运行命令
java-jar printStuff.jar
来测试它是否可以成功执行

发生此错误是因为必须使用其完全限定名调用该类。要清楚,此类的名称不是
printStuff
,而是
com.mystuff.helpers.printStuff
,因此正确的命令应该是:

  • 打开文件夹

  • Compile.java文件:
    javac com\mystuff\helpers\printStuff.java

  • Generate.jar:
    jar cvfe printStuff.jar com.mystuff.helpers.printStuff.\


  • 然后将其读到引用库中,看看错误是否消失。

    好的,感谢您向正确的方向推动!我缺少的是我没有发布编译步骤(您的步骤#2)。我引用的是整个类名,我错误地认为VSCode正在编译我的库(显然不是)。明确地说,我尝试添加您的“main”调用并测试它(这很有效),但是我们想要使用的类是一个没有主入口点的库。main()入口点成功后,我将其删除并重试。它工作得很好。因此,我最终只需要缺少“javac”步骤。太棒了!很高兴知道您的问题已经解决:)