Vscode-java: Visual Code and jar files

Created on 10 Apr 2018  路  3Comments  路  Source: redhat-developer/vscode-java

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;


enhancement

Most helpful comment

https://github.com/eclipse/eclipse.jdt.ls/pull/927 should help, by just adding jar files in a lib/ folder at the root of the directory.

See
auto-add-jar

All 3 comments

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

https://github.com/eclipse/eclipse.jdt.ls/pull/927 should help, by just adding jar files in a lib/ folder at the root of the directory.

See
auto-add-jar

Woo! Unbelievable!
@fbricon thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ronniehicks picture ronniehicks  路  3Comments

marlinla picture marlinla  路  3Comments

protoEvangelion picture protoEvangelion  路  3Comments

tveimo picture tveimo  路  4Comments

yaohaizh picture yaohaizh  路  4Comments