VS Code - The import "#####" cannot be resolved - java

So, i am running a java project which have many library that are available in the current working directory but VS code seems to not recognize these library and giving out error "The import ###### cannot be resolved" ex: The import org.apache.pdfbox.pdmodel.PDDocument cannot be resolved"
here is the image that might help you to know more about it
This is the package that i am working on :
Here the org/apache is the library contain the class file that are need to be imported and FileArrangement.java is the file having the import statements
Error i have been receiving
this is what VS code is been showing
i really need your help because i really don't have any idea how to correct this
I have checked other projects and they are also showing the same result although the import statements for java classes like . java.util.ArrayList doesn't show any kind of error and i have tried to clean java in VS code it also didn't work
i just need to correct this error of VS code to import the classes that i need
No error on java.util package

Putting the libraries in your current working directory does not work for Java, you need to add them to the classpath.
If you're using maven, that manages the classpath for you.
If not, you can manage it in VS Code by executing the Java: Configure Classpath command from the Command Palette (Ctrl+Shift+P).

You can add dependencies via Referenced libraries under the JAVA PROJECTS panel.
Or use java.project.referencedLibraries setting in settings.json.
For example:
"java.project.referencedLibraries": [
"library/**/*.jar",
"/home/username/lib/foo.jar"
]
Details can be found in configure-classpath and manage-dependencies.

Related

How to import packages from the external Library in Java?

I'm a n00b coder. I found an interesting library and trying to start toying with it. Which is not going great. This library is from 99' and uses JUnit (which I'm unfamiliar with) so there is a lot of confusing stuff. But it seems like the source of my failing even more elementary. Namely I have troubles importing packages.
This library has a test called StandardEvalTest.java. I moved to it to main Java directory and now I'm trying and failing to launch it using JUnit.
This package path org.pokersource.game.Deck goes directly from the directory where the test StandardEvalTest.java sits.
I also added the main java directory to the PATH environmental variable. Which as I assumed will allow import to locate the package.
None of those two things help. Also I was suspecting that maybe Deck.java and Deck.class are not enough and I have to do some work to create a package from it. But as far as I can say from Oracle doc the only thing needed is a package name in the header. Which seems to be present.
So I'm out of moves. Please help!
PS: Some additional info inspired by #Dhrubo 's answer:
The test I'm trying to run indeed sits in the main java folder of the library. (I moved it here hoping that when running from here it would be easier to find the package)
If I'm trying to compile the test instead of running it with JUnit he seem to fail to find JUnit classes and other JUnit related stuff.
[Oh OK I'm an idiot! Dont't mind me]
You should include the package while running StandardEvalTest.java as below
javac -cp [classpath] org.pokersource.game.StandardEvalTest.java
and run it from package root directory, I am assuming it is custom java file that you want to compile. You run directory should be parent of your package directory.
** I also see, you are trying to compile StandardEvalTest.java instead of Deck.java ... then check your StandardEvalTest.java file whether it exists in desired location.

Jpype import cannot find module in jar

I have received the task, at work, to find a way to use some methods from an existent jar file in a Python project. I have very limited experience in Python, but I have worked with that specific jar file before (it is part of a project we are working on). I am not allowed to modify much of both projects, and they are required to be as independent as possible.
I have researched multiple ways to include the jar methods in Python. So far, I have tried Jython (which I cannot use because the Python project uses PyQt among other libraries, which force the use of CPython, if my understanding is correct), Pyjnius and JPype. JPype seems the most promising, but I cannot get it working either. I have pasted the my code below, slightly censored because I don't know how much I am allowed to share.
from jpype import *
import jpype.imports
try:
jpype.addClassPath("jars/sdk.jar") #the relative path to the jar file
jpype.startJVM(convertStrings=False)
java.lang.System.out.println(jpype.getClassPath()) #printing the classpath to check, the path is correctly pointing to the sdk.jar file
java.lang.System.out.println("Hello world") #just to check if jpype is installed correctly, works
jpype.imports.registerDomain("a")
from a.b.c.d.e.f.g.h import SomeClass #fails here
except OSError as err:
print(err) # ToDo: Remove print when done
pass
The error I am getting is that the module a.b.c.d.e.f.g.h.SomeClass could not be found. I have tried different ways to give the path (absolute path, relative path, place the jar in different places in the project and outside of it), but that doesn't seem to be the problem, as the path printed is the correct absolute path to the jar file.
Also, the jar is used in other (Java) projects and it works. It is created using maven package (using IntelliJ, if it is relevant, and the same Java version as the one used by the JPype JVM). In the Java projects, the import would be:
import a.b.c.d.e.f.g.h.SomeClass;
I have copied this and just transformed the syntax into Python.
I have also tried to create the class with JObject (which I probably didn't do right anyway) and also tried the older syntax (to my understanding) with JPackage. For the JPackage way, I am getting the exception that the package a.b.c.d.e.f.g.h.SomeClass.someMethod is not Callable, which to my understanding is an equivalent exception to the one I'm getting using jpype imports. I have already gone through all the questions I could find here with similar problems, but none of those solutions have helped me.
Can anyone suggest some possible solution? Or can anyone see what I'm doing wrong? Suggestions of other possibilities to replace JPype are also welcomed. If there is any clarification needed, I will edit the question.
The only thing that seems likely if the jar is on the classpath and failed to import would be for there to be some missing dependency. You have two other ways to try loading the class which may provide additional diagnostics.
jpype.JClass("a.b.c.d.e.f.g.h.SomeClass")
and
jpype.JClass("java.lang.Class").forName("a.b.c.d.e.f.g.h.SomeClass")
The first is manually loading a class by full class specification. It is mechanically what is happening under the import. The second is calling for Java to load the class (bypassing all of JPype). It returns a java.lang.Class which can be passed to JClass to make a wrapper.
Common failures include missing a jar or native library, attempting to start JPype from within a module and having the wrong relative path, error in initialization of the class due to missing resource. JPype is just calling JNI calls, so if everything is fine on Java end it should work. Given that you checked the java.class.path System variable, it has to be something to do with class resources.
The JPype user manual has an alternatives section if you would like to try to find another package. Most of the alternatives with the exception of PyJnius appear to be unmaintained.

How to import file from other folder in java?

I have just started learning java, so struggling in very basics. The problem i am facing currently is "cannot find symbol : class Ques". I have resolved this issue when i was accessing package from parent directory through CLASSPATH export. Now problem is i am trying to access sub-directory from sub-driectroy like this:
family.of.adam(has)/
father.java
WifeOne(sub-direc)/wifeone.java,ChildFromWifeOne.java
WifeTwo(sub-direc)/wifetwo.java,ChildFromWifeTwo.java
Now what i am trying to do is from wifetwo.java i am accessing wifeone.java. I have tried importing (wifeone)like this:
import family.of.adam.WifeOne.*;
import WifeOne.*;
In both cases it failed to import and same error occured which i mentioned above.
I have also tried solution provided in this Question but this effects classpath of WifeOne this is what i think because when i -cp method it starts showing errors related to wifeone.
I am using normal texteditor, compiling through terminal and using mac. Kindly brief me what mistake i am doing.
Assume src is your base folder(place you compile and run the program).
src/family/of/adam/FirstWife.java
if so you need to define the package startmetn the fist line of the FirstWife.java file.
package family.of.adam;
Then,If the second java file in the,
src/Main.java
In Main.java file you need to define the import statement for user the FirstWife.java class.
import family.of.adam.FirstWife;

Java disutil CoordinateConversions import problems

I'm writing a Java class that will be used to send PDUs across a network- to do this, I am following the tutorial at: Tutorial
In the example, the line:
double lla[] = CoordinateConversions.xyzToLatLonDegrees(c);
appears towards the end of the class, and I see that CoordinateConversions has been imported with the line:
import edu.nps.moves.disutil.CoordinateConversions;
I have tried using the xyzToLatLonDegrees(); method in the class that I am writing- calling it in the same way as is done in the example. However, for some reason, I get a compile error that says:
CoordinateConversions cannot be resolved
on the line where I'm trying to use it, and
The import edu.nps.moves.disutil.CoordinateConversions cannot be resolved
on the line where I am importing it.
Does anyone know why this is, and how I can fix the import, so that I can use the xyzToLatLonDegrees() method?
You need to have the CoordinateConversions class on your classpath. Either by obtaining the source and dropping it into your project (possibly adjusting package names, and only if the license allows), or by finding a JAR containing that class and adding it to your build path in your IDE.
You probably need to download the Java files from here.

Java library import difficulty

I'm trying to compile a small test program I have written for a raspberry pi.
The program makes use of the Pi4J library to control the piface add-on board.
What I have done so far is based on the following tutorial: http://www.savagehomeautomation.com/piface
While I can get the above example program to compile within my IDE after setting up the class paths I get compile errors with the one I have made and as far as I can tell the imports are set up in the same way.
It says that each of the following packages does not exist:
import java.io.IOException;
import com.pi4j.component.switches.SwitchListener;
import com.pi4j.component.switches.SwitchState;
import com.pi4j.component.switches.SwitchStateChangeEvent;
import com.pi4j.device.piface.PiFace;
import com.pi4j.device.piface.PiFaceLed;
import com.pi4j.device.piface.PiFaceRelay;
import com.pi4j.device.piface.PiFaceSwitch;
import com.pi4j.device.piface.impl.PiFaceDevice;
import com.pi4j.wiringpi.Spi;
I'm assuming that my problem is relatively simple but I don't really understand how import statements work and it's quite vague topic to search about. I have included some file paths if that helps.
This is where my project resides:
/home/pi/JBerries/relay
and this is where the pi4j library is:
/opt/pi4j
I hope the following image provides some of the information requested, note that the class paths are already set up:
You need to set the CLASSPATH environment variable to /opt/pi4j or the jar file therein. WIthout this the compiler is unable to know where your libary is located and will give you the errors you describe.
If you're using a project in JBerries you need to configure the classpath for the project - the screenshot shows the classpath for single-file compilations only. To edit the project config right-click the root node in the project window and select properties.

Categories

Resources