5

Basically I wish to use the methods of a class within the Jar file, which looks like this:

IDE Screenshot

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

Perception
  • 79,279
  • 19
  • 185
  • 195
Banned
  • 201
  • 6
  • 17

2 Answers2

4

You don't need to import anything.

Jar files aren't imported, they are added to the classpath.
From the screenshot you've posted, we can see that the myJar.jar file is included in your eclipse classpath, so there's nothing more to do there.

Classes are imported, if they are in a different package.
Your Examplew class is in the default package. BMIcalculator is also in the default package. Because they are the same package, you don't need to import it.

You should be able to simply make references to BMIcalculator from within Examplew. Just try it.

Try compiling this code - it should work:

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

You might get warnings about the unused private field, but you can ignore that for now.

If that doesn't work for you, then please post the error, because it doesn't look like the problem is with your imports (or your classpath)

Tim
  • 6,406
  • 22
  • 34
  • I get an error saying that BMICalculator cannot be resolved to a type. – Banned Mar 12 '12 at 01:47
  • My mistake - it should be "BMIcalculator" I'll update my answer - can you try again. – Tim Mar 12 '12 at 01:48
  • Thank you, that worked. However how I refer to the methods and variables? From my understanding we've create a new private object called calc, so if I were to to do calc. I should see all the uses,right? – Banned Mar 12 '12 at 01:52
  • Well, now we're getting into a general "how to write Java code" question, which is pretty hard for us to answer here. You'll need to write a method in your `Examplew` that does whatever your assignment asks you to do. If you need help with that, then ask a new question - it looks like this question is solved - you now have a reference to the calculator class. – Tim Mar 12 '12 at 01:55
1

Quote from this question:

You can’t use classes in the default package from a named package.

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

import Unfinished;

That's no longer allowed. So to access a default package class from within a packaged class requires moving the default package class into a package of its own.

If you have access to the source generated by groovy, some post-processing is needed to move the file into a dedicated package and add this "package" directive at its beginning.

Community
  • 1
  • 1
Jakub Zaverka
  • 8,816
  • 3
  • 32
  • 48
  • Hmm, is there another method that you may suggest as it seems that I would be touching into unknown areas that I have no idea what I would be doing in. Originally, I had the class file and was told that would suffice however it seems that I HAVE to make a JAR file but that road now a dead end as well :() – Banned Mar 12 '12 at 01:31
  • Cant you just add `package myJar;` to the beginning of the BMIcalculator class and create the Jar again? – Jakub Zaverka Mar 12 '12 at 01:37
  • The thing is I was given the class file and when I try to open it I am told to attach a source but that's a no go :(. My lecturer had said the following : "You can find the code class file and javadoc html on XXXXX. You need to copy the .class file into your project folder. You do not need the .java source file." – Banned Mar 12 '12 at 01:39
  • So you have only the BMIcalculator.class and not BMIcalculator.java? – Jakub Zaverka Mar 12 '12 at 01:43
  • Ugh, then the teacher is either deliberately messing with his students or the last java version he saw was 1.3. You can import non-packaged classes in Java prior to 1.4, so you can go try download a JRE version 1.3 and then in your project properties go Java Build Path and set Compiler compliance level to 1.3. But this is more like hack than a clean solution. – Jakub Zaverka Mar 12 '12 at 01:49
  • You try it first without downloading the 1.3 JRE, just set the Compiler compliance flag, that worked for me. – Jakub Zaverka Mar 12 '12 at 01:51
  • @JakubZaverka - you're leading him down the wrong path - his `Examplew` class _is in the default package_, he doesn't need to import the `BMIcalculator`. – Tim Mar 12 '12 at 01:53