6

I'm having trouble adding a .jar file I downloaded for my Java project. This is really the first time I've used eclipse, so please bear with me and for some reason (I have no clue why), I just find it somewhat confusing.

I know that in order reference different class files you simply need to create a class library and add it to the build path. From there, all which needs to be done (unless I'm misunderstanding this for whatever reason) is use the "import" keyword to import whatever .jar, .java, or .class/.interface file necessary into the project.

I've tried that with my .jar. I have it referenced in the build path (all I did was just copy the jar to the project directory, and then use the build path option to add it externally), but when ever try to call the object "Delegator", which obviously is a part of the .jar file, it won't read.

Am I missing something here? Seriously, anyone who knows the answer to this - you're relieving a mother of a headache. And before anyone asks - yes, I've searched this one to death. I've found similar questions, but nothing which quite hit what I was looking for. Either that, or I really just lack the common sense.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
zeboidlund
  • 9,731
  • 31
  • 118
  • 180
  • Is your jar showing up in the referenced libraries of your project? Is your project building fine in eclipse or do you have build errors? – wjans Jun 09 '11 at 05:56
  • go to the build path->order and export tab->just checked the jar you have included in your project – BBdev Jun 09 '11 at 05:58
  • @wjans I haven't tried to build it yet - there's really nothing to build yet. All I've defined is an interface, and yes - the project shows up in the referenced libs. @Uttam Well, I checked off the .jar and still no dice, unfortunately. Any other thoughts? – zeboidlund Jun 09 '11 at 06:00
  • Here is the LIFESAVER !!! http://stackoverflow.com/questions/1334802/how-can-i-use-external-jars-in-an-android-project –  Nov 30 '11 at 18:19

6 Answers6

6

Right click on project->BuildPath->Libraries->Addexternaljar and then press ok and if it doesnot worked then you should go to the

Order and Export  tab
and checked the jar you have just added in your project. It will solved your problem.
BBdev
  • 4,898
  • 2
  • 31
  • 45
  • I already gave that a shot and unfortunately nothing happened. I appreciate your response regardless, though. – zeboidlund Jun 09 '11 at 06:10
5

There are several possible reasons, for the question hasn't mentioned the specific failure, and where it has occurred. The following is a list of possible reasons I could think of, but this may not be exhaustive:

  • You can import a class, in a different package only if the class is public. The only exception is when you are using the class in the same package. If the class is an inner class marked as private, then you're well and truly out of luck. The Delegator class in question might not be public, and that's why you may be unable to use it. This issue ought to be caught by the compiler.
  • The directory structure within the JAR might not match your package import statements in your classes. This might not be necessary, for Eclipse ought to provide possible fixes, but it is better to verify that nevertheless. Again, the compiler should complain if this is the case.
  • If the issue is at runtime, then, it is most likely that the JAR is not available in the runtime classpath. You'll need to configure the Runtime configuration, to add the JAR to the runtime classpath. Refer to the Eclipse documentation on run configurations, if you need to know how to change the runtime classpath.

Note:

  1. Exporting the build classpath entries would matter to other projects that depend on the pertinent project; unexported entries will have to be re-imported if required in other projects. This would not apply to a run configuration.

Update

Every Java application needs a main(String[] args] method to start execution. This is the entrypoint for the application. From the comment, it appears that the main method is in a different class. If so, the said class ought to be used to start the application. In Eclipse, a "Run configuration" might be used for the class that lacks this entrypoint, resulting in the described error. One can rectify this by creating a new Run configuration for the class with the said entrypoint. This may be done by one of the following:

  • editing the existing Run configuration to use the desired Class (the one with the main method). See the above link, in the third bullet point. Edit the value of the class to be launched.
  • creating a new Run configuration for the desired Class. Usually, you'll need to traverse to the desired class, and run your application (using the Alt+Shift+X+J shortcut) from the said class.
Vineet Reynolds
  • 76,006
  • 17
  • 150
  • 174
  • I honestly haven't even built it yet, but I'm extremely new with this IDE, as well as Java period. Would you mind telling me how I may go about altering the directory structure? – zeboidlund Jun 09 '11 at 06:03
  • Well, a couple of things would help you. Eclipse has its own compiler, which compiles classes on the fly. We need to know what error is being reported. Back to the question you've asked - if you're not sure about the directory structure, don't bother changing it. Use the `Ctrl+1` shortcut at the exact line/word of failure to see if Eclipse proposes a fix and choose it, if you believe it will fix the problem. – Vineet Reynolds Jun 09 '11 at 06:05
  • Ok, so when I try to run the program I receive an error telling me that the project doesn't have a "main" type. At first I thought this was just a main method with a String[] args parameter, so I gave that a shot and put that in a class. Still, nothing however. And I at least ruled this out: the source for the Delegator class is public. Also, the only option which I saw may be usable when I checked the line of error was "fix project", which just led me a "configure build path" prompt. – zeboidlund Jun 09 '11 at 06:18
  • @Holland, would it possible to post a screenshot of the problem, or paste the actual error reported by Eclipse? **Edit:** Never mind, I think I've figured your actual problem. Will edit the answer once again. – Vineet Reynolds Jun 09 '11 at 06:18
  • @Vineet Reynolds, did you have any luck on figuring out what the problem might be? – zeboidlund Jun 09 '11 at 15:32
  • @Holland, your run configuration might be referring to a main class that lacks a main method. – Vineet Reynolds Jun 09 '11 at 15:34
2

i was facing similar issue with spring jar files but then tried with different jar files and it work so I think , classes defined in jar files were private and not available outside of jar hence you were not able to access the file .

thanks , Raju Rathi

Raju Rathi
  • 21
  • 1
1

Right click on the project--->Build Path--->Configure Build Path...--->In left side you have to choose Java Build Path--->Libraries--->Add External JARs--->ok--->ok

karthik
  • 137
  • 4
0

Steps to add jar file in eclipse 1. right click on project 2. click on Bulid Path->configure path 3. click on java Build path 4. Click on libraries tab 5. click on add external jar tab 6. choose jar file 7 click on ok

ashok
  • 1
0
  1. Copy the .jar file in libs folder which you want to add in your project.
  2. Right click on .jar file -> Add Build Path

Done.

pioneerBhawna
  • 578
  • 7
  • 15