views:

1089

answers:

8

I have a Java assignment that uses components to build program. The teacher gave us a JAR we are to use to build the calculator using Eclipse. The JAR has 2 classes. We must import the JAR and use its classes.

I import the JAR:

import SWEB401_HW1.NumericOperation;

but when I try to create an object for the class, it gives me an error because the constructor is not visible. What can I do to use the class "NumericOperation" to build the calculator?

A: 

If I understand your question correctly, you do not need to import anything. That's not how you access classes in a JAR file. Instead you need to add the path of the JAR file to Java's classpath; how exactly you do that depends on your operating system. Alternatively, you could extract the JAR file into the directory where your own program's class file is. A JAR file is just a ZIP file with a different extension, so you can extract it using whatever you normally use to open ZIP files, or you can use the jar tool included with the JDK:

jar xf SWEB301_HW1.jar
David Zaslavsky
I don't know how to use the cmd command .. i must to do something, then use the cmd .. because when i write this command, it give me msg:jar is not recognized ....
3yoon af
No need to extract anything. I bet the solution lies in springify answer.
Seb
Of course there's no *need* to extract anything - I never claimed there was - but that is one way to solve the problem I suspect the OP was having.
David Zaslavsky
+1  A: 

you have to add the jar file to the classpath, then you can import any classes that are included in that jar.

if you're executing the program from the command line you would add the jar to the classpath like this:

java -cp <path to jar> <program name>
Greg
I already import the jar using classpath, but i can't use the methods in the jar ..
3yoon af
A: 

There's no need to extract anything. You need to use the JAR in your project by adding it to the classpath.

Are you using Eclipse? If so, you can go to the project properties -> java build path -> libraries -> add external JARs -> search for your jars and add them.

That way you'll get all the JAR classes in your project.

Seb
i already do this step, but still i can't use the methods in the class
3yoon af
Then I'd suggest to follow springify comment. It pretty much covers what I was about to tell you now.
Seb
+3  A: 

With the information you provided - and considering that this is an assignment - I can only give you a few hints about what to look for.

Assuming your project is set up correctly, and you still cannot create instances of NumericOperation, ...

  • ... there could be static factory methods in NumericOperation.
  • ... the "other class" could act as factory for NumericOperation instances
  • ... NumericOperation could actually be an interface or abstract class that you need to implement

EDIT: Don't want to give it all away, so I'll keep this vague. As NumericOperation indeed seems to be an abstract class, try writing a class like the following and see what you must do to stop the IDE complaining:

public class MyNumericOperation extends NumericOperation {}

You can also have a look at the inheritance part of the Java Tutorial here.

springify
I think, NumericOperation is abstract class, but how we ganna use it without refer to it using the constructor ?
3yoon af
Oh, now i understand what you mean by abstract class ..I did this step before and it give me the following msg:implicit super constructor NumericOperation() is not visible. Must explicitly invoke antoher constructor
3yoon af
You will need to call one of the superclass' defined constructors with super(...) then. Have a look at NumericOperation to see what constructors are defined there.
springify
+2  A: 

If the constructor is not visible, then you are trying to invoke a non-public constructor. Look at the code or java doc for your NumericOperation class and find a constructor that is public. Most likely you're invoking the no-argument constructor and the class has specifically hidden it because you need an initial value.

For instance:

public class MyClass {
  private MyClass() {
    // Don't let callers instantiate me without args!
  }

  public MyClass(int initialValue) {
    // create a new object with initialValue
  }
}

If calling code attempts this:

MyClass obj = new MyClass();

You'll get the error you've posted. You need to call new MyClass(int) instead.

Kai
You don't need to add the private constructor. The default constructor is only added by Java if there is no other constructor. So as soon as a constructor with arguments was added (and no no-args constructor was added), you'll need to provide the arguments.
Joachim Sauer
+1  A: 

If the error you get is saying that the onstructor is not "visible", then it's talking about visibility in java (public, provate, protected and package).

This is good - it means that you have sucessfuly imported the class and that it's on your classpath. Ignore all the other answers that talk about fooling with your classpath - eclipse is taking care of it for you ok.

At a guess, your teacher f*cked up has not put a "public" declaration on the constructor you need to use.

To fix this, your class that you are writing needs to be in the SWEB401_HW1 package.

The easiest way in eclipse to do this is to right-click the java file in the navigator and to "refactor" it by "moving" it into package SWEB401_HW1.

paulmurray
A: 

If the instructor gave you Javadocs, an API listing, or the source look for things like this:

  • public static NumericOperation ()
  • public NumericOperation()
  • public NumericOperation ()

The first one could be in any class, but would likely be in the NumericOperation class.

The second one would be a constructor in the NumericOperation class that takes parameters. Remember that if the class provides a constructor the compiler does not generate an no-argument one for you.

The last one would be in another class (since you need an instance to call the method).

In the future posting the exact error message that the compiler spit out is more helpful than an rough statement of what it said.

TofuBeer
A: 

In agreement with some earlier answers, you need to have the jar available via the classpath, so when you run or compile do

 java(c) -cp myjar.jar:. etc 

I usually include the current directory so that it doesn't forget that my code is in the path.

When you import, you must use the java package names. Open the jar in a zip program and file the path to the class files you need, then put "import path;" where you replace the folder separators by '.'

Overflown