Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

visual studio code - vscode+java+windows: I want to import and use org.apache.logging.log4j.Logger without Maven

Using only vscode with java extensions added, and native Java 8 without any additional libraries or extensions, I'd like to use log4j.

I have downloaded the various .jar files and they are in my classpath (although classpath is difficult to verify in vscode, would be nice to have an vscode IDE way to verify the actual path your code sees). The files do show up in vscode under the "JAVA DEPENDENCIES" accordion menu on the left EXPLORER.

The 3 primary .jar files are:

  • log4j-api-2.11.0.jar
  • log4j-core-2.11.0.jar
  • log4j-1.2-api-2.11.0.jar

I have a launch.json file with '"windows": {"classPaths":...' defined and a .classpath file added for good measure with the kind="lib" and the fully qualified literal path given to the 3 primary .jar files.

The files are actually in my Windows CLASSPATH variable and shows up when from within this .java code I execute this line of code:

System.out.println(System.getProperty("java.class.path"));

This shows the literal path to the location of three jar files, as well as an entry for ".", and for the folder that they are in as well (likely overkill).

The error I receive from vscode editor is still

The import org.apache cannot be resolved

in my code's .java file, I have the import, and a red squiggly line is under org.apache

import org.apache.logging.log4j.Logger

Any suggestions of a quick fix that doesn't involve adding Maven?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

2.Configure paths in the .classpath like:

<classpathentry kind="lib" path="lib/log4j-1.2-api-2.11.2.jar"/>
<classpathentry kind="lib" path="lib/log4j-api-2.11.2.jar"/>
<classpathentry kind="lib" path="lib/log4j-core-2.11.2.jar"/>

3.if it still thow error,Clean the workspace directory:

the VS Code user workspace storage area can be found under these locations :

  • Windows : %APPDATA%Code[ - Variant]UserworkspaceStorage
  • MacOS : $HOME/Library/Application Support/Code[ - Variant]/User/workspaceStorage/
  • Linux : $HOME/.config/Code[ - Variant]/User/workspaceStorage/

could refer to add jar


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...