views:

76

answers:

3

I am a brand new Java developer (I have been working in asp.net) and I inherited a project I am trying to get running correctly. I am on Windows 7 and have Tomcat installed and I am using Eclipse

I'm trying to figure out how the heck Jar's work in java and I am really confused at the moment. I have been playing around with it and looking up stuff on the web for about a day now and this is where I am:

I have a dynamic web project which I inherited and it has what I am going to assume is a fairly standard project layout. There is a lib folder in the project root which contains some jar's.

If I don't do anything and go to a particular page in the site I get this error: "The import net.sf cannot be resolved, XLSTransformer cannot be resolved to a type"

So I added jxls-core-0.9.8.jar to my build path in Eclipse and that error went away but now I am getting this error "java.lang.ClassNotFoundException: net.sf.jxls.transformer.XLSTransformer"

I copied that jar from its current location to the tomcat/lib directory and that error went away. Now I am getting essentially the same issue with another file.

Mainly I just need to know how should jar's work? Do they need to be in the class path, in web/WEB-INF/lib, in tomcat/lib, in all of them, none of them I just can't figure it out.

Also when I was looking around I saw some stuff that indicated the order of my classpath entries matters as well. If that is the case how do you insure you have them in the correct order.

A: 

Where your jars need to go actually depends - there are entire best practices and major headaches involved. The term Jar Hell exists for a reason.

All classes used by your application must be on the classpath. How they get there will vary depending on the classloader.

For a fuller understanding, you'll want to do reading on the Java classloader. Ideally you'll want to look into Tomcat's classloader, or at least a Servlet Container's classloader, as the classloader for Tomcat will differ from that for a Java SE (desktop) app, will differ from that for an applet. On top of that, classloaders can really bite you if you don't understand them. If you want the gory details of how a servlet container ought to work, you can check out the latest and greatest Servlet specification.

The information on Tomcat's classloader provides a best practice list for you. Note that I've linked a 5.5 version. If you are using a later or earlier version, check around for the same file for your version.

justkt
A: 

Here's a suggestion that might help in troubleshooting the problem.

WAR files (Web Archives) will put all of your application's dependency JARs in the directory WEB-INF/lib. If you get a report of a missing class, try unzipping your WAR file, or looking in the exploded webapp directory of your app server, and see if the necessary jar is there.

The difference between this location and tomcat/lib is that JARs in tomcat/lib are made available in Tomcat's parent classloader for all loaded apps. (Indeed this can cause a problem if an app has its own version of a particular JAR in WEB-INF/lib.) The JARs in WEB-INF/lib are loaded in the application's classloader and are only available to that app.

If your JAR is missing from WEB-INF/lib, check your build scripts or the Build Path of your project in Eclipse. Good luck!

Mojo
A: 

In Eclipse, I export as "Runnable JAR file", and in the dialog after, under "Library handling", I choose "Extract required libraries into generated Jar".

Basically, what this means is that if I can run it in Eclipse without error, it will run stand alone too since everything that I've told Eclipse is required will be included in the JAR.

Marcus Adams