Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

more classpath issues  RSS feed

 
Jeremy James Brown
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hopefully I can get some more classpath help here. I'm starting the packages part of my book and I can't get javac to read the classpath to the classes I compiled.

here is my javac error



I have my classpath environmental variable set to

.;C:\Program Files (x86)\Java\jre6\lib\ext\QTJava.zip;K:\Java;

I tried both the semicolon at the end and without.

Here is my import statement.



Here is the full path to the folder containing the imported classes.

K:\Java\org\cadenhead\ecommerce

Any help is greatly appreciated!



 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're going to do this with classes and directories and so forth, then here's how I've had it work: you need to put Storefront in a directory named ecommerce, which is in a directory named cadenhead, which is in a directory named org. Org can be in any directory, let's say it is going to go into "Java", as you've indicated.

Assuming GiftShop does not have a package statement, it can also go in Java. Then you can execute your javac, and that should work.

There are other ways you could get this to work, but all the ones I know of involve compiling Storefront separately and accessing its .class file to compile GiftShop; I won't try to describe those on the assumption you're trying to compile both at once.

If you wanted to compile GiftShop by itself, you would need to enter:

K:\Java>javac org\cadenhead\ecommerce\Storefront.java

rc
 
Jeremy James Brown
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ralph Cook wrote:all the ones I know of involve compiling Storefront separately and accessing its .class file to compile GiftShop


Yes that's what I want to do. The part of the book I am using asks us to import the compiled classes. I compiled them and placed them in the directory just like you said. You can see the path in my last post, as well as the import statement. I've read through the book s few times now and I can't figure out why it won't work.

I am starting to think maybe I should just start using eclipse and let it take care of this stuff for me. I'm trying to relearn programming, and learn java and oop all at once. I think that is enough without dealing with cmd line and classpath issues too.
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may have a point about not learning it all at once, and the classpath stuff is kind of frustrating. However, I think it is very worthwhile to know it. Let me explain a concept or two, and then make a guess about your situatioon.

A Classpath names one or more places where java, either compile or runtime, can expect to find classes. Each element of the classpath specifies the "empty package" location for that element. Therefore, if your fully-qualified class name is org.cadenhead.ecommerce.Storefront, then one element of your classpath should point to wherever the "org" directory or its equivalent is located.

I say "or its equivalent" because classpath can contain a jar file; the jar file needs to have Storefront within its directory within the jar (aka zip) file; the class file cannot just be in the jar file without its directory structure behind (in front of?) it.

Now, you say you have a "." in your classpath; that indicates your current directory; if that directory has an "org" directory in it, and the rest of the tree is in place, and Storefront.class is in "ecommerce" down where it's supposed to be, then that should work. So I'm not sure why it is not working.

Your error message indicates, however, that ".\Storefront.class" has the wrong class "org.cadenhead.ecommerce.Storefront"; I *think* that means that you have a correctly compiled class file, but in the Java directory. The class file must be in the directory tree matching the package structure, just as the java file must be to be compiled. Do you also have it in the tree where it's supposed to go? If you do, then I'm guessing the java runtime is picking up the bad one (in the Java directory) first, and erroring out on it because it's just bad, it cannot be used where it is. If you don't have one where it's supposed to go, then the runtime is trying to be helpful and point out that you have it in the wrong place.

I hope that made enough sense.

rc
 
Jeremy James Brown
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ralph Cook wrote:I *think* that means that you have a correctly compiled class file, but in the Java directory.
rc


I had one in the Java Dir and the ecommerce dir. I didn't realized that would mess things up. I thought javac would know were to look because of the import statement. But, now I remember that javac looks up the dir tree one step at a time.


However, I think it is very worthwhile to know it.


Me too. That's why I am doing it the cmd line way. But after two hours of not being able to move forward I was getting frustrated. I'm sure I will move to an IDE soon enough, but I would like to learn how things work manually first.

Thanks a lot! Now I go to sleep having gotten somewhere today.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!