The Artima Developer Community
Sponsored Link

Java Answers Forum
Problems with Classpath, Packages and Imports

18 replies on 2 pages. Most recent reply: Sep 4, 2003 11:21 AM by Ken Sloan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 18 replies on 2 pages [ 1 2 | » ]
Ken Sloan

Posts: 35
Nickname: sloan
Registered: Sep, 2003

Problems with Classpath, Packages and Imports Posted: Sep 1, 2003 12:09 PM
Reply to this message Reply
Advertisement
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.

I have a simple directory structure as follows.

c:\
|
|----packtest
| |
. |----objects
. | |
. | |----Owner.java
|
|----utilities
| |
| |----TicScreener.java

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.

c:\
|
|----packtest
| |
. |----objects
. | |
. | |----Owner.java
|
|----utilities
| |
| |----TicScreener.java
|
|
|----com
|
|----util
|
|----TicScreener.class

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.


Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Problems with Classpath, Packages and Imports Posted: Sep 1, 2003 4:09 PM
Reply to this message Reply
Are you using package objects on top of the owners.java too?

Why don't you try to compile from the packtest directory instead of going into the objects sub directory to compile owners.java

from packtest you could do javac objects/owners.java I guess.

Ken Sloan

Posts: 35
Nickname: sloan
Registered: Sep, 2003

Re: Problems with Classpath, Packages and Imports Posted: Sep 1, 2003 8:54 PM
Reply to this message Reply

----------------------------------
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

c:\
|
|----packtest
| |
. |----objects
. | |
. | |----Owner.java
|
|----utilities
| |
| |----TicScreener.java
|
|
|----com
|
|----util
| |
| |----TicScreener.class
|
|
|----obj
|
|----Owner.class

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.

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Problems with Classpath, Packages and Imports Posted: Sep 2, 2003 12:23 AM
Reply to this message Reply
>>But what happens if I want to import from a completely different directory structure?

In that case you will be creating a JAR file and having that in the classpath.

Also why don't you reffer this link and see.

http://www.kevinboone.com/classpath.html

Ken Sloan

Posts: 35
Nickname: sloan
Registered: Sep, 2003

Re: Problems with Classpath, Packages and Imports Posted: Sep 2, 2003 5:00 AM
Reply to this message Reply
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.

Ken Sloan

Posts: 35
Nickname: sloan
Registered: Sep, 2003

Re: Problems with Classpath, Packages and Imports Posted: Sep 2, 2003 6:50 AM
Reply to this message Reply
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

c:\
|
|----packtest
| |
| |----objects
| |
| |----Owner.java
|
|
|----someDir
|
|----somePckg
|
|-----someClass.class


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.

Joe Parks

Posts: 107
Nickname: joeparks
Registered: Aug, 2003

Re: Problems with Classpath, Packages and Imports Posted: Sep 2, 2003 6:57 AM
Reply to this message Reply
Having you tried the following command, from within the objects directory?

javac -classpath .. -d .. Owner.java

Obviously I don't have the same code as you, but this worked for me.

Joe Parks

Posts: 107
Nickname: joeparks
Registered: Aug, 2003

Re: Problems with Classpath, Packages and Imports Posted: Sep 2, 2003 6:59 AM
Reply to this message Reply
No. You do not need to reference a jar file.

Arun Agarwal

Posts: 5
Nickname: arunrecw
Registered: May, 2003

Re: Problems with Classpath, Packages and Imports Posted: Sep 2, 2003 10:36 AM
Reply to this message Reply
Hi.

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.

other things look fine.


Arun.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Problems with Classpath, Packages and Imports Posted: Sep 2, 2003 3:28 PM
Reply to this message Reply
It's not necessary to build a jar file, to use packages. (You may want to build one for deployment, however, but that is another subject).

Simply this should do the trick:

set classpath=%classpath%;c:\packtest
pushd c:\packtest
move utilities\TicScreener.java com\util
javac com\util\TicScreener.java
pushd objects
javac Owner.java
java Owner


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.

Ken Sloan

Posts: 35
Nickname: sloan
Registered: Sep, 2003

Re: Problems with Classpath, Packages and Imports Posted: Sep 2, 2003 4:22 PM
Reply to this message Reply
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?

Joe Parks

Posts: 107
Nickname: joeparks
Registered: Aug, 2003

Re: Problems with Classpath, Packages and Imports Posted: Sep 3, 2003 6:06 AM
Reply to this message Reply
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.

Ken Sloan

Posts: 35
Nickname: sloan
Registered: Sep, 2003

Re: Problems with Classpath, Packages and Imports Posted: Sep 3, 2003 6:57 AM
Reply to this message Reply
Yes, I am using Windows 2000. Here are the directory listing and path...

Microsoft Windows 2000 [Version 5.00.2195]
(C) Copyright 1985-2000 Microsoft Corp.

c:\packtest>dir
Volume in drive C has no label.
Volume Serial Number is 70B7-31DA

Directory of c:\packtest

09/03/2003 08:29a <DIR> .
09/03/2003 08:29a <DIR> ..
09/02/2003 05:40p <DIR> com
09/02/2003 05:22p <DIR> objects
09/01/2003 09:18a <DIR> utilities
0 File(s) 0 bytes
5 Dir(s) 7,246,540,800 bytes free

c:\packtest>dir /S
Volume in drive C has no label.
Volume Serial Number is 70B7-31DA

Directory of c:\packtest

09/03/2003 08:29a <DIR> .
09/03/2003 08:29a <DIR> ..
09/02/2003 05:40p <DIR> com
09/02/2003 05:22p <DIR> objects
09/01/2003 09:18a <DIR> utilities
0 File(s) 0 bytes

Directory of c:\packtest\com

09/02/2003 05:40p <DIR> .
09/02/2003 05:40p <DIR> ..
09/02/2003 05:35p <DIR> util
0 File(s) 0 bytes

Directory of c:\packtest\com\util

09/02/2003 05:35p <DIR> .
09/02/2003 05:35p <DIR> ..
09/02/2003 05:35p 846 TicScreener.class
1 File(s) 846 bytes

Directory of c:\packtest\objects

09/02/2003 05:22p <DIR> .
09/02/2003 05:22p <DIR> ..
09/01/2003 12:40p 15,996 Owner.java
1 File(s) 15,996 bytes

Directory of c:\packtest\utilities

09/01/2003 09:18a <DIR> .
09/01/2003 09:18a <DIR> ..
08/31/2003 10:54a 655 TicScreener.java
1 File(s) 655 bytes

Total Files Listed:
3 File(s) 17,497 bytes
14 Dir(s) 7,246,540,800 bytes free

c:\packtest>echo %CLASSPATH%
%CLASSPATH%

c:\packtest>path
PATH=C:\WINNT\system32;C:\WINNT;C: \WINNT\System32\Wbem;c:\j2sdk1.4.2\bin;c:\Prog
ram Files\winzip;C:\packtest;

c:\packtest>

Interesting that "echo %CLASSPATH%" didn't produce anything... Could I have something set wrong?

Ken Sloan

Posts: 35
Nickname: sloan
Registered: Sep, 2003

Re: Problems with Classpath, Packages and Imports Posted: Sep 3, 2003 7:07 AM
Reply to this message Reply
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.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Problems with Classpath, Packages and Imports Posted: Sep 3, 2003 10:42 AM
Reply to this message Reply
Looks like classpath isn't set.

Try using the set command instead, "set c" should show all variables starting with 'c'.

Flat View: This topic has 18 replies on 2 pages [ 1  2 | » ]
Topic: Reading Images from JTextPane Previous Topic   Next Topic Topic: Reading Images from JTextPane

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use