I could use some help here. I have been at this for two solid days now and searched all through this and other java forums and tutorials on the internet, all to no avail. I'm sure it's something dumb on my part (I'm pretty new to Java), but I don't know what it could be.
In other words, I have a directory named packtest, which contains two sub-directories: objects and utilities. The objects subdirectory contains a java sourcefile named Owner and utilities contains one named TicScreener.
I want to compile TicScreener into a user-defined package called com.util and place that package under packtest. Using the package statement (package com.util;) in TicScreener's source code and compiling with "javac -d .. TicScreener.java" from inside the utilities subdir, everything goes as planned and I get the following structure. So far, so good.
The problem comes when I try to import (import com.util.*;) into the Owner class. Using "javac -d .. Owner.java" from inside the objects subdir, I get a compiler error saying "package com.util does not exist".
I DO have "c:\packtest\;" in my CLASSPATH.
The only way I can get Owner.java to compile is if I manually move it up into the packtest directory, or manually move com.util into the objects directory. Obviously, I want to be able to write commonly used utilities and import them into other classes in other directories without moving and copying their directories around.
I'm sure this is something dumb on my part...everything I am reading indicates this should work.
Sorry to bug you about this, but it has me flabbergasted, bamboozled and discombobulated. And you know how painful that can be.
---------------------------------- Are you using package objects on top of the owners.java too? ----------------------------------
I don't quite understand the question. My intent was to create another package called com.obj which would hold the Owner.class file. The end result I am trying to achieve would look like this
With the Owner code referencing the TicScreener class (by virtue of importing com.util). This seems like a reasonable thing to do to me, but I am still something of a novice...is this approach sound?
At any rate, I did attempt to compile from packtest using "javac -d . objects\Owner.java" and that did work, resulting in the structure above. But what happens if I want to import from a completely different directory structure? Do I have to copy all my packages from one project directory to another, everytime I want to start something new? Seems to contradict the idea of creating reusable objects.
Thank you very much. The use of .jar files may be the missing link in what I'm trying to do (i.e. import of classes from different drives and directories). The link to the piece on "Mastering the Classpath" looks like it will be very helpful also.
Thanks again. I'll start reading and playing with this and see how things go.
One follow-up, though. Are you saying that one class cannot import another class from outside it's directory structure unless the other class is in a .jar file? So, if I have
That someClass must be in a .jar for Owner to do an import? It doesn't seem right to me, but if that's the way it works I'm not going to argue over it.
Please let me know if I am right or wrong about this.
I am posting first time to group, please bear with me :O)
In this given problem, I am skeptical of one thing, you may try that out.
For a package in classpath it would not have slash (backward) to say. so your classpath would look like ...;c:\packtest;... and if still you want to check whether classes are getting picked up, you try giving following command at command prompt :
javap com.util.TicScreener
this should show you all the methods in the class, basically this command should give you some output, other than class not found error. and this command can be given in any directory as long as classpath is correct.
Part of your problem is that you want to compile something into a package without having it in a matching package-like directory. If you want to compile TickScreener into the package com.util, java wants you to put it in the directory com\util (relative to any point in the classpath and to the current directory, for compiling purposes). This is just a little organizational imposition by Java which may seem silly at first, but turns out to be a nice thing, especially for really large projects.
I assumed Owner was in the default package; if you intended Owner to be in the package "objects" then you can stay in the package directory and compile it with "javac objects\Owner.java".
(There may be some special command line arguments to work around all these idiosyncracies of changing the current directory -- I'm not sure how IDEs jump through these same hoops).
By the way, TicScreener.java needs to start with a package com.util; statement (likewise Owner if it should be in a package) if it really wants to be in that package.
Finally, if you are not already using Jikes, instead of Javac as your compiler: it is a lot faster and gives better error messages.
Thanks everyone. Specifying the -classpath parameter in the javac command seems to handle it.
However, I really don't understand why it is necessary to do so, if I have the same path specified in the system classpath. Seems to me I should be able to simply set it there, without having to re-state each time I run the javac command. Any thoughts on that?
Adding the packtest directory to the classpath should work. In fact, after I suggested that you explicitly set the classpath in the compile command, I tried putting the directory in the system classpath; it worked. I tried it both with a trailing slash and without; both worked.
I'm assuming you are using windows. If so, can you execute the following and post the complete output here?
c:\packtest>dir
c:\packtest>echo %CLASSPATH%
I suggest that something is misspelled or otherwise incorrect in your CLASSPATH entry.
NOTE: That space in the path between C: and \WINNT where it says "C: \WINNT\System32\Wbem;" is NOT really there when I look at the path, either through the DOS listing or in the Control Panel. For some reason posting the message here caused it. It does not show up anywhere but in this forum. I spent about a half hour trying to get the message to post without it, but it keeps popping in. It does not appear when I run the "path" command in DOS nor when I edit the classpath variable through the Control Panel.