How to refer to classes and methods in a JAR file inside Eclipse?

Basically, I want to use class methods in a Jar file that looks like this:

IDE Screenshot

Can someone please tell me what I need to import in order to use these methods and variables?

+4
source share
2 answers

You do not need to import anything.

Jar files are not imported; they are added to the classpath.
In the screenshot that you posted, we see that the myJar.jar file myJar.jar included in your path to the eclipse path, so thereโ€™s nothing more to do.

Classes are imported if they are in another package.
Your Examplew class is in the default package. BMIcalculator also in the default package. Since they are the same package, it does not need to be imported.

You should be able to simply reference the BMIcalculator within Examplew . Just give it a try.

Try compiling this code - it should work:

 public class Examplew { private BMIcalculator calc = new BMIcalculator(); } 

You may receive warnings about an unused private field, but this time you can ignore it.

If this does not work for you, send an error message because it does not seem to be a problem with your import (or your class)

+4
source

Quote from this question :

You cannot use classes in a default package from a named package.

Prior to J2SE 1.4, you could import classes from the default package using syntax similar to this:

 import Unfinished; 

no longer allowed . Thus, to access the default package class from a packaged class, you need to move the default package class to your own package.

If you have access to the source generated with groovy, you need some post-processing to move the file to the selected package and add this โ€œpackageโ€ directive at the beginning.

+1
source

Source: https://habr.com/ru/post/1400911/


All Articles