0

I am always confused by the Java imports like "import com.smth.smth" or "import org.name1.name2.smth", which are errors in my Java code.

How one has to resolve them, possibly with Eclipse?

What are the names of such imports? I tried to google it, but the answers I find always say to download some .jar and add it to the build path through "Add external..."

I do not understand these imports looking like web-addresses. How do they work? Explane me please, or give a reference.

Thank you.

Picture 1:

enter image description here

Picture 2:

enter image description here

jupiter_jazz
  • 333
  • 2
  • 8
  • 2
    An `import` is not something that is resolved, it is a short-cut for fully-qualified class names in your local namespace. If you are attempting to use a class from a 3rd party package, then you should "download some .jar and add it to the build path". – Elliott Frisch Nov 24 '19 at 02:13
  • well, I know what you mean. But, in my case, these imports produce error with the annotation "Import X cannot be resolved". – jupiter_jazz Nov 24 '19 at 02:16
  • @ElliottFrisch added a picture for you – jupiter_jazz Nov 24 '19 at 02:18
  • @ElliottFrisch so, you cannot use the direct references like, ask your IDE to use some Git repo, external page, etc, - you always have to grab it as a .jar into the project?! – jupiter_jazz Nov 24 '19 at 02:20
  • It cannot be resolved because you have nothing on your classpath that has a `org.myrobotlab.....` class. You need to either have the source code for that class in your project or it needs to be included in a jar file from somewhere (that you specified as a dependency). – Thilo Nov 24 '19 at 02:20
  • If you do not want to manually find and download the jar, you can use a system like Maven. It will find and download it for you (but you still have to specifiy which version you want). https://stackoverflow.com/q/3072205/14955 – Thilo Nov 24 '19 at 02:21
  • @Thilo I am confused about the dependencies: the fully-qualified name of the import is (e.g.) "org.myrobotlab.service.interfaces.DeviceControl". I see this after donwloading the .jar like "myrobotlab.jar" in the referenced libraries. So, why do you specify ".com" or ".org" domain, if you have to download the .jar file anyway? – jupiter_jazz Nov 24 '19 at 02:23
  • You also have to specify `import java.util.List` even though that jar is already part of your JDK. The compiler does not just import everything from all jar files you happen to have. Think of how many classes called `List` there are. How would it know which one you want to use? That fully qualified class names exists to disambiguate. And it uses a namespacing similar to DNS to make it global. No one else except those robot lab people are going to use `org.myrobotlab` for their stuff. – Thilo Nov 24 '19 at 02:25
  • But after you have the jar, your IDE will automatically find and create the import statement when you type `new DeviceCon<>`. No need to manually write those anymore. – Thilo Nov 24 '19 at 02:29
  • @Thilo is does not answer a question, unfortunately. I have a conflict like in the picture 2. What do I do? – jupiter_jazz Nov 24 '19 at 02:33
  • What is the error message there? – Thilo Nov 24 '19 at 02:33
  • @Thilo please refer to the picture 1 – jupiter_jazz Nov 24 '19 at 02:39

2 Answers2

1

In Project > Properties: Java Build Path in the tab Libraries a library/dependency (for instance a JAR or class folder) that contains the types (classes, interfaces, enums or annotations) org.myrobotlab.service.interfaces.DeviceControl etc. have to be added.

If you have a module-info.java file, in addition requires ... statements for the modules containing the types have to be added.

import statements are a fundamental thing in Java, so it would be best if you would learn the basics of Java first.

howlger
  • 31,050
  • 11
  • 59
  • 99
0

when importing from a .jar you are just accessing the classes within as if they were with all of your other classes. you have to download the jar and add it too your build path to use anything in it.

organizations or companies usually title their .jar file's packages with org/com (if they are company or organization), then sub package (what the package does) , and then the class name .

RIVERMAN2010
  • 427
  • 3
  • 9