Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visual Code and jar files #501

Closed
danielcalvogonzalez opened this issue Apr 10, 2018 · 3 comments
Closed

Visual Code and jar files #501

danielcalvogonzalez opened this issue Apr 10, 2018 · 3 comments
Assignees

Comments

@danielcalvogonzalez
Copy link

Hello all,

I'm pretty sure this is a silly question but this is driving me crazy as I don't know what to do to work it out. I am not a Java expert although I have some programming skills. I'm trying to use Visual Code + Java Extension Pack as I thought it could be a good combination.

I am not using any project (eclipse, maven, etc) as I'm only creating short and straightforward programs. The problem I'm facing is when I try to use some classes from a jar file (for instance, algs4.jar from https://algs4.cs.princeton.edu/code/algs4.jar)

I create a simple java file in a directory that uses some of the classes with an import directive and I've added the directory where algs4.jar resides to my classpath (operating system) variable. From the command line, I can run successfully javac and java command, so everything looks fine (no errors, no warning, etc). I can compile and run my program. But when I try to run my java file from Visual Code, I receive the following error:

The import edu cannot be resolved
The import edu cannot be resolved
StdRandom cannot be resolved
StdRandom cannot be resolved
StdStats cannot be resolved
StdStats cannot be resolved

at PercolationStats.<init>(PercolationStats.java:7)
at PercolationStats.main(PercolationStats.java:105)

It clearly shows that Visual Code can't find the jar file but I don't know what to do to teach VC where that file resides.

Would kindly someone help me to work this problem out? By the way, where can I find more information about the usage of VC + Java?

Thank you very much.

Environment
  • Operating System: Windows 10
  • JDK version: 1.8.0_161
  • Visual Studio Code version: 1.22.1
  • Java extension version: Language Support for Java by Red Hat v0.22.0
Additional information

These are my import lines

import edu.princeton.cs.algs4.StdRandom;
import edu.princeton.cs.algs4.StdStats;


@fbricon
Copy link
Collaborator

fbricon commented May 23, 2018

The Eclipse backend providing java support in vscode-java doesn't have access to customized libraries added to the JDK.
We strongly encourage our users to depend on a build tool such as Maven or Gradle to define the project classpath, as we have no intentions of defining yet another kind of classpath declaration mechanism.

Here's a Maven-based project to help you started. The pom.xml, or project descriptor, is pretty self-explanatory (and a bit verbose). You'll find:

  • the project name, or Maven coordinates
  • configuration for the compiler to target Java 8
  • declaration of the algs4 dependency
  • location of the repository where algs4 can be downloaded from

algs4-demo-maven.zip

@fbricon fbricon added this to the Mid February 2019 milestone Feb 6, 2019
@fbricon fbricon self-assigned this Feb 6, 2019
@fbricon
Copy link
Collaborator

fbricon commented Feb 6, 2019

eclipse-jdtls/eclipse.jdt.ls#927 should help, by just adding jar files in a lib/ folder at the root of the directory.

See
auto-add-jar

@fbricon fbricon closed this as completed Feb 20, 2019
@JoeyRxy
Copy link

JoeyRxy commented Mar 9, 2019

Woo! Unbelievable!
@fbricon thanks!

@yaohaizh yaohaizh mentioned this issue Mar 14, 2019
leonardost added a commit to leonardost/kifu-recorder-tests that referenced this issue May 30, 2019
I searched in a lot of places for how to import .jar files in VSCode,
and did not have much success. It seems VSCode prefers to use a project
build file (Maven, Gradle) to read dependencies. However, there's a new
feature where you put .jar files in a lib folder and it works.

redhat-developer/vscode-java#384
microsoft/vscode-java-debug#194
redhat-developer/vscode-java#501
eclipse-jdtls/eclipse.jdt.ls#927
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants