4

I am using Visual Studio Code to Write a Java Project. For that i want to use the MigLayout.jar in my Project.

I created a new Project so i would get a .classpath

I tried to follow the instructions here: 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.

This is the path of my java file: 11 - Test Project\Test Project\src\app\app.java

I do want to continue using VS Code and not change to a different Editor. How can I finally use my MigLayout.jar without getting these errors?

EDIT:

I created a lib folder next to an independent file and inserted the jar file I wanted to import. VS Code does now understand MigLayout as a type when I import

import net.miginfocom.layout.Grid; import 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:6: error: package net.miginfocom.swing does not exist import net.miginfocom.swing.MigLayout;

As you can see i have referenced it in the browser: Structure

Stefan Kablowski
  • 41
  • 1
  • 1
  • 5

3 Answers3

2

1.make sure you hava create a folder named lib inside your project and add your .jar file into it

2.Configure paths in the .classpath like:

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

3.if it still thow error,Clean the workspace directory:F1 - input Clean -clean workspace

you could look at the doucument add jar

Leo Zhu
  • 15,726
  • 1
  • 7
  • 23
0

I set up a gradle project and added the dependency via the link from 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

With ./gradlew run from the command line out of the project folder it compiled successfully. Thanks for the help anyways.

PS: I still haven't figured out how to do it without gradle. Even compiling from the command line with: javac -cp "path to jar" myFile i did not get the dependencies right. Let me know if you have experience with that.

Stefan Kablowski
  • 41
  • 1
  • 1
  • 5
0

If you are not using any dependency management tool like maven or gradle, then you can use vscode Classpath Configuration or .vscode/settings.json to add jar files.

Classpath Configuration:

  1. In vscode open Classpath Configuration and Scroll down to Referenced Libraries.
  2. Klick on "Add" and choose your jar file.

.vscode/settings.json:

{
    "java.project.referencedLibraries": [
        "lib/**/*.jar",
        "joda-time-2.12.5.jar"
    ]
}

In my example I added joda-time-2.12.5.jar to my classpath. You also could create a lib directory and move your jar file to lib directory, which is included by default in classpath in vscode.

mkdir lib
mv *.jar lib

Now vscode should be able to recognize imports from joda times and you should be able to run and debug from vscode.

If you want to compile and run your Java file from command line, then follow this command line. Make sure your path is pointing to the jar file you want to include.

javac -cp ".;joda-time-2.12.5.jar" YourJavaFile.java
java -cp  ".;joda-time-2.12.5.jar" YourJavaFile

This command line style is used on Windows. On Unix/Linux use ":" instead of ";"

javac -cp ".:joda-time-2.12.5.jar" YourJavaFile.java
java -cp  ".:joda-time-2.12.5.jar" YourJavaFile