Myn
Myn

Reputation: 691

Unable to use JAR in Eclipse

I have just created my first JAR in Eclipse, just a simple program with a single class Database.class. It is not in a package.

public class Database {
public Database() {
int dbInit = 1; } }

I have added it as an external JAR to the build path libraries for another project in Eclipse, but for some reason I cannot get Database db = new Database(), the default constructor, to work - it's as if the contents of the JAR are not being recognised.

Could anyone please offer any advice on this?

Thanks very much,

M

Upvotes: 0

Views: 925

Answers (3)

AlexR
AlexR

Reputation: 115328

  1. typically this works, so relax: you did some mistake and can fix it.
  2. check content of your jar: run jar vft myjar.jar

You should get output like Database.class

Check that it is exactly what you get. Your class file must be at the root of the jar.

  1. Verify that you are adding it to your second project correctly: Project/Properties/Java Build Path/Libraries, push button "Add external jars...", navigate to the jar and add it.

  2. Now try to write in any java class of your project: Datab then push ctrl/space It should complete to Database. Continue coding and enjoy.

BTW: why did you put your class to default package? I'd suggest you to put it into package. It will help you to avoid mistakes. for example probably you have other class named database in your code. How are you planning to resolve this conflict?

Upvotes: 2

djna
djna

Reputation: 55907

The build path is not used at runtime. In your run configuration there's a tab to allow you to specify the classpath used when running the app.

Upvotes: 0

Navi
Navi

Reputation: 8736

You might have to rebuild or clean your project.

Upvotes: 0

Related Questions