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.
Related
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.
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.
I've been using Eclipse for a while and I'm having trouble understanding what's going on with my first project in IntelliJ. I've read the documentation, and searched other questions, but I still can't seem to grasp it. I think there is something wrong with my project structure. This is what my structure currently looks like;
I'm trying to run the JavaForLoop class, but whenever I do, compilation fails because I have errors in the StringMethods class of the strings package. My question is why would that prevent compilation if the two classes are in separate packages? Neither class uses the other, and they both have the appropriate package declaration statements. With a similar structure in Eclipse, this would work. Should I be using a different project structure?
By default IDEA adds Build Configuration which is executed before launch and includes following steps (taken from here):
Compiling source code in the source path of a module and placing results to the output path.
Compiling source code in the test path of a module and placing results to the test output path.
Creating copies of the resource files in the output path.
Reporting problems in the Messages tool window.
check if it's your case in Edit Configuration screen and if so, remove it.
To use a class from a different package you must declare a import statement to the class.
In your JavaForLoop.java add the import before the class statement (and after package declaration where its the case)
//package ...
import strings.StringMethods;
//public class JavaForLoop { and the rest of the code
Intellij uses regular javac, which will fail to compile if you have errors anywhere in the code.
Eclipse has it's own compiler, that allows to compile and even run code that has compilation errors, causing a runtime exception if any part of the code that has errors is run. This allows you to run parts of the code that work even if other pieces of code are failing.
The simple solution is to resolve your compilation errors. You can also use the eclipse compiler with Intellij, but I've never done this so I can't comment on how well it works.
I'm trying to develop an email parser - to take email that's in a file and be able to programmatically handle the various components - to know who the sender and recipient were, the subject line, main body, and any attachments. I intend to extract attachments as individual files, but I'm stumped right at the beginning.
I started with an already working java program in a fully-functional development environment and have begun adding to it. One of the first additions was this line (then later, set of lines):
import javax.mail.*;
import javax.mail.internet;
import javax.mail.internet.MimeUtility;
Later, in an appropriate place, we have this humble beginning:
MimeMessage m = null;
I was shocked to find that the compile failed with only these two changes, and I learned here (on another StackOverflow page) that the package javax.mail package isn't included in the standard JDK. Puzzled, I looked and found this:
# rpm -qa | grep -i java
android-json-org-java-4.3-0.2.r3.1.fc21.noarch
snappy-java-1.0.5-2.fc21.noarch
tzdata-java-2015b-1.fc21.noarch
python-javapackages-4.1.0-7.fc21.noarch
postgresql94-jdbc-javadoc-9.3.1101-1.f21.noarch
protobuf-java-2.5.0-11.fc21.x86_64
java-1.8.0-openjdk-1.8.0.40-25.b25.fc21.x86_64
java-1.8.0-openjdk-headless-1.8.0.40-25.b25.fc21.x86_64
javassist-3.18.1-2.fc21.noarch
apache-commons-javaflow-1.0-0.8.20120509SNAPSHOT.fc21.noarch
javapackages-tools-4.1.0-7.fc21.noarch
java-1.8.0-openjdk-devel-1.8.0.40-25.b25.fc21.x86_64
antlr3-java-3.5.2-2.fc21.noarch
javamail-1.5.1-3.fc21.noarch
xz-java-1.5-3.fc21.noarch
abrt-java-connector-1.1.0-2.fc21.x86_64
Please note that yes, actually, JavaMail is installed - version 1.5.1-3. However, just to be a belts and suspenders kinda person, I found the JavaMail project and downloaded the latest production version, and put it in the CLASSPATH.
To my great surprise, this did not cure the problem! Being the careful type, and keeping things simple to prove the way as I go, I simply removed the javax.mail.jar file from the library directory, then removed the two lines from my program, recompiled and it worked. Then, I added back in the import line, and it failed. Then I moved the jar file back into the library directory and the compile succeeded, confirming that the javax.mail package was being loaded.
However, when I added in the first reference to the library, MimeMessage (see the line above), the compile failed.
So, of course, I went to check the documentation! Indeed, MimeMessage is an available class.
What am I doing wrong?! I mean, geez, I've been using Java literally since version 1.0 - I'm not known for making too many dumb mistakes, but I figure I must be!
Two open questions come to my mind:
1) Why isn't the installed version of the JavaMail package being used? CLEARLY I had to add it. And do note that I don't have to do ANYTHING special to CLASSPATH (or anywhere else) to get all the rest of Java! And;
2) Now that I've got a JavaMail package in there, why is it not discovering the MimeMessage class?
Any / all help appreciated - and if you spot me being abjectly stupid, please point out my error gently!
Additional Information:
I added two more import lines, so there are now the three listed above. Curiously, when I have the .jar file in the path specified by CLASSPATH, but not explicitly cited, I get one error per import, but when I explicitly cite the .jar file as an explicit item, I only get ONE error! That one error is on:
import javax.mail.internet;
Naturally, knowing that the jar is actually just a zip, I unzipped and looked. Sure enough, a directory named "internet" is there, populated with 38 class files.
Somewhere along the way, I had a wild idea that somehow there was ANOTHER javax.mail entry on the system or in the CLASSPATH, so I looked. This is Fedora Core, so it's under /usr/lib, and I did NOT find another file including the string "mail" in its name, so I presume that's not it, either.
I noticed in the NOTES.txt file that there were some notes about not unpacking the jar for some applications because that was seen as a security risk, but I figured that for what I want to do there is no such risk and maybe it would work, but I couldn't get THAT to work either...
I've also been trying using -cp versus actually altering the CLASSPATH variable, but it doesn't seem to matter. I apparently get identical results either way, so I've just been using -cp for testing because it's faster to try various alternatives.
MimeMessage is in the javax.mail.internet package, which you haven't imported.
EDITED BY QUESTION ASKER:
Actually, I was trying to import, among other things:
import javax.mail.internet;
And that didn't work. What I was missing was that there's nothing to import on just javax.mail.internet. Changing it to:
import javax.mail.internet.*;
worked just fine!
So, while Bill wasn't quite correct, this answer plus his comment got me to see what I was blind to... and I feel silly!
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;