简体   繁体   English

在 Visual Studio Code 中导入 .jar,类路径

[英]Import .jar in Visual Studio Code, classpath

I am using Visual Studio Code to Write a Java Project.我正在使用 Visual Studio Code 编写 Java 项目。 For that i want to use the MigLayout.jar in my Project.为此,我想在我的项目中使用 MigLayout.jar。

I created a new Project so i would get a .classpath我创建了一个新项目,所以我会得到一个 .classpath

I tried to follow the instructions here: https://stackoverflow.com/a/54535301/11654683我试着按照这里的说明操作: https : //stackoverflow.com/a/54535301/11654683

Here you can see my Classpath File在这里你可以看到我的类路径文件

<classpath>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-12"/>
    <classpathentry kind="src" path="src/"/>
    <classpathentry kind="output" path="bin"/>
    <classpathentry exported="true" kind="lib" path="lib/miglayout.jar">
</classpath>

When I try to edit my class App.java it says: App.java isnt on the classpath Only Syntax errors will be reported.当我尝试编辑我的类 App.java 时,它说: App.java 不在类路径上,只会报告语法错误。

This is the path of my java file: 11 - Test Project\\Test Project\\src\\app\\app.java这是我的 java 文件的路径: 11 - Test Project\\Test Project\\src\\app\\app.java

I do want to continue using VS Code and not change to a different Editor.我确实想继续使用 VS Code,而不是更改为其他编辑器。 How can I finally use my MigLayout.jar without getting these errors?我怎样才能最终使用我的 MigLayout.jar 而不会出现这些错误?

EDIT:编辑:

I created a lib folder next to an independent file and inserted the jar file I wanted to import.我在一个独立文件旁边创建了一个 lib 文件夹,并插入了我想导入的 jar 文件。 VS Code does now understand MigLayout as a type when I import当我导入时,VS Code 现在确实将 MigLayout 理解为一种类型

import net.miginfocom.layout.Grid;导入 net.miginfocom.layout.Grid; import net.miginfocom.swing.MigLayout;导入 net.miginfocom.swing.MigLayout;

But when compiling it says: .\\Win.java:5: error: package net.miginfocom.layout does not exist import net.miginfocom.layout.Grid;但是编译时说:.\\Win.java:5: error: package net.miginfocom.layout does not exist import net.miginfocom.layout.Grid; ^ .\\Win.java:6: error: package net.miginfocom.swing does not exist import net.miginfocom.swing.MigLayout; ^ .\\Win.java:6: 错误:包 net.miginfocom.swing 不存在 import net.miginfocom.swing.MigLayout;

As you can see i have referenced it in the browser: Structure如您所见,我在浏览器中引用了它:结构

1.make sure you hava create a folder named lib inside your project and add your .jar file into it 1.确保您在项目中创建了一个名为lib的文件夹并将您的.jar文件添加到其中

2.Configure paths in the .classpath like: 2.在.classpath配置路径,如:

<classpathentry kind="lib" path="lib/miglayout.jar"/>

3.if it still thow error,Clean the workspace directory:F1 - input Clean -clean workspace 3.如果还是报错,清理工作区目录:F1 - 输入Clean -clean workspace

you could look at the doucument add jar你可以看看文件添加罐

I set up a gradle project and added the dependency via the link from https://mvnrepository.com/我设置了一个 gradle 项目并通过https://mvnrepository.com/的链接添加了依赖项

I copied the given commands on the MigLayout Site behind the "Gradle" tab and put them into the dependencies{} in the build.grade file我复制了 MigLayout 站点上“Gradle”选项卡后面的给定命令,并将它们放入 build.grade 文件中的依赖项{}

With ./gradlew run from the command line out of the project folder it compiled successfully.使用./gradlew从它成功编译的项目文件夹中的命令行运行 Thanks for the help anyways.无论如何感谢您的帮助。

PS: I still haven't figured out how to do it without gradle. PS:我还没有想出没有gradle怎么办。 Even compiling from the command line with: javac -cp "path to jar" myFile i did not get the dependencies right.即使从命令行编译: javac -cp "path to jar" myFile我也没有得到正确的依赖项。 Let me know if you have experience with that.如果您有这方面的经验,请告诉我。

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

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