File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Ready to drink a jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Ready to drink a jar" Watch "Ready to drink a jar" New topic
Author

Ready to drink a jar

Ed Connery
Ranch Hand

Joined: Mar 11, 2010
Posts: 70
I've looked in three Java books (and tutorials here) and they all show how to create a JAR file but don't imply there's anything unusual involved in using them once they're on the CLASSPATH.

This problem came up in OOP-1 in the Cattle Drive, so if you haven't done OOP-1 yet, please get lost, now (which is why I'm not posting this in the Cattle Drive forum).

Here's my CLASSPATH: .;C:\Java\Thinking in Java III\Source Code;C:\Java\RanchCommon

The .jar file is in the RanchCommon directory but javac seems to be ignoring it. If I unzip the .jar file right there, creating the com subdirectory (etc), my program compiles and runs without a hitch. I also tried putting the .jar file in C:\Program Files\Java\jdk1.6.0_17\lib\ext (off JAVAHOME, in other words). No joy.

What the devil am I doing wrong?


Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40


A class path is used to specify jar files, and root directories for class files. It is *not* used to specify the directory for jar files. Add the jar file explicitedly to the class path.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ed Connery
Ranch Hand

Joined: Mar 11, 2010
Posts: 70
Thanks for the quick response!

Two things, though. I tried that and it didn't work, suggesting that I did it wrong. I'll play with that some more tomorrow.

The other thing is a question: what happens when you have a lot of code in multiple .jar files? Doesn't the classpath get a bit complicated?
Nilesh Kanawade
Greenhorn

Joined: Mar 17, 2010
Posts: 3
Hi Ed,

You just have to include jars by using ; as a delimiter. only thing is your classpath command looks complicated by logically its not!

Also if you are using any editor such as Eclipse then life gets more easy. As you have to just add jars in the library as External jars.


Cheers!


"We are what we repeatedly do. Excellence, therefore, is not an act but a habit."
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Ed Connery wrote:The other thing is a question: what happens when you have a lot of code in multiple .jar files? Doesn't the classpath get a bit complicated?


Probably but we really don't care. Smarter folks than me wrote the JVM and they figured all this stuff out ages ago. I've seen some pretty monstrous classpaths before but it all still works.


GenRocket - Experts at Building Test Data
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10360
    
    8

Ed Connery wrote:
This problem came up in OOP-1 in the Cattle Drive, so if you haven't done OOP-1 yet, please get lost, now (which is why I'm not posting this in the Cattle Drive forum).


Do I understand you correctly, that you are actually asking people to get lost if they haven't done OOP 1 yet?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Smitesh Shinde
Ranch Hand

Joined: Feb 16, 2008
Posts: 45

Ed,

It looks like it is the problem of spaces in the folder name appearing in your classpath e.g. "Thinking in Java III". You have to put the path inside double quotes in the case. And of course you have to include jar files explicitly inside classpath.
Ed Connery
Ranch Hand

Joined: Mar 11, 2010
Posts: 70
Maneesh Godbole wrote:
Ed Connery wrote:
This problem came up in OOP-1 in the Cattle Drive, so if you haven't done OOP-1 yet, please get lost, now (which is why I'm not posting this in the Cattle Drive forum).


Do I understand you correctly, that you are actually asking people to get lost if they haven't done OOP 1 yet?

That was just a Spoiler Warning, I guess the joke miscarried. Sorry.

Smitesh, thanks for the good spot! I'll try that.

Anyway, I got confused by the text in Just Java 2 (6th Ed), pp. 102-103, for example: "Putting the jar file where Java will find it: Java looks in the directory $JAVAHOME/jre/lib/ext for jar files. You can install your programs just by placing the jar file containing them in this directory. They will be found automatically without further actin on the user's part." Then under "Telling Java where to look for the jar file or package roots" it says "The JDK will look at the CLASSPATH environment variable to know where to look for top level packages/directories... You set CLASSPATH to be a list of all the directories where the JDK should start looking for your top level package/directory names." There's no mention of adding the jar by name.

The problem is similar in Core Java but (looking again) at least their examples show "archive.jar" in the CLASSPATH. There's no mention at all of CLASSPATH when Thinking in Java talks about JAR files. Okay, I'm done grumbling.

[Edit] OK, I lied. I realize I must be making an ignorant mistake.

I simplified my CLASSPATH:

.;C:\Java\RanchCommon\jr.jar

Here's my import statement:

import com.javaranch.common.*;

Here is the path going inside jr.jar:

C:\Java\RanchCommon\jr.jar\com\javaranch\common\

GDate.class and JDate.class are in that directory.

An example statement would be

GDate today = new GDate();

Both instances of GDate are flagged as "cannot find symbol...class GDate"

The code works fine if I unzip the jar in RanchCommon.

I am at a loss.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Ed Connery wrote:The other thing is a question: what happens when you have a lot of code in multiple .jar files? Doesn't the classpath get a bit complicated?

I have a production application which at last count had 47 jars in its classpath. However I did take care to put them in a directory whose name is a lot shorter than the one you chose. And yes, whoever mentioned the problem about spaces in the path name was also worth paying attention to.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40


Many many years ago, I had an application, on some OS (either AIX or HPUX, don't remember anymore) that had a class path that exceeded the max environment variable length. The classpath had to be shortened, by using shorter directory names. We were also thinking of extracting the jar files to a common location -- and overlaying them too.

These days, I don't think any of the shells have a max limit, so it is not a concern anymore.


As for confusion, production applications are generally started via scripts. And scripts don't declare a big classpath. Instead, they declares lot of different paths (and commented). The big classpath is generated from the many smaller (and hopefully, less confusing) paths.

Henry
Ed Connery
Ranch Hand

Joined: Mar 11, 2010
Posts: 70
Even with the spaces, the classpath worked as long as I unzipped jr.jar. So the compiler finds its way to the directory, but not into the jar. So, what mistake am I making in the steps I showed a couple of messages back?
Ed Connery
Ranch Hand

Joined: Mar 11, 2010
Posts: 70
I'll bump this once because I'm at a standstill on this problem, and it seems like the most basic situation of using a .jar. Again, if I unzip jr.jar in the RanchCommon directory, the program runs fine, so the classpath works except for the .jar.

I simplified my CLASSPATH:

.;C:\Java\RanchCommon\jr.jar

Here's my import statement:

import com.javaranch.common.*;

Here is the path going inside jr.jar:

C:\Java\RanchCommon\jr.jar\com\javaranch\common\

GDate.class and JDate.class are in that directory.

An example statement would be

GDate today = new GDate();

Both instances of GDate are flagged as "cannot find symbol...class GDate"

The code works fine if I unzip the jar in RanchCommon.

I am at a loss.

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3529
    
  15
Ed Connery wrote:Here is the path going inside jr.jar:

C:\Java\RanchCommon\jr.jar\com\javaranch\common\


Is this the actual directory structure that appears in your jar. If so, then that's your problem.
The jar should only include the directory hierarchy starting at (and including) the com directory i.e. com should be the top level directory in your jar.


Joanne
Ed Connery
Ranch Hand

Joined: Mar 11, 2010
Posts: 70
com is one of the top level directories in the jar, along with directories META-INF, src, test, and a build.xml file.

[Edit] OK, now I'm mystified. I moved jr.jar up from C:\Java\RanchCommon to C:\Java and changed my classpath accordingly. Presto! The damned thing worked!

I moved it back down to RanchCommon and changed the classpath back. Bang. It blows up.

Repeat as needed. So I know how to access and use a jar file...if that jar file happens to be in C:\Java. That doesn't make me feel that I understand how to properly use jar files. [By the way, I checked and yes I *did* use proper capitalization of the directory names.]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ready to drink a jar