• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Ready to drink a jar

 
Ed Connery
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21419
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Ed Connery
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maneesh Godbole
Saloon Keeper
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Smitesh Shinde
Ranch Hand
Posts: 45
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21419
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ed Connery
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic